# Problem solutions at end of chapter

I'm writing a document with multiple chapters. Each chapter has problems and each problem has a solution. I want some of the solutions to be right after the problem and the others to appear at the end of the chapter.

Currently, I'm using

\newtheorem{problem}{Задача}[chapter]
\newtheorem{solution}{Решение}[chapter]


When a solution should appear at the end of the chapter I do:

\appto\SolutionsChapterFive{
\begin{solution}
\end{solution}
}


and at the end of the chapter, I just call the command with

\SolutionsChapterFive


Unfortunately, that messes up the numbering of the problems.

For example:

Chapter 5

Problem 5.1. Blah blah

Solution 5.1. Ablh ablh

Problem 5.2. Balh balh

Problem 5.3. Albh albh

Solution 5.3. Ablh ablh (But that appears as solution 5.2 ☹️ )

Problem 5.4. Habl Habl

Solutions

Solution 5.2. Ablh ablh (But that appears as solution 5.3 ☹️ )

Solution 5.4. Ablh ablh

How can I fix that? Is there a better way of approaching the problem?

• It is easier to tackle your problem if you provide a minimal working example (MWE) that contains just enough code to reproduce the (nominally) faulty behaviour. Plase take the time to complete your question by a MWE. – Bubaya Feb 24 at 17:02

Correctly carrying over the solution number with \appto (or some other macro) seems tricky due to how expansion works in LaTeX. Perhaps someone is knowledgeable with the etoolbox package will be able to provide a solution with the \appto approach. For now I would suggest a simple solution:

When you come to solutions which should appear at the end, save the current solution number in a new counter and then step the solution counter (so following solutions are numbered correctly). Then, before writing the solutions at the end of the chapter, set the solution counter to this saved value.

You loose the convenience of the \appto approach, but provided you do not have a large amount of end-of-chapter solutions I see the workflow as entirely practical.

A minimal example:

\documentclass{book}
\usepackage{amsmath}
\usepackage{amsthm}
\newtheorem{problem}{Problems}[chapter]
\newtheorem{solution}{Solutions}[chapter]
\begin{document}
\setcounter{chapter}{5}
\begin{problem}
Problems 1
\end{problem}
\begin{solution}
Solutions 1
\end{solution}
\begin{problem}
Problems 2
\end{problem}
\newcounter{fivea} % New counter to save solution number (fivea' must be unique)
\setcounter{fivea}{\value{solution}} % Sote current solution number
\stepcounter{solution} % Step the counter, as if solutions occured here
\begin{problem}
Problems 3
\end{problem}
\begin{solution}
Solutions 3
\end{solution}
\begin{solution}
Solutions 4
\end{solution}
% End of chapter material
\setcounter{solution}{\value{fivea}} % Restore the saved value
\begin{solution} % Write solutions in the usual way
Solutions 2
\end{solution}
\end{document}


Output:

Of course, you can easily wrap these steps in a macro and new environment to make things a bit slicker:

\documentclass{book}
\usepackage{amsmath}
\usepackage{amsthm}
\newtheorem{problem}{Problems}[chapter]
\newtheorem{solution}{Solutions}[chapter]
\newcommand\saveSolutionNumber[1]{\newcounter{#1}\setcounter{#1}{\value{solution}}\stepcounter{solution}}
\newenvironment{chapterSolutions}[1]{\setcounter{solution}{\value{#1}}\begin{solution}}{\end{solution}}
\begin{document}
\setcounter{chapter}{5}
\begin{problem}
Problems 1
\end{problem}
\begin{solution}
Solutions 1
\end{solution}
\begin{problem}
Problems 2
\end{problem}
\saveSolutionNumber{fivea} % fivea is the new counter
\begin{problem}
Problems 3
\end{problem}
\begin{solution}
Solutions 3
\end{solution}
\begin{solution}
Solutions 4
\end{solution}
\begin{chapterSolutions}{fivea} % Solutions based on counter fivea
Solutions 2
\end{chapterSolutions}
\end{document}
`

with exactly the same output as before.