# Solutions of exercises produced with tcolorbox-Numbering titles

Using the brilliant macros from the tcolorbox package, I write in a book of probability, in every chapter, exercises with solutions. Because I want to write all the solutions together in a chapter, I use the really ingenious macro developed in my question here:

Solutions of exercises produced with tcolorbox

Now, I use "literal" numbering of the chapters, by using the following macro:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  MACRO FOR LITERAL NUMBERING CHAPTERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\words[1]{\expandafter\xwords\csname c@#1\endcsname}
\def\xwords#1{\ifcase#1\or
one\or
two\or
three\or
four\or
five\or
six\or
seven\or
eight\or
nine\or
ten
\else
I need more words\fi}

\usepackage{etoolbox} %% comment if 'etoolbox' have been loaded before
\makeatletter
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  END MACRO FOR LITERAL NUMBERING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


and I want to use the same numbering for the titles of solutions, i.e. something like Solutions of the exercises of the chapter one,

Solutions of the exercises of the chapter two.. etc.

How can I do this ?

I give here the .tex file used:

\documentclass{book}

\usepackage[most]{tcolorbox}

\usepackage{xpatch}

% Formatting command as a 'headline' of the solutions of chapter X
\NewDocumentCommand{\solutionchapterformat}{m}{%
\noindent \bgroup\bfseries Solutions of the exercises of the chapter #1\egroup%
}
\makeatletter

\xpretocmd{\chapter}{%
\begingroup
%%    \ifnum\value{chapter}>0\relax
%%    \tcbrecord{\string\clearpage}% Write a clearpage after the first chapter for each new chapter
%%    \fi   %% Uncomment these 3 lines if you want a pagebreak between the solutions of 2 chapters
\c@chapter \numexpr\c@chapter+1% Increase the count register \@chapter by one to trick \thechapter using the 'correct' chapter number
\tcbrecord{%
\solutionchapterformat{\thechapter}}%
\endgroup
}{}{}

\NewDocumentCommand{\extrasolutioncontent}{+m}{%
%%\tcbrecord{Extra solution stuff\par}% Remove this later on!
\tcbrecord{\detokenize{#1}}%
}

\newcommand{\fetchsolutions}{%
%For the first chapter
\begingroup
\c@chapter1%
\solutionchapterformat{\thechapter}%
\endgroup% Now get the rest of the stuff
\tcbinputrecords
}
\makeatother

\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}
(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},
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}
(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=}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  MACRO FOR LITERAL NUMBERING CHAPTERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\words[1]{\expandafter\xwords\csname c@#1\endcsname}
\def\xwords#1{\ifcase#1\or
one\or
two\or
three\or
four\or
five\or
six\or
seven\or
eight\or
nine\or
ten
\else
I need more words\fi}

\usepackage{etoolbox} %% comment if 'etoolbox' have been loaded before
\makeatletter
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  END MACRO FOR LITERAL NUMBERING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{geometry}
\geometry{paperwidth=180mm, paperheight=180mm, top=-10mm, bottom=25mm}

\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}

\chapter{The second chapter}
\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}

We can write any code between two exercises, directly...

\extrasolutioncontent{We can write too any code between two solutions, by using the ingenious $\backslash$extrasolutioncontent macro developed in the answer...}

\begin{exercise}{Ex3}[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}

\tcbstoprecording
\newpage
\chapter{Solutions of the exercices}
\fetchsolutions
\end{document}


and the compilation of some "interesting" pages...

Edit. I edit my question to give an answer, using pgfplots package, what I gave into an "answer" but the webmasters of the site said that I must edit my question to give an answer, and I do it !

The following commands, in the preamble:

\def\mywordscounter{{"zero","one","two","three"}} %%% You can go far from..
%%  Counters of pgf begins always by 0...
%%  The non-numeric-values must be between " "

