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}
\shade[inner color=green!80!yellow,outer color=yellow!10!white]
(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},
attach title to upper=\quad,
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}
\shade[inner color=red!50!yellow,outer color=yellow!10!white]
(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:
lowerbox=invisible
lowerbox=invisible
, for even exercises... so this option must be automated for all the even exercises. Thank you