我尝试创建一个显示欧元值的命令。我希望它显示不同的类型(0 个小数位、2 个小数位或全部小数位),并通过命令进行设置\seteurformat
。
效果很好(代码的未注释部分)。
我还想选择临时更改一次命令调用的长度,因此创建了一个局部变量,但不知何故,从全局变量到局部变量的复制不起作用并且它保持为空,导致不显示任何内容...
\documentclass{scrlttr2}
\usepackage[ngerman]{babel}
\RequirePackage[defaultsans, scale=0.9]{opensans}
\renewcommand\familydefault{\sfdefault}
\usepackage[detect-all, separate-uncertainty]{siunitx}
\sisetup{group-minimum-digits = 4, group-separator = {~}, locale=DE}
\ExplSyntaxOn
\str_new:N \g__eur_length_str
\NewDocumentCommand{\seteurformat}{m}
{
\str_gset:Nn \g__eur_length_str { #1 }
}
\str_new:N \l__eur_length_temp_str
\NewDocumentCommand{\EUR}{O{}m}
{
\str_if_empty:nTF { #1 }
{ % true
\str_set:Nn \l__eur_length_temp_str { \str_show:N \g__eur_length_str }
}
{ % false
\str_set:Nn \l__eur_length_temp_str { #1 }
}
\str_case:Vn \l__eur_length_temp_str
{
{full}{\num[group-separator = {.}, minimum-decimal-digits=2]{#2} \nobreakspace \unit{€}}
{standard}{\num[group-separator = {.},round-mode = places, round-precision = 2]{#2} \nobreakspace \unit{€}}
{min}{\num[group-separator = {.}, round-mode = places, round-precision = 0]{#2} \nobreakspace \unit{€}}
}
% this part works but then I lose the option to temporarily change the length with #1
% \str_case:Vn \g__eur_length_str
% {
% {full}{\num[group-separator = {.}, minimum-decimal-digits=2]{#2} \nobreakspace \unit{€}}
% {standard}{\num[group-separator = {.},round-mode = places, round-precision = 2]{#2} \nobreakspace \unit{€}}
% {min}{\num[group-separator = {.}, round-mode = places, round-precision = 0]{#2} \nobreakspace \unit{€}}
% }
}
\ExplSyntaxOff
\begin{document}
\seteurformat{full}
\EUR{456}
\seteurformat{min}
\EUR{456}
\EUR[full]{456}
\EUR{456}
\EUR[min]{456}
\EUR{456}
\end{document}
答案1
你似乎对它有什么误解\str_show:N
。如果你想将一个字符串设置为另一个字符串的值,你可以这样做
\str_set_eq:NN \l__eur_length_temp_str \g__eur_length_str
这是一个固定的代码:
\documentclass{article}
\usepackage[default, scale=0.9]{opensans}
\usepackage[detect-all, separate-uncertainty]{siunitx}
\sisetup{group-minimum-digits = 4, group-separator = {~}, locale=DE}
\ExplSyntaxOn
\str_new:N \g__eur_length_str
\str_new:N \l__eur_length_temp_str
\NewDocumentCommand{\seteurformat}{m}
{
\str_gset:Nn \g__eur_length_str { #1 }
}
\NewDocumentCommand{\EUR}{om}
{
\group_begin:
\IfNoValueTF{#1}
{% no optional argument
\str_set_eq:NN \l__eur_length_temp_str \g__eur_length_str
}
{% optional argument present
\str_set:Nn \l__eur_length_temp_str { #1 }
}
\str_case:Vn \l__eur_length_temp_str
{
{full}{\num[group-separator = {.}, minimum-decimal-digits=2]{#2} \nobreakspace \unit{€}}
{standard}{\num[group-separator = {.},round-mode = places, round-precision = 2]{#2} \nobreakspace \unit{€}}
{min}{\num[group-separator = {.}, round-mode = places, round-precision = 0]{#2} \nobreakspace \unit{€}}
}
\group_end:
}
\ExplSyntaxOff
\begin{document}
\seteurformat{full}
\EUR{456} (full)
\seteurformat{min}
\EUR{456} (min)
\EUR[full]{456} (full)
\EUR{456} (min)
\EUR[min]{456} (min)
\EUR[standard]{456} (standard)
\end{document}
更好的实现是,将用户级命令交给内部函数,无需复制字符串。
\documentclass{article}
\usepackage[default, scale=0.9]{opensans}
\usepackage[detect-all, separate-uncertainty]{siunitx}
\sisetup{group-minimum-digits = 4, group-separator = {~}, locale=DE}
\ExplSyntaxOn
\str_new:N \g__eur_length_str
\NewDocumentCommand{\seteurformat}{m}
{
\str_gset:Nn \g__eur_length_str { #1 }
}
\NewDocumentCommand{\EUR}{om}
{
\IfNoValueTF{#1}
{% no optional argument
\mrcarnivore_eur_print:Vn \g__eur_length_str { #2 }
}
{% optional argument present
\mrcarnivore_eur_print:nn { #1 } { #2 }
}
}
\cs_new_protected:Nn \mrcarnivore_eur_print:nn
{
\str_case:nn { #1 }
{
{full}{\num[group-separator = {.}, minimum-decimal-digits=2]{#2} \nobreakspace \unit{€}}
{standard}{\num[group-separator = {.},round-mode = places, round-precision = 2]{#2} \nobreakspace \unit{€}}
{min}{\num[group-separator = {.}, round-mode = places, round-precision = 0]{#2} \nobreakspace \unit{€}}
}
}
\cs_generate_variant:Nn \mrcarnivore_eur_print:nn { V }
\ExplSyntaxOff
\begin{document}
\seteurformat{full}
\EUR{456} (full)
\seteurformat{min}
\EUR{456} (min)
\EUR[full]{456} (full)
\EUR{456} (min)
\EUR[min]{456} (min)
\EUR[standard]{456} (standard)
\end{document}