# Automatically highlight undefined references

Is there a way to force LaTeX to automatically change the formatting of the ?? that gets typeset in place of an undefined reference? For example, can one make LaTeX draw a yellow "highlight" background behind the ??, or render the ?? in red text?

I would like it to be easier to spot these undefined references when skim reading PDF output.

An MWE would be

\documentclass{article}
\begin{document}
\ref{undefinedReference}
\end{document}


It would be especially great if this could be made to work for undefined bibliographic citations as well as undefined cross refs.

• Missing references are noted in the log file and produce a warning. A good start is to keep on eye on what LaTeX tells you there. – Andrew Swann May 9 '14 at 14:26

I don't think you will miss this:

\documentclass{article}
\usepackage{color}
\makeatletter

\def\@setref#1#2#3{%
\ifx#1\relax
\protect\G@refundefinedtrue
\nfss@text{\reset@font\bfseries\huge\textcolor{red}{DEFINE #3 NOW, AND MAKE IT QUICK!}}%
\@latex@warning{Reference #3' on page \thepage \space
undefined}%
\else
\expandafter#2#1\null
\fi}

\makeatother
\begin{document}
\ref{undefinedReference}
\end{document}

• A macro that makes the machine punch you in the eye would be missed more frequently than this one. – egreg May 9 '14 at 15:15
• Thanks David Carlisle. I used \nfss@text{\reset@font\colorbox{yellow}{??$_\text{#3}$}} and put the whole thing into a .sty file to make a package that I can invoke to achieve this functionality on all my manuscripts. – Ubiquitous May 9 '14 at 15:46
• @Ubiquitous I'd prefer the version that punched egreg in the eye, but OK:-) – David Carlisle May 9 '14 at 16:04
• How can I adapt this answer for missing citations? – Hotschke Jul 27 '15 at 9:38
• @Hotschke in principle just copy the definition of \@citex from latex.ltx and change \hbox{\reset@font\bfseries ?} to whatever you want it to be. However more so that \ref many citation packages redefine \@citex or redefine \cite to use some other variant command, so it depends a bit what your current definition is. In any case though the definition will at some point be using a macro that is typesetting a literal ? and you can simply define it to typeset something else. – David Carlisle Jul 27 '15 at 10:59

Based on David's answer, here's a solution with PDF annotations as 'yellow "highlight" background':

\documentclass{article}
\usepackage[rgb]{xcolor}
\usepackage{pdfcomment}
\makeatletter

\def\@setref#1#2#3{%
\ifx#1\relax
\protect\G@refundefinedtrue
\nfss@text{\reset@font\bfseries\pdfmarkupcomment[color=yellow,author={RefCheck Assistant}]{??}{DEFINE #3 NOW, AND MAKE IT QUICK!}}%
\@latex@warning{Reference #3' on page \thepage \space
undefined}%
\else
\expandafter#2#1\null
\fi}

\makeatother
\begin{document}
see \ref{undefinedReference}
\end{document}


without clicking and mouseover you get a tooltip: