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:



            \caption{MWE figure}
        \blindtext as shown in Figure~\ref{fig:some_figure}.


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?


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)?

  • I hoped that this workaround would work as well when using the option [answerdelayed], but that doesn't seem to be the case... Any suggestion on how the above can be reformulated for that setting?
    – Jan
    Aug 3, 2021 at 15:45

1 Answer 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.

  • Is it possible to condition this code depending on the value of the noanswer option? This would allow to enable / disable the answers without modifying these two lines of code. Aug 18, 2022 at 15:06

You must log in to answer this question.

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