I produce exercises with solutions, in book
class, chapter by chapter, using the doc in the last version of tcolorbox
. I want to produce the answers grouped in the end of the book. I can do this using \tcbstartrecording
in the beginning of the file and \tcbstoprecording
in the end of the file. At the end, the command \tcbinputrecords
gives the answers in the same order. I want to obtain the answers grouped by chapter, with a title for each group, "Solutions of the exercises of the chapter 1" for those of the first chapter, "Solutions of the exercises of the chapter 2" for those of the second chapter, and so on..
I give the page 4 of the pdf file what I obtain. I want the title "Solutions of the exercises of the chapter 1" before the box titled "Solution of Exercise 1.1 on page 1" and the title "Solutions of the exercises of the chapter 2" before the box titled "Solution of Exercise 2.1 on page 3". How can I do this?
Here is the .tex
file used:
\documentclass{book}
\usepackage[most]{tcolorbox}
\tcbuselibrary{skins,breakable}
\usepackage{polyglossia}
\setmainlanguage{english}
\tcbuselibrary{skins,xparse}
\NewTColorBox[auto counter,number within=chapter]{exercise}{m+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:#1},
attach title to upper=\quad,
after upper={\par\hfill\textcolor{green!40!black}%
{\itshape Solution on page~\pageref{solution:#1}}},
lowerbox=ignored,
savelowerto=solutions/exercise-\thetcbcounter.tex,
record={\string\solution{#1}{solutions/exercise-\thetcbcounter.tex}},
#2
}
\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}
\chapter{The first chapter}
\tcbstartrecording
\begin{exercise}{Ex1}[coltitle=cyan!80!black]
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}
\vspace*{1cm}
The solution of the exercise \ref{exercise:Ex1} is in the page \pageref{solution:Ex1}
\vspace*{1cm}
%\tcbstoprecording
%\tcbinputrecords
\chapter{The second chapter}
%\tcbstartrecording
\begin{exercise}{Ex2}[coltitle=cyan!80!black]
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}
\vspace*{1cm}
The solution of the exercise \ref{exercise:Ex2} is in the page \pageref{solution:Ex2}
\vspace*{1cm}
\tcbstoprecording
\newpage
\section{Solutions of the exercices}
\tcbinputrecords
\end{document}
and the page 4 of its compilation with Xelatex:
Best Answer
It is possible to prepend the
\chapter
command to write some information about the chapter into the record file. This will fail for the first chapter since the recording has not started then.It will also fail if the chapter number is reset.
Since the prepending occurs before the chapter number is increased, I've decided to
trick
\c@chapter
using a number increased by one within aTeX
group, so the counter value is not really changed but\thechapter
will use the tricked value for while and apply the correct format as defined for\thechapter
. This is better than using something like\arabic{chapter}
etc, in my point of view.Change the
\solutionchapterformat
macro at will to produce any nice/fancy heading of the solution-chapter-group (but leave the\bgroup... \egroup
)