[Tex/LaTex] Math book: Exercise – Solution environment

environmentsexercises

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?

Best Answer

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}

enter image description here enter image description here enter image description here