2

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}

\question Answer the following 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}

enter image description here

2 Answers 2

2

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}{%
\ifprintanswers\begin{equation}\else\begin{equation*}\fi
}{%
\ifprintanswers\end{equation}\else\end{equation*}\fi
}

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}{%
\ifprintanswers\begin{equation}\else\begin{equation*}\fi
}{%
\ifprintanswers\end{equation}\else\end{equation*}\fi
}

\begin{document}

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

\begin{questions}

\question Answer the following 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.

5
  • 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, 2018 at 14:05
  • 1
    I've updated the answer by using a new environment equationsolution. Oct 18, 2018 at 14:23
  • 1
    Redefining the equation environment is not really an option because it has to know if it is in an solution environment or not. Oct 18, 2018 at 14:43
  • 1
    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, 2018 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, 2018 at 10:36
1

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}

\question Answer the following 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}

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

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .