# Math book: Exercise - Solution environment

If I use this code provided by a question formulate of Sebastian for exercise environment.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\newtcolorbox[auto counter]{problem}[2][]{%
breakable,
blankest,
enhanced,
attach boxed title to top left={yshift=-\tcboxedtitleheight},
left=\tcboxedtitlewidth+1mm,
top=0pt,
colback=white,
fonttitle=\bfseries\sffamily,
coltitle={cyan!60!black},
colbacktitle=white,
colframe=white,
title=\thetcbcounter,
overlay unbroken and first={%
\node[fill=gray!30, inner sep=1pt, minimum width=\tcboxedtitlewidth,
outer sep=0pt, text=cyan!60!black, anchor=north] at(title.south)
{\foreach \i in {1,...,#2}{\if#2gt1{\,}\fi$\bullet$}};},#1}

\begin{document}

\lipsum[1]

\begin{problem}{1}
\lipsum[2]
\end{problem}

\begin{problem}{4}
\lipsum[3]
\end{problem}

\begin{problem}{2}
\lipsum[4]
\end{problem}

\end{document}


How could we create solutions environment, so we have at the end of the book solution of the exercises where each one have reference to chapter, page, number of exercise with the corresponding level of difficulty?

## 2 Answers

I suggest to you to read exercise package documentation.

There are the package options:

lastexercise if no references is given for an answer, then the answer is supposed to refer to the last exercise (see section 2.1).

and

answerdelayed save the answers instead of typeset them. The answers can be included later in the document with the command \shipoutAnswer.

and the Exercise environment option:

difficulty The difficulty of the exercise (a number). It will be available later with the counter \ExerciseDifficulty.

You can start with this simple example and then make your customization. As you can see, if you write your Answer environment immediately after the corresponding Exercise, the answer is automatically joined to the previous exercise.

\documentclass[11pt]{book}
\usepackage[lastexercise,answerdelayed]{exercise}

\usepackage{lipsum}

\begin{document}

\chapter{Test 1}

\lipsum[1]

\section{Problems}

\begin{Exercise}[difficulty={1}]
My easy exercise.
\end{Exercise}
\begin{Answer}
My answer to my easy exercise.
\end{Answer}

\chapter{Test 2}

\lipsum[1]

\section{Problems}
\begin{Exercise}[difficulty={2}]
My difficult exercise.
\end{Exercise}
\begin{Answer}
My answer to my difficult exercise.
\end{Answer}

\chapter{Solutions}

\shipoutAnswer

\end{document}


• Thank you @CarLaTeX, I really appreciate it. I don't have enough skills to do some substantial customization, but your comment is very helpful. I didn't like graphic expression of difficulty in exercise package, so that is why I started searching for another solution. As you can see, I put estimation of difficulty not only in the text of problem, but also in the answer. It could be that it's not necessary, but I liked it. – Владимир Божовић Jun 1 '18 at 7:53
• @ВладимирБожовић Thank you, I added an answer just to explain that it is possible to link the answer with the previous question automatically. – CarLaTeX Jun 1 '18 at 7:58

This is MWE for problems - solutions environment, that is combining @Sebastiano code and @CarLaTeX code from Math book: how to write Exercise and Answers

\documentclass[11pt]{book}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\usepackage{multicol}
\usepackage{ifthen}
\usepackage{xcolor}
\colorlet{lightcyan}{cyan!40!white}
\usepackage{chngcntr}
\usepackage{stackengine}
\usepackage{tasks}

\newboolean{firstanswerofthechapter}

\colorlet{lightcyan}{cyan!40!white}

\newtcolorbox[auto counter]{problem}[2][]{%
breakable,
blankest,
enhanced,
attach boxed title to top left={yshift=-\tcboxedtitleheight},
left=\tcboxedtitlewidth+2mm,
top=0pt,
colback=white,
fonttitle=\bfseries\sffamily,
coltitle={cyan!60!black},
colbacktitle=white,
colframe=white,
title=\thetcbcounter,
overlay unbroken and first={%
\node[fill=gray!30, inner sep=1pt,
minimum width=\tcboxedtitlewidth,
outer sep=0pt,
anchor=north] at (title.south) {%
\foreach \i in {1,...,4}{%
\ifnum\i>#2%
\,\textcolor{cyan!60!black}{$\bullet$}%
\else
\,\textcolor{red}{$\bullet$}%
\fi}};},
#1}

\newtcolorbox{exsolution}[2]{%
breakable,
blankest,
enhanced,
attach boxed title to top left={yshift=-\tcboxedtitleheight},
left=\tcboxedtitlewidth+2mm,
top=0pt,
colback=white,
fonttitle=\bfseries\sffamily,
coltitle={cyan!60!black},
colbacktitle=white,
colframe=white,
title=#1,
overlay unbroken and first={%
\node[fill=gray!30, inner sep=1pt,
minimum width=\tcboxedtitlewidth,
outer sep=0pt,
anchor=north] at (title.south) {%
\foreach \i in {1,...,4}{%
\ifnum\i>#2%
\,\textcolor{cyan!60!black}{$\bullet$}%
\else
\,\textcolor{red}{$\bullet$}%
\fi}};},
overlay unbroken and first={%
\node[fill=gray!30, inner sep=1pt,
minimum width=\tcboxedtitlewidth,
outer sep=0pt,
anchor=north] at (title.south) {%
\foreach \i in {1,...,4}{%
\ifnum\i>#2%
\,\textcolor{cyan!60!black}{$\bullet$}%
\else
\,\textcolor{red}{$\bullet$}%
\fi}};}, }

\newlength{\longestlabel}
\settowidth{\longestlabel}{\bfseries viii.}
\settasks{counter-format={tsk[r].}, label-format={\bfseries}, label-width=\longestlabel,
item-indent=0pt, label-offset=2pt, column-sep={10pt}}

\usepackage[lastexercise,answerdelayed]{exercise}
\renewcommand{\ExerciseName}{EXERCISES}
\renewcommand{\ExerciseHeader}{}

\renewcommand{\AnswerName}{Problems}
\renewcommand{\AnswerHeader}{\ifthenelse{\boolean{firstanswerofthechapter}}%
{\bigskip\noindent\textcolor{cyan}{\textbf{CHAPTER \thechapter}}\newline\newline%
\noindent\bfseries\emph{\textcolor{cyan}{\AnswerName\ {\thesection}, page %
\pageref{\AnswerRef}}}\smallskip}
{\noindent\bfseries\emph{\textcolor{cyan}{\AnswerName\ {\thesection} page \pageref{\AnswerRef}}}\smallskip}}
\setlength{\QuestionIndent}{16pt}

\begin{document}

\chapter{Test 1}

\lipsum[1]

\section{Problems}

\begin{Exercise}\label{EX-1.1}
\begin{problem}{1}
\lipsum[2]
\end{problem}
\end{Exercise}

\chapter{Test 2}

\lipsum[1]

\section{Problems}
\begin{Exercise}\label{EX-2.1}

\begin{problem}{4}
\lipsum[3]
\end{problem}

\begin{problem}{2}
\lipsum[4]
\end{problem}
\end{Exercise}

\setboolean{firstanswerofthechapter}{true}

\begin{Answer}[ref={EX-2.1}]
\begin{exsolution}{3}{2}
\lipsum[2]
\end{exsolution}
\end{Answer}

\chapter{Solutions}

\shipoutAnswer

\end{document}


However, as one can see, this is pretty clumsy solution. In my opinion it would be the most practical to have unified environment for problem and corresponding solution immediately after. Something like

\begin{prob}
\lipsum[2]
\begin{solution}
.....
\end{solution}
\end{prob}


so, this solution (if exists) is sent automatically to chapter with solutions of exercises. Unfortunately, I don't have enough LaTeX experience and knowledge to do it.