[Tex/LaTex] Labeling problems with exam class usage when using multiple chapters

answerscountersexamlabels

Using the exam document class I'm trying to create a questions/answers-sheet for a book. When working on the first chapter, everything worked nicely. When I came to the second chapter, I realized I would have problems with the question numbering, since there are questions with the same numbers. The specific warnings I got was:

LaTeX Warning: Label `question@1' multiply defined.

The basic layout of the body of my document is as follows:

\section*{Chapter 1}
\setcounter{section}{1}
    \begin{questions}
        \numberwithin{question}{section}
        \setcounter{question}{0}
        \question

            \begin{solution}

            \end{solution}
    \end{questions}

\section*{Chapter 2}
\setcounter{section}{2}
    \begin{questions}
        \numberwithin{question}{section}
        \setcounter{question}{0}
        \question

            \begin{solution}

            \end{solution}
    \end{questions}

Compilation still works, but my editor is giving me a page full of warnings. There has to be a better more correct way, than what I'm doing right now. Could you recommend me a different way to make such a document?

Best Answer

Looking at the source exam class would need quite a few changes to use repeated numbers as it uses the number counter internally in several places. However to help anyone else answering and to address other problems in the posted document here is a start at a solution.

You shouldn't use star form for numbered sections, and \numberwith should only be used once. the fact that it wasn't having an effect and you had to manually reset the question counter is related to your use of the * form. Nested counters reset when the parent counter is incremented with \stepcounter (or better \refstepcounter) setting the section counter via \setcounter does not do this (and also means you can not use \label and \ref to refer to the section.

update Perhaps the easiest way to restart the equation numbering without breaking the rest of the features is to maintain the counter with unique numbers eg 101, 1002, in the first section and 201, 202 in the second, and then just arrange that this gets printed as 1.1,1.2,2.1,2.1 in the heading:

enter image description here

\documentclass{exam}
\usepackage{amsmath}

\makeatletter

\def\@seccntformat#1{%
  \expandafter\ifx\csname the#1\endcsname\thesection Chapter~\fi
  \csname the#1\endcsname
  \setcounter{question}{\thesection00}%
   \quad}

\renewcommand\thequestion{%
\the\numexpr(\value{question}-49)/100\relax.%
\the\numexpr\value{question}-(100*((\value{question}-49)/100))\relax}

\makeatother

\begin{document}

    \begin{questions}
\fullwidth{\section{}}
        \question aaa

            \begin{solution}
xxx
            \end{solution}


\fullwidth{\section{}}

        \question bbb

            \begin{solution}
yyy
            \end{solution}
    \end{questions}

\end{document}