# 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 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 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 at 15:46
@Ubiquitous I'd prefer the version that punched egreg in the eye, but OK:-) –  David Carlisle May 9 at 16:04

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:

-