2

Following on from Use of \numlist in macro...

I am (still) trying to write exam papers. I'm trying to make the code I write as easy to re-use as possible, so I tried adapting the code from the above question/answer to fit my needs better. As much of the front page of the exam papers is constant, I've separated this out to an included file - however, there are one or two bits of information that need to be passed to this included file. The only way I could think of to do this is via a \newcommand. However, this causes problems with the expansion of lists... The \usegraphpaper function fails when passed the \newcommand as the parameter. How can I fix this?

MWE

\documentclass[a4paper,twoside,addpoints,12pt]{exam}
\usepackage{siunitx}
\usepackage{xstring}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\ExpandList}{m} % Used to expand lists of question numbers
{
    \justint_expandlist_prologue:n { #1 }
}

\seq_new:N \l__justint_expandlist_items_seq
\seq_new:N \l__justint_expandlist_items_paren_seq

\cs_new_protected:Nn \justint_expandlist_prologue:n
{
    \seq_set_split:Nnn \l__justint_expandlist_items_seq { ; } { #1 }
    {
        \seq_set_map:NNn
        \l__justint_expandlist_items_paren_seq % new seq
        \l__justint_expandlist_items_seq % old seq
        { ##1 } %
        \seq_use:Nnnn \l__justint_expandlist_items_paren_seq
        {~and~} % between two
        {,~} % between more than two
        {~and~} % between last two
    }
}

\ExplSyntaxOff

\newcommand{\usegraphpaper}[1]{ % If a (part) question is to be answered on graph paper. Separate part numbers using ;
    \ifdef{\NeedAnswerBook}{}{% If using an answer book, don't print anything
        \IfStrEqCase{#1}{%
            {}{%
                \textbf{Answer this question on the graph paper provided.}
            }%
        }[\textbf{Answer part\IfSubStr{#1}{;}{s}{} \ExpandList{#1} of this question on the graph paper provided.}]%
    }
}
\newcommand{\NeedInsert}{4;10}

\begin{document}

\usegraphpaper{1;2;3}

\usegraphpaper{4;5}

\usegraphpaper{6}

\usegraphpaper{}

\usegraphpaper{\NeedInsert}

\usegraphpaper{\NeedInsert{}}

\end{document}
1
  • 1
    I'd replace \ExpandList{#1} by \expandafter\ExpandList\expandafter{#1} in the definition of \usegraphpaper.
    – campa
    Mar 25, 2022 at 14:48

1 Answer 1

1

Since your argument expands to “safe” things, namely part numbers, you can expand it before passing it to the main macros.

I'd not mix xstrings with expl3.

\documentclass[a4paper,twoside,addpoints,12pt]{exam}
\usepackage{siunitx}
%\usepackage{xparse} % not needed with LaTeX after October 2020

\newif\ifNeedAnswerBook % is false by default

\ExplSyntaxOn

\NewDocumentCommand{\usegraphpaper}{m}
  {
    \justint_usegraphpaper:n { #1 }
  }

\seq_new:N \l__justint_expandlist_items_seq
\seq_new:N \l__justint_expandlist_items_paren_seq

\cs_new_protected:Nn \justint_usegraphpaper_arg:n
  {
    \seq_set_split:Nnn \l__justint_expandlist_items_seq { ; } { #1 }
    \seq_set_map:NNn
      \l__justint_expandlist_items_paren_seq % new seq
      \l__justint_expandlist_items_seq % old seq
      { ##1 } %
    \seq_use:Nnnn \l__justint_expandlist_items_paren_seq
      {~and~} % between two
      {,~} % between more than two
      {~and~} % between last two
  }
\cs_generate_variant:Nn \justint_usegraphpaper_arg:n { e }

\cs_new_protected:Nn \__justint_usegraphpaper_plural:n
  {
    \seq_set_split:Nnn \l__justint_expandlist_items_seq { ; } { #1 }
    \int_compare:nT { \seq_count:N \l__justint_expandlist_items_seq > 1 } { s }
  }
\cs_generate_variant:Nn \__justint_usegraphpaper_plural:n { e }

\cs_new_protected:Nn \justint_usegraphpaper:n
  {
    \legacy_if:nF {NeedAnswerBook}
      {
        \tl_if_empty:nTF { #1 }
          {
            \textbf{Answer~this~question~on~the~graph~paper~provided.}
          }
          {
           \textbf
             {
               Answer~part\__justint_usegraphpaper_plural:e { #1 }~
               \justint_usegraphpaper_arg:e { #1 }~of~this~question~
               on~the~graph~paper~provided.
             }
          }
      }
  }
\ExplSyntaxOff

\newcommand{\NeedInsert}{4;10}

\begin{document}

\usegraphpaper{1;2;3}

\usegraphpaper{4;5}

\usegraphpaper{6}

\usegraphpaper{}

\usegraphpaper{\NeedInsert}

\NeedAnswerBooktrue

X\usegraphpaper{1;2;3}X

X\usegraphpaper{4;5}X

X\usegraphpaper{6}X

X\usegraphpaper{}X

X\usegraphpaper{\NeedInsert}X

\end{document}

The second part shows that nothing is printed.

enter image description here

You must log in to answer this question.

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