这是 MWE。我使用标记列表,可以在最终的 pdf 文件中获得 10 和 October。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\tl_new:N \l_my_tl
\prop_new:N \l_my_prop
\NewDocumentCommand{\myset}{m}
{
\tl_set:Nn \l_my_tl { #1 }
\prop_put:Nnn \l_my_prop {test} {#1}
}
\NewDocumentCommand{\myget}{}
{
\tl_use:N \l_my_tl
%%% comment out the above line and uncomment the below lines
% \prop_get:NnN \l_my_prop {test} \l_tmpa_tl
% \tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff
\newcommand\MonthName[1]{%
\ifcase#1\relax\or
January \or Feburary \or March
\or April \or May \or June
\or July \or August \or September
\or October \or November \or December
\else#1\fi
}
\myset{10}
\begin{document}
\myget\\
\MonthName{\myget}
\end{document}
然后如果我切换到道具列表(正如评论所说),也就是说,
\NewDocumentCommand{\myget}{}
{
\prop_get:NnN \l_my_prop {test} \l_tmpa_tl
\tl_use:N \l_tmpa_tl
}
报告错误\MonthName{\myget}
! Missing number, treated as zero.
\prop_get
test
从属性列表 中恢复用 key 存储的值\l_my_prop
,并将其放置在标记列表变量 中\l_tmpa_tl
。我使用这个变量。与第一种情况相比,我认为它与 有关系\prop_get
。但我不知道如何修复它。
答案1
\prop_get:NnN
不可扩展,因此您不能直接在 中使用\ifcase
。当\ifcase
尝试扩展\myget
(由于 而不可扩展\prop_get:NnN
)时,它会失败并且 TeX 会说Missing number, treated as zero
。
但是,您所做的分配\l_tmpa_tl
基本上是无用的,因此您可以跳过该步骤并\prop_item:Nn
直接使用可扩展的,并\myget
使用以下方式使其可扩展\NewExpandableDocumentCommand
:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\tl_new:N \l_my_tl
\prop_new:N \l_my_prop
\NewDocumentCommand{\myset}{m}
{
\tl_set:Nn \l_my_tl { #1 }
\prop_put:Nnn \l_my_prop {test} {#1}
}
\NewExpandableDocumentCommand{\myget}{}
{
% \tl_use:N \l_my_tl
%%% comment out the above line and uncomment the below lines
\prop_item:Nn \l_my_prop {test}
}
\ExplSyntaxOff
\newcommand\MonthName[1]{%
\ifcase#1\relax\or
January \or Feburary \or March
\or April \or May \or June
\or July \or August \or September
\or October \or November \or December
\else#1\fi
}
\myset{10}
\begin{document}
\myget\\
\MonthName{\myget}
\end{document}
您可以使用更多expl3
-是 \int_case:nn
以及:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\tl_new:N \l_my_tl
\prop_new:N \l_my_prop
\NewDocumentCommand{\myset}{m}
{
\tl_set:Nn \l_my_tl { #1 }
\prop_put:Nnn \l_my_prop {test} {#1}
}
\NewExpandableDocumentCommand{\myget}{}
{
\prop_item:Nn \l_my_prop {test}
}
\NewExpandableDocumentCommand{\MonthName}{m}
{
\int_case:nnF { #1 }
{
{ 1 } { January }
{ 2 } { Feburary }
{ 3 } { March }
{ 4 } { April }
{ 5 } { May }
{ 6 } { June }
{ 7 } { July }
{ 8 } { August }
{ 9 } { September }
{ 10 } { October }
{ 11 } { November }
{ 12 } { December }
}
{ \int_eval:n {#1} }
}
\ExplSyntaxOff
\myset{10}
\begin{document}
\myget\\
\MonthName{\myget}
\end{document}