# 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?

• Thank for the use of my question. You could change a part of this code: tex.stackexchange.com/questions/369265/…. Probabily is an optimal solution. I want upvoted your question for your commitment.+1 May 28, 2018 at 19:54
• Thanks @Sebastiano! I am trying to make MWE, so it could be improved. May 29, 2018 at 7:56

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{lipsum}

\begin{document}

\chapter{Test 1}

\lipsum[1]

\section{Problems}

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

\chapter{Test 2}

\lipsum[1]

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

\chapter{Solutions}

\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, 2018 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. Jun 1, 2018 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}

\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.}
item-indent=0pt, label-offset=2pt, column-sep={10pt}}

\renewcommand{\ExerciseName}{EXERCISES}

{\bigskip\noindent\textcolor{cyan}{\textbf{CHAPTER \thechapter}}\newline\newline%
\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}

\begin{exsolution}{3}{2}
\lipsum[2]
\end{exsolution}

\chapter{Solutions}

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