# Problem using answers and tasks packages

I am trying to create questions (using answers package) with several subquestions using the tasks package. Each question has a solution wich is to be typeset at the end of the document using functionality of the answers package.

My problem is that the solutions are not numbered properly.

\documentclass{article}

\Newassociation{solution}{Solution}{ans}
\newtheorem{exercise}{Exercise}[section]

\begin{document}
\Opensolutionfile{ans}[ans2]

\section{Exercises}

\begin{exercise}\label{xrc:one}
\begin{solution}
\end{solution}
\end{exercise}

\begin{exercise}\label{xrc:two}
Statement of the second exercise

\begin{tasks}[counter-format= {tsk[a].) }, label-offset = {2em}, label-format ={\bfseries},label-width={1.5em}](3)

\begin{solution}
Solutions to the second exercise

\end{solution}
\end{exercise}

\begin{exercise}\label{xrc:two}
Statement of the third exercise

\begin{tasks}[counter-format= {tsk[a].) }, label-offset = {2em}, label-format ={\bfseries},label-width={1.5em}](3)

\begin{solution}
Solutions to the third exercise

\end{solution}
\end{exercise}

\Closesolutionfile{ans}

\section*{Solutions}\label{svar}

\input{ans2}

\end{document}

• This is not a problem related to tasks: any \refstepcounter{...} in the same place will cause the same problem… – clemens Aug 30 '16 at 16:28
• Welcome to TeX.sx, by the way :) – clemens Aug 30 '16 at 16:28
• try to add  \addtocounter{exercise}{-1}\refstepcounter{exercise} just before each \begin{solution} – touhami Aug 30 '16 at 17:27

This is not a problem directly related to the tasks package – any \refstepcounter{…} in the exercise before the solution environment causes the same problem:

\documentclass{article}

\Newassociation{solution}{Solution}{ans}
\newtheorem{exercise}{Exercise}[section]

\newcounter{test}
\renewcommand\thetest{test:\arabic{test}}

\begin{document}
\Opensolutionfile{ans}[ans2]

\section{Exercises}

\begin{exercise}\label{xrc:one}
Exercise without \verb+\refstepcounter+
\begin{solution}
Solution without \verb+\refstepcounter+
\end{solution}
\end{exercise}

\begin{exercise}\label{xrc:two}
\refstepcounter{test}
Statement of the second exercise
\begin{solution}
Solutions to the second exercise
\end{solution}
\end{exercise}

\begin{exercise}\label{xrc:three}
Statement of the third exercise
\refstepcounter{test}
\begin{solution}
Solutions to the third exercise
\end{solution}
\end{exercise}

\Closesolutionfile{ans}

\section*{Solutions}\label{svar}

\input{ans2}

\end{document}


You can “repair” this by setting \@currentlabel to the exercise number again using

\addtocounter{exercise}{-1}\refstepcounter{exercise}


directly before the solution environment. With the etoolbox package and its \AtBeginEnvironment you can even repair this without having to add the code before each and every solution. Add

\usepackage{etoolbox}

to your preamble (but after \Newassociation{solution}{Solution}{ans} of course).