Convert non-ASCII/UTF-8 characters into LaTeX codes

霸气de小男生 提交于 2019-12-10 10:46:47

问题


We have to convert non-ASCII, UTF-8, or named entity characters into LaTeX codes. Now we are using non-ASCII to Unicode, then Unicode to LaTeX/entity using a Perl script.

For example:

 ó --> \'{o}
 ó --> \'{o}
 ó --> \'{o}

Is there any direct conversion from non-ASCII, or UTF-8 to LaTeX codes in Perl program/script?


回答1:


This is very straightforward using the XML::Entities module to decode the entities, and the LaTeX::Encode module to re-encode them as LaTeX

Note that I've explicitly created an alias xml_decode for the decoding function, as the exported name is just decode, which is far too imprecise

use utf8;
use strict;
use warnings 'all';
use feature 'say';

use XML::Entities ();
use LaTeX::Encode 'latex_encode';
*xml_decode = \&XML::Entities::decode;

for my $s ( 'ó', 'ó', 'ó' ) {
    my $reencoded = latex_encode(xml_decode('all', $s));
    say $reencoded;
}

output

{\'o}
{\'o}
{\'o}


来源:https://stackoverflow.com/questions/38587627/convert-non-ascii-utf-8-characters-into-latex-codes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!