I'm writing a book of maths problems. I need a way to present both the problems and their solutions at the end of the book.

I worked out a way to get the problems and answers to look just as I would like them to, except for one thing.

I like to use tasks for some problems that have multiple questions. Tasks gives a nice way to work with horizontal lists.

But using them with the answers package gives strange results.

The problems look just the way I want them.

But the numbering and spacing goes wrong in the solutions when I use tasks, in problem 1.2.2.

My question is simply if anyone knows why this happens, knows way to fix the numbering or has a good substitute for tasks.

\documentclass[a4paper,12pt,twoside,openright]{book}

% -- For nice horizontal lists -- %

% -- For solutions -- %
\Newassociation{solution}{Sol}{solutions}
\renewenvironment{Sol}[1]{\begin{enumerate}\item[\bfseries #1.]}{\end{enumerate}}
\Newassociation{secnr}{SectionNumbering}{solutions}
\renewenvironment{SectionNumbering}[1]{\begin{trivlist}\item \bfseries #1}{\end{trivlist}}

\begin{document}

\Opensolutionfile{solutions}
\chapter{Mathematics}
\begin{secnr}Bls. \pageref{section_1A}\end{secnr}

\begin{enumerate}
\item What is $2+3$?
\begin{solution}
5
\end{solution}
\item Calculate the following:
\begin{enumerate}
\item $8+2$
\item $9+3$
\item $10+4$
\end{enumerate}
\begin{solution}
\begin{enumerate}
\item $10$
\item $12$
\item $14$
\end{enumerate}
\end{solution}
\end{enumerate}

\section{Subtraction}\label{section_1B}
\begin{secnr}Bls. \pageref{section_1B}\end{secnr}

\begin{enumerate}
\item What is $100 - 3$
\begin{solution}
97
\end{solution}
\item Calculate the following:
\task $10 - 3$
\task $10 - 4$
\task $20 - 4$
\task $20 - 5$
\begin{solution}
\task $7$
\task $6$
\task $16$
\task $15$
\end{solution}
\item How are you doing?
\begin{solution}
Fine.
\end{solution}
\end{enumerate}

\Closesolutionfile{solutions}

\chapter*{Solutions}
\input{solutions}

\end{document}

• I think both of these packages use \@currentlabel. So the last \task (in question not answer) has label d) which is then written to the answer. I don't know how to make them compatible but I assume that's not too easy. You probably need to create a new custom label to be used by one of those packages. Sorry I cannot help. – nox Jul 4 '18 at 17:42

A crude way of approaching the matter could be copying those macros that hold info that usually is actualized by \refstepcounter and that gets written to .aux-file by the \label-command, i.e., the macros \@currentlabel, \@currentHref (in case the hyperref package is in use) and \@currentlabelname (in case the hyperref package is in use), right before starting the tasks-environment, and having them restored after ending the tasks-environment, right before starting the solution-environment.

As the user nox pointed out, this can be automatized by using the commands \BeforeBeginEnvironment and \AfterEndEnvironment from the etoolbox package.

Besides this I recommend using the command \Readsolutionfile from the answers package for inputting the file with the solutions rather than using \input.

The former command also takes into account that one might wish to load the package answers with its option nosolutionfiles which means that no external solutions-file will be generated.

I also recommend placing the sectioning-command for the section-header of the solutions-section into the solution-file itself. This can be done using the environment Filesave from the answers package. This takes into account that such a section-header is not needed in case the package answers is loaded with the option nosolutionfiles which means that solutions are not written into an extra file and laterwards inserted as an extra section but each solution is placed right by the question.

With the following example you can choose whether either to generate an extra section for solutions or to have each solution printed near the question by turning into a comment/not turning into a comment the line \PassOptionsToPackage{nosolutionfiles}{answers}:

\documentclass[a4paper,12pt,twoside,openright]{book}

% -- For patching environments --

\usepackage{etoolbox}

% -- For nice horizontal lists --

% Unlike enumerate environment the tasks environment does not
% change info needed for placing labels etc locally but globally.
% Thus patch the tasks environment to save these things at the
% start of the environment and to reset these things at
% the end of the environment:
\makeatletter
\newcommand\savelabelinfo{%
\let\MyNicecurrentlabel=\@currentlabel
\let\MyNicecurrenthref=\@currentHref
\let\MyNicecurrentlabelname=\@currentlabelname
}%
\newcommand\restorelabelinfo{%
\global\let\@currentlabel=\MyNicecurrentlabel
\global\let\@currentHref=\MyNicecurrenthref
\global\let\@currentlabelname=\MyNicecurrentlabelname
}%
\makeatother

% -- For solutions --

% In case you wish solutions writtem directly after questions,
% enable the following line:

\Newassociation{solution}{Sol}{solutions}
\Newassociation{secnr}{SectionNumbering}{solutions}

\makeatletter
% \InCaseSolutionsViaFile{<Tokens to deliver in case option
%                          "nosolutionfiles" is not provided>}%
%                        {<Tokens to deliver in case option
%                          "nosolutionfiles" is provided>}%
\newcommand\InCaseSolutionsViaFile[2]{}%
\let\InCaseSolutionsViaFile=\@secondoftwo
}{%
\let\InCaseSolutionsViaFile=\@firstoftwo
}%
\InCaseSolutionsViaFile{%
\renewenvironment{Sol}[1]{\begin{enumerate}\item[\bfseries #1.]}{\end{enumerate}}%
\renewenvironment{SectionNumbering}[1]{\begin{trivlist}\item \bfseries #1}{\end{trivlist}}%
}{%
% Turn sol-environment into something that prints the body
% within something like a deflist environment with label "Solution(s):"
\renewenvironment{Sol}[1]{%
\begin{list}{}{%
\renewcommand\makelabel[1]{\textbf{##1}\hfil}%
\settowidth\labelwidth{\textbf{Solution(s):}}%
\setlength\leftmargin{\labelwidth}%
}%
\item[Solution(s):]%
}{\end{list}}%
% Turn secnr-environment into something like the verbatim-environment
% but without printing the body, thus gobbling its own content:
\renewenvironment{secnr}[1]{%
\@bsphack
\let\do=\@makeother
\dospecials
\obeylines
\secnrgobble
}{\@Esphack}%
}%
\newcommand\secnrgobble{}%
\begingroup
\catcode|=0 \catcode[= 1 \catcode]=2 %
\catcode\{=12 \catcode\}=12 \catcode\\=12 %
|@firstofone[%
|endgroup
|def|secnrgobble#1\end{secnr}[|end[secnr]]%
]%
\makeatother

% \usepackage{hyperref}

\begin{document}

\Opensolutionfile{solutions}
\begin{Filesave}{solutions}
\chapter*{Solutions}
\end{Filesave}
%
\chapter{Mathematics}
\begin{secnr}Bls. \pageref{section_1A}\end{secnr}

\begin{enumerate}
\item What is $2+3$?
\begin{solution}
5
\end{solution}
\item Calculate the following:
\begin{enumerate}
\item $8+2$
\item $9+3$
\item $10+4$
\end{enumerate}
\begin{solution}
\begin{enumerate}
\item $10$
\item $12$
\item $14$
\end{enumerate}
\end{solution}
\end{enumerate}

\section{Subtraction}\label{section_1B}
\begin{secnr}Bls. \pageref{section_1B}\end{secnr}

\begin{enumerate}
\item What is $100 - 3$
\begin{solution}
97
\end{solution}
\begin{samepage}
\item Calculate the following:
\task $10 - 3$
\task $10 - 4$
\task $20 - 4$
\task $20 - 5$
\end{samepage}
\begin{solution}
\task $7$
\task $6$
\task $16$
\task $15$
\end{solution}
\item How are you doing?
\begin{solution}
Fine.
\end{solution}
\end{enumerate}

\Closesolutionfile{solutions}


In case the line \PassOptionsToPackage{nosolutionfiles}{answers} is turned into a comment, you get:
In case the line \PassOptionsToPackage{nosolutionfiles}{answers} is not turned into a comment, you get:
• Nice. By the way you can patch the tasks environment in order to not have to type \savelabelinfo and \restorelabelinfo around it by hand: \usepackage{etoolbox} \BeforeBeginEnvironment{tasks}{\savelabelinfo} \AfterEndEnvironment{tasks}{\restorelabelinfo} – nox Jul 4 '18 at 21:22