Is there a way to make \ref and \pageref fail instead of printing ?? if the .aux file doesn't have any page number for the requested label?

  • Are you using hyperref or any other cross-referencing package?
    – Werner
    Dec 12, 2017 at 1:18
  • 1
    ...note that this will be a useless feature, since the introduction of any new (previously non-existent) \label-and-\ref will fail; you need at least one compilation in order for the \label to have a valid reference/page number.
    – Werner
    Dec 12, 2017 at 1:24
  • @Werner I am aware of this, but it will not be a useless feature because I will only enable it on the second run in my build process. Dec 12, 2017 at 2:45

1 Answer 1


The macro responsible for printing ?? a warning when you \reference a non-existent \label is \@setref:

   \nfss@text{\reset@font\bfseries ??}%
   \@latex@warning{Reference `#3' on page \thepage \space

You can just change the use of \@latex@warning to \@latex@error (accommodating for \@latex@error needing two arguments rather than \@latex@warning's one):





\section{A section}
See section~\ref{sec:section1}.% Succeeds ...
                               % ... IF you have already compiled with \label{sec:section1}
See section~\ref{sec:section2}.% Fails


The use of this feature seems useless... if we assume the use-case is that you have an existing document that has some undefined \references that you want to identify, you'll "activate" this redefinition and compile. Now, somewhere mid-document your compilation fails (assuming again you are not running LaTeX in nonstopmode) and compilation stops. All references following this fail will be undefined in subsequent compilations (since their \labels haven't been written to the .aux), which will again fail. So you'll have to constantly active/de-active this "feature" and perform multiple compilations after every failure.

A better option would be to process your .log and identify the undefined \references using a regular text search.

  • I am building with the Nix package manager. All builds will be from a fresh state; there will be no "subsequent compilations" Dec 12, 2017 at 2:47
  • Then I'm not sure how you expect this to work in light of Werner's next to last paragraph. A fresh state without an aux file will most likely come across missing references.
    – Teepeemm
    Dec 12, 2017 at 3:02
  • from a fresh state, two things happen: first I generate the aux files, then I run with ref failures enabled Dec 12, 2017 at 3:08
  • I don't find it useless to have some command \finalcompilation which turns some warnings which shouldn't occur in the last compilation into errors. Dec 12, 2017 at 14:40

You must log in to answer this question.

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