Passe uma macro como parâmetro para o pacote xsim

Passe uma macro como parâmetro para o pacote xsim

Estou tentando escrever um documento usando o pacote xsim. Está dividido em capítulos e cada um contém seus próprios exercícios e suas soluções, ao final de cada um.

Atualmente, a estrutura de cada capítulo é escrita à mão como em, ChManual.texmas eu gostaria de usar algo como macroChapter.tex, que usa \thechaptercomo parâmetro para invocar e imprimir as coleções definidas para o xsim.

O seguinte MWE funciona se você usar ChManual.tex, se você descomentar as linhas %\input{macroChapter.tex}dos capítulos 1 e 2, não funciona mostrando erro: 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}

Responder1

Esta é mais uma variante de “expandir o argumento antes de passá-lo para a próxima macro”.

Especificamente , você deseja xvariantes das macros e também comandos de usuário adaptados:\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

Então use (observe os pontos de exclamação):

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

insira a descrição da imagem aqui

Aliás: prefiro usar \arabic{chapter}em vez dos \thechapternomes das coleções – você quer o número e não a saída impressa!

informação relacionada