3

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}
\usepackage{answers}
\usepackage{tasks}

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

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

\section{Exercises}

\begin{exercise}\label{xrc:one}
Exercise without tasks
\begin{solution}
Solution without tasks
\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)
\task First task
\task Second task
\task Third task
\end{tasks}

\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)
\task First task
\task Second task
\task Third task
\task Fourth task
\task Fifth task
\task Sixth task
\end{tasks}

\begin{solution}
Solutions to the third exercise


\end{solution}
\end{exercise}

\Closesolutionfile{ans}

\section*{Solutions}\label{svar}

\input{ans2}

\end{document}
  • 1
    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
3

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}
\usepackage{answers}

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

enter image description here

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}
\AtBeginEnvironment{solution}{\addtocounter{exercise}{-1}\refstepcounter{exercise}}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.