# Equation numbering and solutions exam class

I use the exam class for exams. When I use equations in the solution environment, the counter for equation numbering is updated, even when the solutions are not printed. Is it possible not to consider the counter inside the solution environment when solutions are not printed?

Below is a MWE. It produces a wrong equation numbering. The numbering is correct for the case in which the solutions are printed.

%\documentclass[11pt,answers]{exam}
\documentclass[11pt]{exam}
\usepackage{parskip}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{lmodern}
\usepackage{microtype}

\usepackage{mathtools}
\usepackage{amsmath}

\usepackage{hyperref}

\begin{document}

\centerline{\bf Exam Class: Numbering Equations, MWE}

\begin{questions}

\begin{parts}

\part Let $a=3$ and $b=4$. Consider the equation

\begin{equation}
a^{2} + b^{2} = c^{2},
\end{equation}
where all values are positive. Calculate the value of $c$.

\begin{solution}
We have, given that $c>0$,
\begin{equation}
c^{2} = 3^{2} + 4^{2} \Rightarrow c = 5.
\end{equation}
\end{solution}

\part Let $a=5$. The variable $b$ is given by
\begin{equation}
b = a^{2}.
\end{equation}
Calculate the value of $b$.

\begin{solution}
We have
\begin{equation}
b = 5^{2} \Rightarrow b = 25.
\end{equation}
\end{solution}

\end{parts}

\end{questions}

\end{document} A quick fix would to use \begin{equation*} and \end{equation*} or the normal equation environment based on the answers option. So we define a new environment equationsolution:

\newenvironment{equationsolution}{%
}{%
}


MWE:

%\documentclass[11pt,answers]{exam}
\documentclass[11pt]{exam}
\usepackage{parskip}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{lmodern}
\usepackage{microtype}

\usepackage{mathtools}
\usepackage{amsmath}

\usepackage{hyperref}

\newenvironment{equationsolution}{%
}{%
}

\begin{document}

\centerline{\bf Exam Class: Numbering Equations, MWE}

\begin{questions}

\begin{parts}

\part Let $a=3$ and $b=4$. Consider the equation

\begin{equation}
a^{2} + b^{2} = c^{2},
\end{equation}
where all values are positive. Calculate the value of $c$.

\begin{solution}
We have, given that $c>0$,
\begin{equationsolution}
c^{2} = 3^{2} + 4^{2} \Rightarrow c = 5.
\end{equationsolution}
\end{solution}

\part Let $a=5$. The variable $b$ is given by
\begin{equation}
b = a^{2}.
\end{equation}
Calculate the value of $b$.

\begin{solution}
We have
\begin{equationsolution}
b = 5^{2} \Rightarrow b = 25.
\end{equationsolution}
\end{solution}

\end{parts}

\end{questions}

\begin{equation}
c = \sqrt{a^2+b^2}
\end{equation}
\end{document}


Mind you that there will be inconsistencies between the exam the student sees and the exam with the solutions. That's why I always put the solutions after the exam questions.

• This is interesting, but it makes the equation environment too cumbersome. Is it possible to redefine the equation environment before \begin{document} to obtain this behavior? – Andre Oct 18 '18 at 14:05
• I've updated the answer by using a new environment equationsolution. – Jesse op den Brouw Oct 18 '18 at 14:23
• Redefining the equation environment is not really an option because it has to know if it is in an solution environment or not. – Jesse op den Brouw Oct 18 '18 at 14:43
• Thank you. Your solution is good, but when I tried to implement it I found that it will require redefinition of environments related to equations, such as align. A solution that involves the counter (\addtocounter, \setcounteretc) might be easier to implement. Ideally, the counter had not to work inside the solution environment if the answers are not being printed. – Andre Oct 19 '18 at 13:29
• Thank you for your solution. I now added a solution that involves the use of a new counter. The advantage is that is avoids the creation of new environments for equation, align, and other environments that change the equation counter. In the end, I believe that this solution is easier to implement. Best. – Andre Oct 24 '18 at 10:36

I found a solution that uses a new counter and ifprintanswers. The new counter stores the value of the equation counter, and it is used later if the answers are printed. The solution of the counter is based on the solution to this question.

Here is the new MWE:

%\documentclass[11pt,answers]{exam}
\documentclass[11pt]{exam}
\usepackage{parskip}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{lmodern}
\usepackage{microtype}

\usepackage{mathtools}
\usepackage{amsmath}

\usepackage{hyperref}

\newcounter{eqBeforeSolution} % New counter

\begin{document}

\centerline{\bf Exam Class: Numbering Equations, MWE}

\begin{questions}

\begin{parts}

\part Let $a=3$ and $b=4$. Consider the equation

\begin{equation}
a^{2} + b^{2} = c^{2},
\end{equation}
where all values are positive. Calculate the value of $c$.

\setcounter{eqBeforeSolution}{\value{equation}} % Store current equation counter value
\begin{solution}
We have, given that $c>0$,
\begin{equation}\label{eq10}
c^{2} = 3^{2} + 4^{2} \Rightarrow c = 5.
\end{equation}
Equation \eqref{eq10} shows the solution.
\end{solution}

\else
% Uses the value of the new counter to reset the equation counter
\setcounter{equation}{\value{eqBeforeSolution}}
\fi

\part Let $a=5$. The variable $b$ is given by
\begin{equation}
b = a^{2}.
\end{equation}
Calculate the value of $b$.

\begin{solution}
We have
\begin{equation}
b = 5^{2} \Rightarrow b = 25.
\end{equation}
\end{solution}

\end{parts}

\end{questions}

\end{document} 