% Formatting command as a 'headline' of the solutions of chapter X
\NewDocumentCommand{\solutionchapterformat}{m}{%
\section{Solutions of the exercises of the chapter  \protect\pgfmathparse{\mywordscounter[#1]}\pgfmathresult}%
}


enables to do the job. One can note the command \protect added before the \solutionchapterformat command, on the suggestion of @ChristianHupfer, which gives the suitable output..

Unfortunately, there's a problem in my file because its contents are false ! Here is the tex file used after update:

\documentclass{book}

\usepackage[most]{tcolorbox}

\usepackage{xpatch}
\usepackage{pgfplots}
\def\mywordscounter{{"zero","one","two","three"}} %%% You can go far from..
%%  Counters of pgf begins always by 0...
%%  The non-numeric-values must be between " "

% Formatting command as a 'headline' of the solutions of chapter X
\NewDocumentCommand{\solutionchapterformat}{m}{%
\section{Solutions of the exercises of the chapter  \protect\pgfmathparse{\mywordscounter[#1]}\pgfmathresult}%
}
\makeatletter

\xpretocmd{\chapter}{%
\begingroup
%%    \ifnum\value{chapter}>0\relax
%%    \tcbrecord{\string\clearpage}% Write a clearpage after the first chapter for each new chapter
%%    \fi   %% Uncomment these 3 lines if you want a pagebreak between the solutions of 2 chapters
\c@chapter \numexpr\c@chapter+1% Increase the count register \@chapter by one to trick \thechapter using the 'correct' chapter number
\tcbrecord{%
\solutionchapterformat{\thechapter}}%
\endgroup
}{}{}

\NewDocumentCommand{\extrasolutioncontent}{+m}{%
%%\tcbrecord{Extra solution stuff\par}% Remove this later on!
\tcbrecord{\detokenize{#1}}%
}

\newcommand{\fetchsolutions}{%
%For the first chapter
\begingroup
\c@chapter1%
\solutionchapterformat{\thechapter}%
\endgroup% Now get the rest of the stuff
\tcbinputrecords
}
\makeatother

\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}
(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},
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}
(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=}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  MACRO FOR LITERAL NUMBERING CHAPTERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\words[1]{\expandafter\xwords\csname c@#1\endcsname}
\def\xwords#1{\ifcase#1\or
one\or
two\or
three
\else
I need more words\fi}

\usepackage{etoolbox} %% comment if 'etoolbox' have been loaded before
\makeatletter
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  END MACRO FOR LITERAL NUMBERING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\tableofcontents

\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}

\chapter{The second chapter}
\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}

We can write any code between two exercises, directly...

\extrasolutioncontent{We can write too any code between two solutions, by using the ingenious $\backslash$extrasolutioncontent macro developed in the answer...}

\begin{exercise}{Ex3}[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}

\tcbstoprecording
\newpage
\fetchsolutions
\end{document}


which gives bad output in the contents... "Solutions of the exercises of the chapter 0.017" !!

What's the problem ?

• I am quite occupied right now (my paid daily job at school prevents unpaid support round the clock, unfortunately ;-)) others will do the job ;-)
– user31729
Mar 7, 2017 at 6:45
• Luckily I found some time slot
– user31729
Mar 7, 2017 at 9:16
• Many thanks for your disponibility. Texifying is really happy with you, and all the team Mar 7, 2017 at 10:27

Use the fmtcount package and its \numberstring and \numberstringnum macros to produce the word correspondence of numbers.

\documentclass{book}

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

\usepackage{xpatch}

% Formatting command as a 'headline' of the solutions of chapter X
\NewDocumentCommand{\solutionchapterformat}{m}{%
\noindent \bgroup\bfseries Solutions of the exercises of the chapter \numberstringnum{#1}\egroup%
}
\makeatletter

\xpretocmd{\chapter}{%
\begingroup
%%    \ifnum\value{chapter}>0\relax
%%    \tcbrecord{\string\clearpage}% Write a clearpage after the first chapter for each new chapter
%%    \fi   %% Uncomment these 3 lines if you want a pagebreak between the solutions of 2 chapters
\c@chapter \numexpr\c@chapter+1% Increase the count register \@chapter by one to trick \thechapter using the 'correct' chapter number
\tcbrecord{%
\solutionchapterformat{\thechapter}}%
\endgroup
}{}{}

\NewDocumentCommand{\extrasolutioncontent}{+m}{%
%%\tcbrecord{Extra solution stuff\par}% Remove this later on!
\tcbrecord{\detokenize{#1}}%
}

\newcommand{\fetchsolutions}{%
%For the first chapter
\begingroup
\c@chapter1%
\solutionchapterformat{\thechapter}%
\endgroup% Now get the rest of the stuff
\tcbinputrecords
}
\makeatother

\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}
(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},
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}
(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=}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  MACRO FOR LITERAL NUMBERING CHAPTERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\words[1]{\expandafter\xwords\csname c@#1\endcsname}
\def\xwords#1{\ifcase#1\or
one\or
two\or
three
\else
I need more words\fi}

\renewcommand{\words}[1]{\numberstring{chapter}}

\usepackage{etoolbox} %% comment if 'etoolbox' have been loaded before
\makeatletter
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  END MACRO FOR LITERAL NUMBERING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{geometry}
\geometry{paperwidth=180mm, paperheight=180mm, top=-10mm, bottom=25mm}

\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}

\chapter{The second chapter}
\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}

We can write any code between two exercises, directly...

\extrasolutioncontent{We can write too any code between two solutions, by using the ingenious $\backslash$extrasolutioncontent macro developed in the answer...}

\begin{exercise}{Ex3}[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}

\tcbstoprecording
\newpage
\chapter{Solutions of the exercices}
\fetchsolutions
\end{document}


• Ingenious macro as usual. Thanks for this happy TeXifying Mar 7, 2017 at 10:28
• Unfortunately... this was too beautiful !!... I haven't knew what's the fmtcount package exactly... when I read the doc, I find this: Version 1.02 of the fmtcount package now has limited multilingual support. The following languages are implemented: English, Spanish, Portuguese, French, French (Swiss) and French (Belgian). German support was added in version 1.1.2 Italian support was added in version 1.31. But my book is written in arabic, and I thank that there's not a language problem, but there is ! Mar 7, 2017 at 15:03
• So have you another solution based on my own counter words ? Mar 7, 2017 at 15:05
• @FaouziBellalouna: Well, there's not a single word in your question that you need support for Arabic language. How many chapters do you plan? If it is limited to 10 etc, the \words macro can be used anyway, you have to replace the English expressions by the Arabic ones (including the fonts) -- I neither speak Arabic nor can I read the Arabic 'alphabet' ...
– user31729
Mar 7, 2017 at 15:52
• Ok I haven't say that I use the arabic language because all happens the same stricto sensu else for some things and I don't stop traveling between right-to-left and left-to-right modes, but there's more and more beautiful macros developed for this task... Mar 7, 2017 at 16:11

I can solve my question by using the pgfplots package, which enables to define "literal" counters. The following commands in the preamble of my question:

\usepackage{pgfplots}
\def\mywordscounter{{"zero","one","two","three"}} %%% You can go far from..
%%  Counters of pgf begins always by 0...
%%  The non-numeric-values must be between " "

% Formatting command as a 'headline' of the solutions of chapter X
\NewDocumentCommand{\solutionchapterformat}{m}{%
\noindent\bgroup\bfseries Solutions of the exercises of the chapter  \pgfmathparse{\mywordscounter[#1]}\pgfmathresult\egroup%
}


gives the suitable behaviour. Thanks to Christian Hupfer for its original beautiful macro.

But there's a final last problem !! If I want my output title of solutions in format of section. If I use instead the command:

\NewDocumentCommand{\solutionchapterformat}{m}{%
\section{Solutions of the exercises of the chapter  \pgfmathparse{\mywordscounter[#1]}\pgfmathresult}%
}


I obtain the error message:

! Incomplete \iffalse; all text was ignored after line ...

What am I doing wrong here ?

• You should ask a follow-up question for your final problem and edit your answer. It shouldn't ask questions but should rather be a self-contained answer to the above question. (I accidently flagged this as not an answer. Sorry…) Mar 8, 2017 at 13:37
• You need to protect the command \pgfmathparse, i.e. \section{....\protect\pgfmathparse{...}} etc.
– user31729
Mar 8, 2017 at 15:52