# Why do we get undefined references warning in exam class when labels are defined and used in solutions (and answers are not printed)?

Let us consider this code which uses the exam class.

\documentclass{exam}

\begin{document}

\begin{questions}
\question Why do we get undefined references warning in this case?
\begin{solution}
\begin{enumerate}
\item\label{enu:l}This will not give the answer.
\item And neither this one. This one~(\ref{enu:l}) did not give
\end{enumerate}
\end{solution}
\end{questions}

\end{document}


The label is being defined and used inside the solution. However, it generates an undefined references warning when the answers are not being printed.

LaTeX Warning: Reference enu:l' on page 1 undefined on input line 12.
...............
...............
LaTeX Warning: There were undefined references.


But common sense dictates that since the label is being defined and used inside the solution, we should not get the warning message depending on whether the solution is being printed or not.

Or if a phantom output of the solution is being generated to leave space for it, we should not get the warning. It is being processed, is not it?

Then why do we still get the warning?

A bug perhaps? Or a feature? (Perhaps not.)

• When you don't print answers, the labels in the solution are not written in the .aux file, but the solution text in main file is read. Hence the warning. Check the .aux file with and without \printanswers line. Is it a bug? I am not sure. – user11232 Nov 24 '14 at 3:07
• @HarishKumar Is not some phantom output being generated in the output to leave the space for answer? And if it is being read (and processed, hence the warning), what is wrong with creating the references? – Masroor Nov 24 '14 at 3:17
• From the class file: If printanswers is true, we print the solution using a TheSolution environment. If printanswers is false and cancelspace is false, we insert lined vertical space equal to the optional argument (the default value of which is 0pt). – Johannes_B Nov 24 '14 at 7:57
• Or look at the output of your example. Referring to something non-existing? It's a bit odd. – Johannes_B Nov 24 '14 at 7:57
• @Johannes_B I do not get your point. From the package documentation, "... solution, solutionorbox, solutionorlines, solutionordottedlines... Each of these can take one optional argument, which is an amount of space to be left for the students to write their answers when solutions are not being printed (see section 8.4). If you use these environments without any optional arguments, then they are identical: They print either the solution or nothing at all." The issue I am trying to convey is, when the text is not being printed at all, why complain about references totally contained inside it? – Masroor Nov 24 '14 at 8:32

Redefine solution to grab its contents, instead of typesetting it in a box that's discarded.

\documentclass{exam}
\usepackage{environ}

\makeatletter
\RenewEnviron{solution}[1][0pt]{%
\Solution@Emphasis\begin{TheSolution}\BODY\end{TheSolution}
\else
\ifcancelspace
\else
\par\penalty\z@\vspace*{#1}%
\fi
\fi
}
\makeatother

\begin{document}

\begin{questions}
\question Why do we get undefined references warning in this case?
\begin{solution}
\begin{enumerate}
\item\label{enu:l}This will not give the answer.
\item And neither this one. This one~(\ref{enu:l}) did not give

• \RenewEnviron or \renewenvironment? – Masroor Nov 24 '14 at 16:49
• @Masroor \RenewEnviron, because we want it to become an environment grabbing its contents. – egreg Nov 24 '14 at 16:51
• But \RenewEnviron does not work in my machine, but \renewenvironment does. Are we talking about different version of LaTeX? – Masroor Nov 24 '14 at 16:58
• Also, it does complain about \BODY when \printanswers is on. – Masroor Nov 24 '14 at 17:01
• @Masroor You probably have an outdated version of environ. Please, upgrade your TeX distribution. You can't use \renewenvironment` for this job. – egreg Nov 24 '14 at 17:03