Pasar una macro como parámetro al paquete xsim

Pasar una macro como parámetro al paquete xsim

Estoy intentando escribir un documento usando el paquete xsim. Está dividido en capítulos y cada uno contiene sus propios ejercicios y sus soluciones, al final de cada uno.

Actualmente, la estructura de cada capítulo está escrita a mano como en ChManual.texpero me gustaría usar algo como macroChapter.tex, que usa \thechaptercomo parámetro para invocar e imprimir las colecciones definidas para xsim.

El siguiente MWE funciona si usas ChManual.tex, si descomentas las líneas %\input{macroChapter.tex}de los capítulos 1 y 2 no funciona mostrando un error: 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}

Respuesta1

Esta es otra variante más de "expandir el argumento antes de pasarlo a la siguiente macro".

Específicamente desea xvariantes de las macros \xsim_start_collection:n, \xsim_stop_collection:nasí \xsim_print_collection:nncomo comandos de usuario adaptados:

\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

Luego use (observe los signos de exclamación):

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

ingrese la descripción de la imagen aquí

Por cierto: prefiero usarlo \arabic{chapter}en lugar de \thechapteren los nombres de las colecciones: ¡quieres el número y no la salida impresa!

información relacionada