Como usar a variável de lista de tokens de \prop_get

Como usar a variável de lista de tokens de \prop_get

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_getrecupera o valor armazenado com key testda lista de propriedades \l_my_prope 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:NnNnão é expansível, então você não pode usar diretamente no \ifcase. Quando \ifcasetenta 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:Nndiretamente, que é expansível, e torná-lo \mygetexpansí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:nntambé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}

informação relacionada