I'm currently writing a .sty
-file that defines the look of assignment-sheets for students. I now want do define some custom environments for the questions and answers.
The answer blocks are printed inside of a tcolorbox
and only shown, if a boolean is set to true. So in the definition of my custom environment I first check the boolean and then paste the tcolorbox
opening tag.
Unfortunately this returns the following error:
LaTeX Error: \begin{tcb@savebox} on input line 62 ended by \end{enumerate}.
Here is the code:
\newenvironment{solution}
{
\ifthenelse{\boolean{solution}}{
\begin{tcolorbox}[breakable, width=\textwidth, colframe=red, colback=white]
}
{
\end{tcolorbox}
}{}
}
I used the code like this:
\begin{solution}
\begin{align*}
x^2 + y^2 &= z^2\\
\Rightarrow x &= \sqrt{z^2 - y^2}\\
&= ...
\end{align*}
\end{solution}
What have I done wrong? Thank you very much in advance!
Clarification
It seems that my question is a bit unclear, so I'm trying to explain it a little better by the use of the following picture.
Note that the page on the left only displays the assignments and the page on the right also includes the solutions to the problems. What I want to do, is to be able to compile both of these documents out of one single .tex
file. I don't want to have a file for the assignments and one for the solutions. In my preamble i want to set a boolean to either true
or false
. If the boolean solution
is set to false
, the solution should NOT be compiled, so the resulting document is the one on the left. If the boolean
is set to true
, the solutions should be compiled, so the resulting document is the one on the right.
The solution
-environment I asked for should check if the boolean
is set to true
and if so, compile it's content.
Until now it worked if I coded like this:
\begin{enumerate}[a)]
%
%
%%%%%%%%%%%%%%%
%% Question
\item Compute the Fourier transform of $e^{-|x|}$ for $x\in \mathbb{R}$.
%
%
%%%%%%%%%%%%%%%
%% Solution
\ifthenelse{\boolean{solution}}{
\begin{tcolorbox}[breakable, width=\textwidth, colframe=red, colback=white]
\begin{eqnarray*}
\hat{f}(\xi)&=&\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}e^{-|x|}e^{-ix\xi}dx\\
&=&\frac{1}{\sqrt{2\pi}}\int_{0}^{\infty}e^{-x-ix\xi}dx+\int_{-\infty}^0e^{x-ix\xi}dx\\
&=&\frac{1}{\sqrt{2\pi}}\int_{0}^{\infty}(e^{-x-ix\xi}-e^{-x+ix\xi})dx\\
&=&\frac{1}{\sqrt{2\pi}}[\frac{1}{-(1+i\xi)}(-1)-\frac{1}{-1+i\xi}(-1)]\\
&=&\frac{1}{\sqrt{2\pi}}[\frac{1-i\xi}{1+\xi^2}+\frac{-(1+i\xi)}{1+\xi^2}]\\
&=&\frac{1}{\sqrt{2\pi}}\frac{-2i\xi}{1+\xi^2}\\
&=&-\sqrt{\frac{2}{\pi}}\frac{i\xi}{1+\xi^2}
\end{eqnarray*}
\end{tcolorbox}
}{}
%
%
\item Compute the Fourier transform of $e^{-a|x|^2},~a>0$, directly, where $x\in \mathbb{R}$.\\
\ifthenelse{\boolean{solution}}{
\begin{tcolorbox}[breakable, width=\textwidth, colframe=red, colback=white]
\begin{eqnarray*}
\hat{f}(\xi)&=&\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}e^{-a|x|^2}e^{-ix\xi}dx\\
&=&\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}e^{-a(x+\frac{i\xi}{2a})^2+\frac{-\xi^2}{4a}}dx~~~~~~~~x'\doteq x+\frac{i\xi}{2a}\\
&=&\frac{1}{\sqrt{2\pi}}e^{-\frac{\xi^2}{4a}}\int_{-\infty}^{\infty}e^{-ax^2}dx\\
&=&\frac{e^{-\frac{\xi^2}{4a}}}{2a}
\end{eqnarray*}
\end{tcolorbox}
}{}
\end{enumerate}
The custom solution
-environment should combine these two lines (and their closing tags) to make programming quicker and cleaner:
\ifthenelse{\boolean{solution}}{
\begin{tcolorbox}[breakable, width=\textwidth, colframe=red, colback=white]
Hopefully this clears up the misunderstandings. If you have any further question, feel free to ask. 🙂
Best Answer
The
\ifthenelse
condition ends prematurely and leaves an openenvironment
hanging around in the middle of nowhere.In conjunction with
tcolorbox
environment, the end - delimiter is\endtcolorbox
and I suggest to use two\ifthenelse
statements, one for thestart
code of the environment and another one for the end code.A better approach would use
\DeclareTColorbox
, in my opinion or a weird\scantokens
construct.Also possible: Use
\tcolorboxenvironment
to wrap around an existingsolution
environment.Cleaner solution with two different environments
Third installment of a solution with
\NewEnviron
and the\BODY
command.The
\BODY
command contains the environment 'text' and is printed only in the casesolution
is true.