2

I'm using the exercise package to write exercises and their corresponding answers. I have hundreds of exercises, each in their own file. The exercises are to be included two separate documents. In the first, each exercise should appear without its answer, and in the second, they should appear with their answer. To do this, I'm using the package option noanswer in the first document. The problem is that in this first document, any references defined in an answer are reported as undefined even when the only reference to them is also in the answer.

For example:

\documentclass{article}

\usepackage{mwe}
\usepackage{float}
\usepackage[noanswer]{exercise}

\begin{document}
    \begin{Exercise}
        \label{exe:some_exercise}
        \blindtext
    \end{Exercise}
    \begin{Answer}[ref={exe:some_exercise}]
        \begin{figure}[H]
            \centering
            \includegraphics[width=.48\linewidth]{example-image-a}
            \caption{MWE figure}
            \label{fig:some_figure}
        \end{figure}
        \blindtext as shown in Figure~\ref{fig:some_figure}.
    \end{Answer}
\end{document}  

reports:

LaTeX Warning: Reference `fig:some_figure' on page 1 undefined on input line 20

I've tried other package options like answerdelayed and then never calling \shipoutAnswer, but that has the same problem. How can I suppress the undefined reference warnings caused by the exercise answers which are not being included, but still see warnings for undefined references in the parts of the document that are being typeset?

EDIT:

I found this similar question with an answer but I do not know how to apply its answer to the exercise package.

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

1

Try to add following two lines after loading of exercise.sty, if you are setting [noanswer]:

\expandafter\let\expandafter\InitAnswer\csname @InitAnswer\endcsname
\expandafter\def\csname @InitAnswer\endcsname{\def\ref##1{?}\InitAnswer}

The reason of your problem is that the Answer text is normally processed in \vbox and the result is not used if [noanswer] option is set. It means that \ref needs to know the label, but the label is not written to the aux file, because \write aux{... label} is a part of the printed material in the \vbox which is unused. The asynchronous \write primitive plays its role here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.