13

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.

1
  • 6
    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. May 9, 2014 at 14:26

2 Answers 2

14

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}
7
  • 2
    A macro that makes the machine punch you in the eye would be missed more frequently than this one.
    – egreg
    May 9, 2014 at 15:15
  • 1
    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, 2014 at 15:46
  • 2
    @Ubiquitous I'd prefer the version that punched egreg in the eye, but OK:-) May 9, 2014 at 16:04
  • How can I adapt this answer for missing citations?
    – Hotschke
    Jul 27, 2015 at 9:38
  • 1
    @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. Jul 27, 2015 at 10:59
8

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}

enter image description here

without clicking and mouseover you get a tooltip:

enter image description 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.