パッケージ 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必要なのは番号であり、印刷された出力ではありません。

関連情報