![为什么在解决方案中定义和使用标签(并且未打印答案)时,我们会在考试课中收到未定义的引用警告?](https://linux22.com/image/302080/%E4%B8%BA%E4%BB%80%E4%B9%88%E5%9C%A8%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%E4%B8%AD%E5%AE%9A%E4%B9%89%E5%92%8C%E4%BD%BF%E7%94%A8%E6%A0%87%E7%AD%BE%EF%BC%88%E5%B9%B6%E4%B8%94%E6%9C%AA%E6%89%93%E5%8D%B0%E7%AD%94%E6%A1%88%EF%BC%89%E6%97%B6%EF%BC%8C%E6%88%91%E4%BB%AC%E4%BC%9A%E5%9C%A8%E8%80%83%E8%AF%95%E8%AF%BE%E4%B8%AD%E6%94%B6%E5%88%B0%E6%9C%AA%E5%AE%9A%E4%B9%89%E7%9A%84%E5%BC%95%E7%94%A8%E8%AD%A6%E5%91%8A%EF%BC%9F.png)
让我们考虑一下使用考試班。
\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}