由于某种原因,以下代码
\documentclass{article}
\usepackage[english,bulgarian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T2A]{fontenc}
\begin{document}
Здравей
\ExplSyntaxOn
\tl_set:Nx \l_tmpa_tl {Здравей}
\tl_map_inline:Nn \l_tmpa_tl { #1 ~ }
\ExplSyntaxOff
\end{document}
导致一系列错误
看起来这是问题所在\tl_map_inline
,因为存储和使用西里尔字母的令牌列表可以正常工作。
请注意,我使用的是 TeXLive 2022,而在 2019 上,上述代码可以完美运行。
我现在该如何修复 2022 设置中的问题?
答案1
如果你使用
\documentclass{article}
\usepackage[english,bulgarian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T2A]{fontenc}
\begin{document}
Здравей
\ExplSyntaxOn
\tl_set:Nx \l_tmpa_tl {Здравей}
\show\l_tmpa_tl
\tl_map_inline:Nn \l_tmpa_tl { #1 ~ }
\ExplSyntaxOff
\end{document}
您将看到当前版本定义
> \l_tmpa_tl=macro:
->Здравей.
旧版本
> \l_tmpa_tl=macro:
->\T2A\CYRZ \T2A\cyrd \T2A\cyrr \T2A\cyra \T2A\cyrv \T2A\cyre \T2A\cyrishrt .
通常新版本是首选,这里的实际用例是什么,可能有办法实现它?
一个快速的解决方法是将每个双字节 UTF-8 对放在一个组中:
> \l_tmpa_tl=macro:
->{З}{д}{р}{а}{в}{е}{й}.
\documentclass{article}
\usepackage[english,bulgarian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T2A]{fontenc}
\begin{document}
Здравей
\ExplSyntaxOn
\def\uviii#1#2{\ifx\relax#1\else{#1#2}\expandafter\uviii\fi}
\tl_set:Nx \l_tmpa_tl {\uviii Здравей\relax\relax}
%\show\l_tmpa_tl
\tl_map_inline:Nn \l_tmpa_tl { #1 ~ }
\ExplSyntaxOff
\end{document}
\documentclass{article}
\usepackage[english,bulgarian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T2A]{fontenc}
\begin{document}
Здравей
\ExplSyntaxOn
\def\uviii#1{\ifx\relax#1\else
\ifnum\expandafter`\string#1<128~
\expandafter\expandafter\expandafter\uviiia\else
\expandafter\expandafter\expandafter\uviiib
\fi
\fi
#1}
\def\uviiia#1{#1\uviii}
\def\uviiib#1#2{{#1#2}\uviii}
\tl_set:Nx \l_tmpa_tl {\uviii Здравейabc\relax}
%\show\l_tmpa_tl
\tl_map_inline:Nn \l_tmpa_tl { #1 ~ }
\ExplSyntaxOff
\end{document}
答案2
其他答案解释了为什么会发生这种情况。团队很可能很快会添加“文本映射”功能 - 作为目前的模型:
\documentclass{article}
\usepackage[T2A]{fontenc}
\ExplSyntaxOn
\cs_new:Npn \text_map_function:nN #1#2
{ \exp_args:Ne \__text_map_function:nN { \text_expand:n {#1} } #2 }
\cs_new:Npn \__text_map_function:nN #1#2
{
\__text_map_function:Nw #2 #1
\q__text_recursion_tail \q__text_recursion_stop
\prg_break_point:Nn \text_map_break: { }
}
\bool_lazy_or:nnTF
{ \sys_if_engine_luatex_p: }
{ \sys_if_engine_xetex_p: }
{
\cs_new:Npn \__text_map_function:Nw #1#2
{
\__text_if_recursion_tail_stop_do:Nn #2 { \text_map_break: }
#1 {#2}
\__text_map_function:Nw #1
}
}
{
\cs_new:Npn \__text_map_function:Nw #1#2
{
\__text_if_recursion_tail_stop_do:Nn #2 { \text_map_break: }
\bool_lazy_and:nnTF
{ \tl_if_single_token_p:n {#2} }
{ ! \token_if_cs_p:N #2 }
{
\int_compare:nNnTF { `#2 } > { "80 }
{
\int_compare:nNnTF { `#2 } < { "E0 }
{ \__text_map_function:NNN }
{
\int_compare:nNnTF { `#2 } < { "F0 }
{ \__text_map_function:NNNN }
{ \__text_map_function:NNNNN }
}
}
{ \__text_map_function:Nn }
#1 #2
}
{ \__text_map_function:Nn #1 {#2} }
}
\cs_new:Npn \__text_map_function:NNN #1#2#3
{ \__text_map_function:Nn #1 {#2#3} }
\cs_new:Npn \__text_map_function:NNNN #1#2#3#4
{ \__text_map_function:Nn #1 {#2#3#4} }
\cs_new:Npn \__text_map_function:NNNNN #1#2#3#4#5
{ \__text_map_function:Nn #1 {#2#3#4#5} }
\cs_new:Npn \__text_map_function:Nn #1#2
{
#1 {#2}
\__text_map_function:Nw #1
}
}
\cs_new:Npn \text_map_break:
{ \prg_map_break:Nn \text_map_break: { } }
\cs_new:Npn \text_map_break:n
{ \prg_map_break:Nn \text_map_break: }
\cs_new_protected:Npn \text_map_inline:nn #1#2
{
\int_gincr:N \g__kernel_prg_map_int
\cs_gset_protected:cpn
{ __text_map_ \int_use:N \g__kernel_prg_map_int :w } ##1 {#2}
\exp_args:Nnc \text_map_function:nN {#1}
{ __text_map_ \int_use:N \g__kernel_prg_map_int :w }
\prg_break_point:Nn \text_map_break:
{ \int_gdecr:N \g__kernel_prg_map_int }
}
\ExplSyntaxOff
\begin{document}
\ExplSyntaxOn
\text_map_inline:nn { Здравей } { #1 ~ }
\ExplSyntaxOff
\end{document}
答案3
hyperref 必须在本地取消保护 utf8 字符才能写入书签。您可以复制其代码:
\documentclass{article}
\usepackage[T2A]{fontenc}
\begin{document}
\makeatletter
\def\antshar@expand@utfvii{%
\count@"C2
\@tempcnta"F5
\def\UTFviii@tmp{\expandafter\def\expandafter~\expandafter{~}}%
\UTFviii@loop
}
\ExplSyntaxOn
\group_begin:
\antshar@expand@utfvii
\tl_set:Nx \l_tmpa_tl {Здравей}
\tl_map_inline:Nn \l_tmpa_tl { #1 ~ }
\group_end:
\ExplSyntaxOff
\end{document}