# Solutions of the even exercises only with the tcolorbox package

I produce exercises with tcolorbox for a textbook, and I want to let place for solution with the option lowerbox=invisible, for even exercises, and give the solutions of odd exercises in the end of the Textbook.. and, eventually, give all the solutions somewhere in the project of a book I'm writing about this course. I give an example, where I put the option lowerbox=invisible "locally", in the even exercises, to watch the reader the behavior I'm pursueing for the exercises, and I want to write the solutions only for even exercises, in the first time (inside the chapter), and in the second time, a file containing all the solutions, in the end of book.

I give examples of exercises, given in the doc of tcolorbox,

\documentclass{article}

\usepackage[most]{tcolorbox}
\tcbuselibrary{skins,breakable}

\NewTColorBox[auto counter,number within=section]{exercise}{+O{}}{%
enhanced,colframe=green!20!black,colback=yellow!10!white,coltitle=green!40!black,
fonttitle=\bfseries,
underlay={\begin{tcbclipinterior}
(interior.north west) circle (2cm);
\draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
(interior.south west) grid (interior.north east);
\end{tcbclipinterior}},
title={Exercise~ \thetcbcounter:},
label={exercise@\thetcbcounter},
after upper={\par\hfill\textcolor{green!40!black}%
{\itshape Solution on page~\pageref{solution@\thetcbcounter}}},
lowerbox=ignored,
savelowerto=solutions/exercise-\thetcbcounter.tex,
record={\string\solution{\thetcbcounter}{solutions/exercise-\thetcbcounter.tex}},
#1
}
\NewTotalTColorBox{\solution}{mm}{%
enhanced,colframe=red!20!black,colback=yellow!10!white,coltitle=red!40!black,
fonttitle=\bfseries,
underlay={\begin{tcbclipinterior}
(interior.north west) circle (2cm);
\draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
(interior.south west) grid (interior.north east);
\end{tcbclipinterior}},
title={Solution of Exercise~\ref{exercise@#1} on page~\pageref{exercise@#1}:},
phantomlabel={solution@#1},
attach title to upper=\par,
}{\input{#2}}
\tcbset{no solution/.style={no recording,after upper=}}

\begin{document}

\section{Exercises}
\tcbstartrecording
\begin{exercise}
Compute the derivative of the following function:
\begin{equation*}
f(x)=\sin((\sin x)^2)
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( \sin((\sin x)^2) \right) '
=\cos((\sin x)^2) 2\sin x \cos x.
\end{align*}
\end{exercise}
\begin{exercise}[lowerbox=invisible]    %%  Local option
Compute the derivative of the following function:
\begin{equation*}
f(x)=(\sin(\sin x))^2
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( (\sin(\sin x))^2 \right) '
=2\sin(\sin x) \cos(\sin x) \cos x.
\end{align*}
\end{exercise}
\begin{exercise}
Compute the derivative of the following function:
\begin{equation*}
f(x)=\sqrt{x^3-6x^2+2x}
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( \sqrt{x^3-6x^2+2x} \right) '
= \frac{3x^2-12x+2}{2\sqrt{x^3-6x^2+2x}}.
\end{align*}
\end{exercise}
\begin{exercise}[lowerbox=invisible]
Compute the derivative of the following function:
\begin{equation*}
f(x)=\cos((2x^2+3)^3)
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( \cos((2x^2+3)^3) \right) '
=-\sin((2x^2+3)^3) 3(2x^2+3)^2 2\cdot 2x\\
&=-12x(2x^2+3)^2\sin((2x^2+3)^3).
\end{align*}
\end{exercise}
\begin{exercise}
Compute the derivative of the following function:
\begin{equation*}
f(x)=(x^2+1) \sqrt{x^4+1}
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( (x^2+1) \sqrt{x^4+1} \right) '
= 2x\sqrt{x^4+1} + \frac{2x^3(x^2+1)}{\sqrt{x^4+1}}.
\end{align*}
\end{exercise}
\tcbstoprecording
\newpage
\section{Solutions}
\tcbinputrecords
\end{document}


and the pdf file obtained:

• With other words, even numbered solutions shall not be shown immediately, but only later on? – user31729 Mar 17 '18 at 12:49
• yes.. and even exercises produced with the option lowerbox=invisible – Faouzi Bellalouna Mar 17 '18 at 12:56
• Please restate your question ... your request is clamped together and it is hardly recognizable what you're really after... – user31729 Mar 17 '18 at 12:58
• I wrote "I want to let place for solution with the option lowerbox=invisible, for even exercises... so this option must be automated for all the even exercises. Thank you – Faouzi Bellalouna Mar 17 '18 at 13:05

The easiest way I see is to use a wrapper macro \solutionbox that decides to show the solution based on the swith \ifshowallsolutions or based on the number being odd or not.

The same is done for the exercise environment, preventing lowerbox=ignored and being invisible then.

Multiple labels warning are prevented by using a different label name for the full bunch of solutions. Also, references to dropped solutions are caught by using \IfRefUndefinedExpandable from refcount.

Update

\documentclass{article}

\usepackage{refcount}
\usepackage[most]{tcolorbox}

\newif\ifshowallsolutions

\makeatletter
\newcounter{tcb@cnt@exercise}

\NewTColorBox[use counter=tcb@cnt@exercise,number within=section]{exerciseinternal}{s+O{}}{%
enhanced,colframe=green!20!black,colback=yellow!10!white,coltitle=green!40!black,
fonttitle=\bfseries,
underlay={\begin{tcbclipinterior}
(interior.north west) circle (2cm);
\draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
(interior.south west) grid (interior.north east);
\end{tcbclipinterior}},
title={Exercise~ \thetcbcounter:},
label={exercise@\thetcbcounter},
after upper={\IfRefUndefinedExpandable{solution@\thetcbcounter}{}{\par\hfill\textcolor{green!40!black}%
{\itshape Solution on page~\pageref{solution@\thetcbcounter}}}},
IfBooleanTF={#1}{lowerbox=invisible}{lowerbox=ignored},
savelowerto=solutions/exercise-\thetcbcounter.tex,
record={\string\solutionbox{\thetcbcounter}{solutions/exercise-\thetcbcounter.tex}{\number\c@tcb@cnt@exercise}},
#2
}

\NewDocumentEnvironment{exercise}{+O{}}{%
\ifodd\c@tcb@cnt@exercise%
\exerciseinternal[#1]%
\else
\exerciseinternal*[#1]%
\fi
}{\endexerciseinternal}

\NewDocumentCommand{\solutionbox}{+m+m+m}{%
\ifshowallsolutions
\solution{#1}{#2}
\else
\ifodd#3%
\solution{#1}{#2}%
\fi
\fi
}

\NewTotalTColorBox{\solution}{mm}{%
enhanced,colframe=red!20!black,colback=yellow!10!white,coltitle=red!40!black,
fonttitle=\bfseries,
underlay={\begin{tcbclipinterior}
(interior.north west) circle (2cm);
\draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
(interior.south west) grid (interior.north east);
\end{tcbclipinterior}},
title={Solution of Exercise~\ref{exercise@#1} on page~\pageref{exercise@#1}:},
phantomlabel={\ifshowallsolutions solutionredisplayed@#1\else solution@#1\fi},
attach title to upper=\par,
}{%
\input{#2}%
}
\tcbset{no solution/.style={no recording,after upper=}}
\makeatother

\begin{document}

\section{Exercises}
\tcbstartrecording
\begin{exercise}[coltitle=blue]
Compute the derivative of the following function:
\begin{equation*}
f(x)=\sin((\sin x)^2)
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( \sin((\sin x)^2) \right) '
=\cos((\sin x)^2) 2\sin x \cos x.
\end{align*}
\end{exercise}
\begin{exercise}    %%  Local option
Compute the derivative of the following function:
\begin{equation*}
f(x)=(\sin(\sin x))^2
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( (\sin(\sin x))^2 \right) '
=2\sin(\sin x) \cos(\sin x) \cos x.
\end{align*}
\end{exercise}
\begin{exercise}
Compute the derivative of the following function:
\begin{equation*}
f(x)=\sqrt{x^3-6x^2+2x}
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( \sqrt{x^3-6x^2+2x} \right) '
= \frac{3x^2-12x+2}{2\sqrt{x^3-6x^2+2x}}.
\end{align*}
\end{exercise}
\begin{exercise}[lowerbox=invisible]
Compute the derivative of the following function:
\begin{equation*}
f(x)=\cos((2x^2+3)^3)
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( \cos((2x^2+3)^3) \right) '
=-\sin((2x^2+3)^3) 3(2x^2+3)^2 2\cdot 2x\\
&=-12x(2x^2+3)^2\sin((2x^2+3)^3).
\end{align*}
\end{exercise}
\begin{exercise}
Compute the derivative of the following function:
\begin{equation*}
f(x)=(x^2+1) \sqrt{x^4+1}
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( (x^2+1) \sqrt{x^4+1} \right) '
= 2x\sqrt{x^4+1} + \frac{2x^3(x^2+1)}{\sqrt{x^4+1}}.
\end{align*}
\end{exercise}
\tcbstoprecording
\newpage
\section{Selected Solutions}
\tcbinputrecords

\section{All solutions}
\showallsolutionstrue
\tcbinputrecords
\end{document}


• Very ingenious solution. Now, how can I insert the option lowerbox=invisible, for all the even exercises ? – Faouzi Bellalouna Mar 17 '18 at 13:08
• I accepted your answer, even if it's incomplete for my purpose, and your ideas are wellcome for automating the option lowerbox=invisible .. – Faouzi Bellalouna Mar 17 '18 at 13:23
• @FaouziBellalouna: I've an idea for that... – user31729 Mar 17 '18 at 13:28
• Other thing. I don't understand very well your sentence "However, this will cause multiple labels."... – Faouzi Bellalouna Mar 17 '18 at 13:29
• I looked again to the details of the solution.. the doc of the refcount package, the macros used, IfBooleanTF, IfRefUndefinedExpandable..etc .. The method you introduced is really ingenious and very useful for me. Many thanks another time – Faouzi Bellalouna Mar 17 '18 at 16:37