将宏作为参数传递给包xsim

将宏作为参数传递给包xsim

我正在尝试使用 xsim 包编写一份文档。它分为几章,每章都包含自己的练习,每章末尾都有解决方案。

目前,每个章节的结构都是手写的,ChManual.tex但我想使用类似的东西macroChapter.tex,它使用\thechapter作为参数来调用和打印为 xsim 定义的集合。

如果您使用,以下 MWE 可以工作ChManual.tex,如果您取消注释第 1 章和第 2 章的行%\input{macroChapter.tex},它将无法工作并显示错误:unknown-collection "collCh\thechapter"

\documentclass{report}

\usepackage{filecontents}

\usepackage{xsim}

\DeclareExerciseCollection{collCh1}
\DeclareExerciseCollection{collCh2}
\DeclareExerciseCollection{collChManual}

\begin{filecontents}{Ch1.tex}

    \begin{exercise}
    This is exercise 1 from chapter \thechapter.
    \end{exercise}

    \begin{solution}
    This is solution of exercise 1 from chapter \thechapter.
    \end{solution}

\end{filecontents}

\begin{filecontents}{Ch2.tex}

    \begin{exercise}
    This is exercise 1 from chapter \thechapter.
    \end{exercise}

    \begin{solution}
    This is solution of exercise 1 from chapter \thechapter.
    \end{solution}

    \begin{exercise}
    This is exercise 2 from chapter \thechapter.
    \end{exercise}

    \begin{solution}
    This is solution of exercise 2 from chapter \thechapter.
    \end{solution}

\end{filecontents}

\begin{filecontents}{ChManual.tex}

    \begin{exercise}
    This is exercise 1 from chapter \thechapter (Manual).
    \end{exercise}

    \begin{solution}
    This is solution of exercise 1 from chapter \thechapter (Manual).
    \end{solution}

    \begin{exercise}
    This is exercise 2 from chapter \thechapter (Manual).
    \end{exercise}

    \begin{solution}
    This is solution of exercise 2 from chapter \thechapter (Manual).
    \end{solution}

\end{filecontents}

\begin{filecontents}{macroChapter.tex}

    This is chapter \thechapter.

    \collectexercises{collCh\thechapter}

    \input{Ch\thechapter.tex}

    \collectexercisesstop{collCh\thechapter}

    \printcollection[print=exercises]{collCh\thechapter}

    Bla bla bla bla bla bla bla bla bla bla bla.

    \printsolutions[chapter=\thechapter,collection=collCh\thechapter]

\end{filecontents}

\begin{filecontents}{manualChapter.tex}

    This is chapter Manual.

    \collectexercises{collChManual}

    \input{ChManual.tex}

    \collectexercisesstop{collChManual}

    \printcollection[print=exercises]{collChManual}

    Bla bla bla bla bla bla bla bla bla bla bla.

    \printsolutions[chapter=3,collection=collChManual]

\end{filecontents}

\begin{document}

\chapter{One}

%\input{macroChapter.tex}

\chapter{Two}

%\input{macroChapter.tex}

\chapter{Manual}

\input{manualChapter.tex}

\end{document}

答案1

这是“在将参数传递给下一个宏之前扩展该参数”的另一种变体。

具体来说,您需要x宏的变体\xsim_start_collection:n以及适配的用户命令:\xsim_stop_collection:n\xsim_print_collection:nn

\ExplSyntaxOn
\RenewDocumentCommand \collectexercises {t!m}
  {
    \IfBooleanTF {#1}
      { \xsim_start_collection:x {#2} }
      { \xsim_start_collection:n {#2} }
  }

\RenewDocumentCommand \collectexercisesstop {t!m}
  {
    \IfBooleanTF {#1}
      { \xsim_stop_collection:x {#2} }
      { \xsim_stop_collection:n {#2} }
  }

\RenewDocumentCommand \printcollection {t!O{}m}
  {
    \IfBooleanTF {#1}
      { \xsim_print_collection:nx {#2} {#3} }
      { \xsim_print_collection:nn {#2} {#3} }
  }

\cs_generate_variant:Nn \xsim_start_collection:n  {x}
\cs_generate_variant:Nn \xsim_stop_collection:n   {x}
\cs_generate_variant:Nn \xsim_print_collection:nn {nx}

\ExplSyntaxOff

然后使用(注意感叹号):

\begin{filecontents}{macroChapter.tex}

    This is chapter \thechapter.

    \collectexercises!{collCh\thechapter}

    \input{Ch\thechapter.tex}

    \collectexercisesstop!{collCh\thechapter}

    \printcollection![print=exercises]{collCh\thechapter}

    Bla bla bla bla bla bla bla bla bla bla bla.

    \printsolutions[chapter=\thechapter,collection=collCh\thechapter]

\end{filecontents}

在此处输入图片描述

顺便说一句:我宁愿使用\arabic{chapter}而不是\thechapter集合名称中的——你想要的是数字而不是打印的输出!

相关内容