I am trying to use xsim to design a workbook for students, with a second version available for tutors. Therein will be exercises/questions/homework, however you want to call it, and their respective solutions.

From what I reviewed so far, xsim, seems to be a mostly well-suited package to achieve this. However, there is one piece of information I could not find in the documentation: How do I switch between an empty answer space/blank/box and the printed solutions?

Ideally, I want a way for the answer space to take up exactly the phantom space of the typeset solution.

Attached you find a MWE of how I want it to work, however I wish to have the procedure automated.

The following MWE produces (notice the solution/print = true)

\documentclass{article}
\usepackage{mwe}
\usepackage{amssymb, amsmath}
\usepackage{xsim}
\usepackage{tcolorbox}

\DeclareExerciseEnvironmentTemplate{tcolorbox}
{%
\tcolorbox[
beforeafter skip = .5\baselineskip ,
title =
\textbf{\GetExerciseName~\GetExerciseProperty{counter}}%
\GetExercisePropertyT{subtitle}{ \textit{\PropertyValue}}%
]%
}
{\endtcolorbox}

\DeclareExerciseType{problem}{
exercise-env = problem ,
exercise-name = Problem ,
exercise-template = tcolorbox ,
solution-template = tcolorbox
}

\xsimsetup{
}

\begin{document}
\begin{problem}
Show that $(\mathbb{R}, +, \cdot)$ is a field.
\end{problem}

Let $a, b, c \in \mathbb{R}$ arbitrary. Then, we show the following properties:
\begin{description}
\item[Commutativity of addition]  $a + b = b+ a$.
\item[Associativity of addition] $a +(b + c) = (a+b) +c$.
\item[Additive identity] $0 \in \mathbb{R}$, and $a +0 = a$.
\item[Additive inverse] for each $a \in \mathbb{R}$, there exists $-a \in \mathbb{R}$, sch that $a + (-a) = 0$.
\item[Commutativity of multiplication] $a \cdot b = b \cdot a$.
\item[Associativity of multiplications] $a \cdot (b \dot c) = (a \cdot b) \cdot c$.
\item[Multiplicative identity] $1 \in \mathbb{R}$, such that $1 \cdot a = a$.
\item[Multiplicative inverse] for each $a \in \mathbb{R}$, there exists $a^{-1}\in \mathbb{R}$, such that $a \cdot a^{-1} = 1$.
\item[Distributivity] $a\cdot (b + c) = (a \cdot b) + (a \cdot c)$.
\end{description}
These are the necessary and sufficient properties of a field, hence $(\mathbb{R}, +, \cdot)$ is a field.
\end{document}


but I want an empty box with the same dimensions when answers are disabled.

# Update

Ultimately, I ended up ditching xsim, persued a different path, and came up with my own alternative solution, which is detailed in this other question and answer. However, it is still not an answer to this question.

# Update

One step later than ultimately, I retuned to using xsim. See my answer with the solution below.

• IIRC at least one of the example documents that comes with the xsim package has a solution for the question. Sep 24 '21 at 16:37
• Thank you for pointing me to these examples, I didn't notice them before. I believe I found a way to make work what I wanted to achieve, but I'm under pressure setting up my exercises now. Once done, I will add my solution as an answer myself.
– marc
Sep 27 '21 at 10:20
• While large parts of what I wanted are covered in your examples, a tiny bug prevents me from fully implementing my desired behaviour (bug report: github.com/cgnieder/xsim/issues/90). If you know a short fix, that would be great.
– marc
Sep 28 '21 at 8:43

In the meantime I have come up with an answer, however this answer depends on the version of xsim in use.

## xsim < v0.19b

See the updated MWE for example:

\documentclass{article}
\usepackage{mwe}
\usepackage{amssymb, amsmath}
\usepackage[]{xsim}
\usepackage{tcolorbox}

\xsimsetup{
solution/print = true,
}

\SetExerciseParameters{exercise}{
exercise-name = \XSIMtranslate{exercise},
solution-name = \XSIMtranslate{solution},
exercise-template = exercise,
solution-template = solution,
counter=section,
}

\DeclareExerciseEnvironmentTemplate{exercise}
{%
\tcolorbox[
beforeafter skip = .5\baselineskip ,
title =
\textbf{\GetExerciseName~\GetExerciseProperty{counter}}%
]%
}%
{%
\endtcolorbox
% at this point, add invisible solution
\IfSolutionPrintTF{}{%
\tcolorbox[%
upperbox=invisible,%
beforeafter skip = .5\baselineskip ,%
title =
\textbf{\GetExerciseParameter{solution-name}~\GetExerciseProperty{counter}}%
]%
\GetExerciseBody{solution}%
\endtcolorbox%
}%
}%

\DeclareExerciseEnvironmentTemplate{solution}%
{%
\tcolorbox[
beforeafter skip = .5\baselineskip ,
title =
\textbf{SOL: \GetExerciseName~\GetExerciseProperty{counter}}%
]%
}%
{\endtcolorbox}%

