使用 \newcommand 作为参数

使用 \newcommand 作为参数

在宏中使用 \numlist...

我 (仍然) 正在尝试编写试卷。我试图使我编写的代码尽可能易于重复使用,因此我尝试调整上述问题/答案中的代码以更好地满足我的需求。由于试卷首页的大部分内容是不变的,我已将其分离到一个包含文件中 - 但是,有一两条信息需要传递给这个包含文件。我能想到的唯一方法是通过\newcommand。但是,这会导致列表扩展的问题...当将作为参数\usegraphpaper传递时,函数会失败。我该如何解决这个问题?\newcommand

平均能量损失

\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

由于您的参数扩展为“安全”的东西,即零件编号,因此您可以在将其传递给主宏之前对其进行扩展。

我不会跟 混xstrings在一起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}

第二部分显示没有打印任何内容。

在此处输入图片描述

相关内容