
這是一個 MWE。我使用令牌列表,我可以在最終的pdf文件中獲得10和10月。
\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
-y \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}