\begin{document}

\section{A section title}

\begin{exercise}
Show that $(\mathbb{R}, +, \cdot)$ is a field.
\end{exercise}

\begin{solution}
Let $a, b, c \in \mathbb{R}$ arbitrary. Then, we show the following properties:
\begin{description}
\item[Commutativity of addition]  $a + b = b+ a$.
\item[Associativity of addition] $a +(b + c) = (a+b) +c$.
\item[Additive identity] $0 \in \mathbb{R}$, and $a +0 = a$.
\item[Additive inverse] for each $a \in \mathbb{R}$, there exists $-a \in \mathbb{R}$, sch that $a + (-a) = 0$.
\item[Commutativity of multiplication] $a \cdot b = b \cdot a$.
\item[Associativity of multiplications] $a \cdot (b \dot c) = (a \cdot b) \cdot c$.
\item[Multiplicative identity] $1 \in \mathbb{R}$, such that $1 \cdot a = a$.
\item[Multiplicative inverse] for each $a \in \mathbb{R}$, there exists $a^{-1}\in \mathbb{R}$, such that $a \cdot a^{-1} = 1$.
\item[Distributivity] $a\cdot (b + c) = (a \cdot b) + (a \cdot c)$.
\end{description}
These are the necessary and sufficient properties of a field, hence $(\mathbb{R}, +, \cdot)$ is a field.
\end{solution}
\end{document}


produces the outputs (dependent on the printsolutions property):

## xsim v0.20

Ideally, the same code should work. However, it does not until this bug is resolved. The workaround in the issue's comments, namely using

\xsimsetup{collect=true}


and later in the document body using

\printcollection[print=both]{all exercises}


unfortunately appends a filled-in copy of the solutions if the option solutions/print=false is set.

So the current workaround for this answer is: Move the solution body into the exercise for printing:

\documentclass{article}
\usepackage{mwe}
\usepackage{amssymb, amsmath}
\usepackage[]{xsim}
\usepackage{tcolorbox}

\xsimsetup{
solution/print = true,  % toggle this line
collect=true,
}

\SetExerciseParameters{exercise}{
exercise-name = \XSIMtranslate{exercise},
solution-name = \XSIMtranslate{solution},
exercise-template = exercise,
solution-template = solution,
counter=section,
}

\DeclareExerciseEnvironmentTemplate{exercise}
{%
\tcolorbox[
beforeafter skip = .5\baselineskip ,
title =
\textbf{\GetExerciseName~\GetExerciseProperty{counter}}%
]%
}%
{%
\endtcolorbox
% at this point, add invisible solution
\IfSolutionPrintTF{%
\tcolorbox[%
beforeafter skip = .5\baselineskip ,%
title =
\textbf{\GetExerciseParameter{solution-name}~\GetExerciseProperty{counter}}%
]%
\GetExerciseBody{solution}%
\endtcolorbox%
}{%
\tcolorbox[%
upperbox=invisible,%
beforeafter skip = .5\baselineskip ,%
title =
\textbf{\GetExerciseParameter{solution-name}~\GetExerciseProperty{counter}}%
]%
\GetExerciseBody{solution}%
\endtcolorbox%
}%
}%

\DeclareExerciseEnvironmentTemplate{solution}%
{%
\tcolorbox[
beforeafter skip = .5\baselineskip ,
title =
\textbf{\GetExerciseName~\GetExerciseProperty{counter}}%
]%
}%
{\endtcolorbox}%

\begin{document}
\section{A section title}

\begin{exercise}
Show that $(\mathbb{R}, +, \cdot)$ is a field.
\end{exercise}

\begin{solution}
Let $a, b, c \in \mathbb{R}$ arbitrary. Then, we show the following properties:
\begin{description}
\item[Commutativity of addition]  $a + b = b+ a$.
\item[Associativity of addition] $a +(b + c) = (a+b) +c$.
\item[Additive identity] $0 \in \mathbb{R}$, and $a +0 = a$.
\item[Additive inverse] for each $a \in \mathbb{R}$, there exists $-a \in \mathbb{R}$, sch that $a + (-a) = 0$.
\item[Commutativity of multiplication] $a \cdot b = b \cdot a$.
\item[Associativity of multiplications] $a \cdot (b \dot c) = (a \cdot b) \cdot c$.
\item[Multiplicative identity] $1 \in \mathbb{R}$, such that $1 \cdot a = a$.
\item[Multiplicative inverse] for each $a \in \mathbb{R}$, there exists $a^{-1}\in \mathbb{R}$, such that $a \cdot a^{-1} = 1$.
\item[Distributivity] $a\cdot (b + c) = (a \cdot b) + (a \cdot c)$.
\end{description}
These are the necessary and sufficient properties of a field, hence $(\mathbb{R}, +, \cdot)$ is a field.
\end{solution}

\printcollection[print=exercises]{all exercises}
\end{document}


This produces the following desired output (identical to the previous version, but with less straightforward code and possible additional unobserved drawbacks):