2

Here is a MWE. I use a token list, and I can get 10 and October in the final pdf file.

\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}

Then if I switch to prop list (as the comment says), that is,

\NewDocumentCommand{\myget}{}
{
     \prop_get:NnN \l_my_prop {test} \l_tmpa_tl
     \tl_use:N \l_tmpa_tl
}

an error is reported for \MonthName{\myget}

! Missing number, treated as zero.

\prop_get recovers the value stored with key test from the property list \l_my_prop, and places this in the token list variable \l_tmpa_tl. And I use this variable. Compared to the first case, I think it has something to do the \prop_get. But I don't know how to fix it.

1 Answer 1

3

\prop_get:NnN isn't expandable, so you can't use directly in \ifcase. When \ifcase tries to expand \myget (which isn't expandable because of \prop_get:NnN) it fails and TeX says Missing number, treated as zero.

However, the assignment you are doing to \l_tmpa_tl is basically useless, so you can skip that step and use \prop_item:Nn directly, which is expandable, and make \myget expandable with \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}

You can use the more expl3-y \int_case:nn as well:

\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}
1
  • Nice. Thank you for your explanation and working demo.
    – jiewuza
    Mar 21, 2019 at 3:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .