1

I am trying to write a document using the xsim package. It is divided into chapters and each contains its own exercises and its solutions, at the end of each.

At present, the structure of each chapter is written by hand as in ChManual.tex but I would like to use something like macroChapter.tex, which uses \thechapter as parameter to invoke and print the collections defined for xsim.

The following MWE works if you use ChManual.tex, if you uncomment the lines %\input{macroChapter.tex} of chapters 1 and 2, it does not work showing an 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}
2
  • Welcome to TeX.SX! Please, make the example into a single file: the fact you're using \input is irrelevant.
    – egreg
    Jul 9, 2017 at 18:06
  • When using input I only wanted to show and justify the need for the content of the macroChapter.tex file (included multiple times in main doc) to be completely parametric in function of the macro \thechapter. Jul 10, 2017 at 18:21

1 Answer 1

1

This is yet another variant of “expand the argument before you pass it to the next macro”.

Specifically you want x variants of the macros \xsim_start_collection:n, \xsim_stop_collection:n and \xsim_print_collection:nn as well as adapted user commands:

\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

Then use (notice the exclamation marks):

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

enter image description here

BTW: I'd rather use \arabic{chapter} instead of the \thechapter in the collection names – you want the number and not the printed output!

5
  • Thanks a lot, but \printsolutions is missing. Jul 10, 2017 at 18:23
  • @Fernando I only did quick checks but I don't think a patch is needed for it.
    – cgnieder
    Jul 10, 2017 at 18:52
  • It is needed. Your solution does not work for me because \printsolutions generates the same error. I tried myself but I can't find the key. Jul 11, 2017 at 14:34
  • @FernandoNadal What I did was: add the patches to your MWE: it ran without errors and produced the picture I attached. But anyway, the patch is completely analoguous: you need the variant \xsim_print_all_solutions_per_type:nx of \xsim_print_all_solutions_per_type:nn. Check the definition of \printsolutions in xsim.interface.code.tex.
    – cgnieder
    Jul 11, 2017 at 15:40
  • Sorry, sorry, sorry !! My mistake. in my document, I added ! to all the comands, including \printsolutions. Jul 11, 2017 at 18:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .