2

I'm trying to write exercise sheets in a modular way: Each exercise has its own file with question/solution environments as provided by the exsheets package.

The exercise sheets then \inputs the needed exercise. The trouble is that I want to be able to decide, in each exercise sheet, whether or not I want to print the solutions. The exsheets package helps with that since you can give solution environments the print=true/false option.

So, I naturally tried setting a variable in my exercise sheet file, variable that would be read by each solution environment to decide if it should be printed or not, but it doesn't work, since I think the parameter given is not expanded early enough.

Here would be a MWE for the sheet file:

\documentclass{article}

\usepackage{exsheets}

\def\printsolutions{true}

\begin{document}
    Some text.  
    \input{Exercise1}
\end{document

and here an exercise:

\begin{question}
    Question
\end{question}
\begin{solution}[print=\printsolutions]
    Solution
\end{solution}

And the problem now is that latex complains that the option given to print is not a valid choice:

! LaTeX error: "kernel/key-choice-unknown"
! 
! Key 'exsheets/solution/print' accepts only a fixed set of choices.

How should that be solved?

Thanks in advance!

2 Answers 2

2

Define a command that can set the correct key:

\documentclass{article}


\usepackage{exsheets}

\ExplSyntaxOn
\NewDocumentCommand\setsolutionprint { m }
{
 \keys_set:nn { exsheets / solution}{ print = #1 }
}

\ExplSyntaxOff



\begin{document}

    Some text.
\setsolutionprint{true}

    \input{exercise1}

\end{document}

Attention:

exsheets doesn't work with a current expl3 version. See https://bitbucket.org/cgnieder/exsheets/issues/44/error-latex3-error-variant-form-n

3
  • That works fine, thanks! Two questions if I may: 1) What does the "m" stand for in the \NewDocumentCommand line? It doesn't seem to get used anywhere… 2) Can one use \setsolutionprint multiple time in the same document to selectively set it to true/false at different times? Commented Feb 19, 2019 at 10:51
  • 1) m stands for the mandatory argument (the #1 used later), 2) try it out. Commented Feb 19, 2019 at 10:53
  • 2) it does :) thanks again! Commented Feb 19, 2019 at 11:05
1

Here would be a MWE for the sheet file:

\documentclass{article}

\usepackage{exsheets}

\newcommand\Exchange[2]{#2#1}%
\def\printsolutions{true}

\begin{document}
    Some text.  
    \input{Exercise1}
\end{document}

and here an exercise:

\begin{question}
    Question
\end{question}
\expandafter\Exchange\expandafter{\printsolutions]}{\begin{solution}[print=}%
    Solution
\end{solution}

You must log in to answer this question.

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