让我们考虑一下使用考試班。
\documentclass{exam}
%\printanswers
\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
the answer either.
\end{enumerate}
\end{solution}
\end{questions}
\end{document}
标签正在定义,用过的在解决方案中。但是,当答案未被打印时,它会生成未定义的引用警告。
LaTeX Warning: Reference `enu:l' on page 1 undefined on input line 12. ............... ............... LaTeX Warning: There were undefined references.
但常识告诉我们,由于标签是在解决方案内部定义和使用的,因此我们不应该根据解决方案是否被打印而收到警告消息。
或者,如果正在生成解决方案的幻像输出以留出空间,我们不应该收到警告。它正在被处理,不是吗?
那为什么我们仍然会收到警告?
可能是 bug?还是某个功能?(也许不是。)
答案1
重新定义solution
以获取其内容,而不是将其排版在丢弃的框中。
\documentclass{exam}
\usepackage{environ}
\makeatletter
\RenewEnviron{solution}[1][0pt]{%
\ifprintanswers
\Solution@Emphasis\begin{TheSolution}\BODY\end{TheSolution}
\else
\ifcancelspace
\else
\par\penalty\z@\vspace*{#1}%
\fi
\fi
}
\makeatother
\printanswers
\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
the answer either.
\end{enumerate}
\end{solution}
\end{questions}
\end{document}