
Aqui está um MWE. Eu uso uma lista de tokens e consigo obter 10 e outubro no arquivo pdf final.
\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}
Então, se eu mudar para prop list (como diz o comentário), ou seja,
\NewDocumentCommand{\myget}{}
{
\prop_get:NnN \l_my_prop {test} \l_tmpa_tl
\tl_use:N \l_tmpa_tl
}
um erro é relatado para\MonthName{\myget}
! Missing number, treated as zero.
\prop_get
recupera o valor armazenado com key test
da lista de propriedades \l_my_prop
e coloca isso na variável da lista de tokens \l_tmpa_tl
. E eu uso essa variável. Comparado ao primeiro caso, acho que tem algo a ver com o \prop_get
. Mas não sei como consertar isso.
Responder1
\prop_get:NnN
não é expansível, então você não pode usar diretamente no \ifcase
. Quando \ifcase
tenta expandir \myget
(que não é expansível por causa de \prop_get:NnN
), ele falha e o TeX diz Missing number, treated as zero
.
No entanto, a tarefa que você está fazendo \l_tmpa_tl
é basicamente inútil, então você pode pular essa etapa e usar \prop_item:Nn
diretamente, que é expansível, e torná-lo \myget
expansível com \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}
Você pode usar mais expl3
-sim \int_case:nn
também:
\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}