GeneralChange "?" and "??" text for missing citations and refernces

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
mknap
Posts: 2
Joined: Thu Jun 16, 2011 8:06 pm

Change "?" and "??" text for missing citations and refernces

Post by mknap »

Question:
Is there a way to change LaTeX's printing of "?" and "??" whenever there are missing citations or references.

Motivation:
Too many times I have compiled a document or presentation without checking my log files, and it was only later when I noticed the missing reference or citation. Imagine noticing the missing reference while you are presenting! Of course, that has never happened to me. :)

Ideal Solution:
So, what I would like to do is make the "?" or "??" really pop out of the page so that upon quick glance, one can see a missing reference or citation. For example, if I could simple change the "?" to "MISSING CITATION" and change the "??" to "MISSING REFERENCE", that would be great. Ideally, though, I would like to give it some color if I use the color package so that "?" would become

Code: Select all

\colorbox{red}{?citation?}
and "??" would become

Code: Select all

\colorbox{red}{?reference?}
MWE
Of course, I don't know if a MWE is needed for such a question, but here is one:

Code: Select all

\documentclass{article}

\begin{document}

This is a missing reference: \ref{somefig}.

This is a mssing citation: \cite{somebib}.

\end{document}

Thanks all !

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Change "?" and "??" text for missing citations and refernces

Post by Stefan Kottwitz »

Good that you post a MWE, it's useful in any case. Here's a redefinition of involved macros as desired, also as MWE:

Code: Select all

\documentclass{article}
\usepackage{xcolor}
\newcommand*{\missingreference}{\colorbox{red}{?reference?}}
\newcommand*{\missingcitation}{\colorbox{red}{?citation?}}
\makeatletter
\def\@setref#1#2#3{%
  \ifx#1\relax
   \protect\G@refundefinedtrue
   \nfss@text{\reset@font\missingreference}%
   \@latex@warning{Reference `#3' on page \thepage \space
             undefined}%
  \else
   \expandafter#2#1\null
  \fi}
\def\@citex[#1]#2{\leavevmode
  \let\@citea\@empty
  \@cite{\@for\@citeb:=#2\do
    {\@citea\def\@citea{,\penalty\@m\ }%
     \edef\@citeb{\expandafter\@firstofone\@citeb\@empty}%
     \if@filesw\immediate\write\@auxout{\string\citation{\@citeb}}\fi
     \@ifundefined{b@\@citeb}{\hbox{\reset@font\missingcitation}%
       \G@refundefinedtrue
       \@latex@warning
         {Citation `\@citeb' on page \thepage \space undefined}}%
       {\@cite@ofmt{\csname b@\@citeb\endcsname}}}}{#1}}
\makeatother
\begin{document}

This is a missing reference: \ref{somefig}.

This is a mssing citation: \cite{somebib}.

\end{document}
The original macros contain \bfseries ?? instead.

The macros could also be patched, which makes the solution much shorter, using etoolbox.

Stefan
LaTeX.org admin
mknap
Posts: 2
Joined: Thu Jun 16, 2011 8:06 pm

Change "?" and "??" text for missing citations and refernces

Post by mknap »

Amazing !

Thank you, thank you, thank you.

Very nice work ! I am surprised that, in my searching, I never found anyone else doing something like this.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Change "?" and "??" text for missing citations and refernces

Post by Stefan Kottwitz »

Here's the etoolbox patch - instead of these two long redefinitions you could load the etoolbox packakage and patch those two commands:

Code: Select all

\patchcmd{\@setref}{\bfseries ??}{\colorbox{red}{?reference?}}{}{}
\patchcmd{\@citex}{\bfseries ?}{\colorbox{red}{?citation?}}{}{}
Stefan
LaTeX.org admin
Post Reply