17

Possible Duplicate:
How can I get LaTeX to warn about unreferenced figures?

In my document I have several figures, most of which I created references for (using \ref{}) immediately after I placed them into the document. However, I had a few figures that I forgot to reference in the document, leaving some figures "hanging" without any citation, which is a mistake on my part and must be corrected.

So, my questions is:

Is there a way to force latex to issue a warning if a figure is never referenced?

nickb
  • 393

2 Answers2

17

The refcheck package might be useful here.

The following MWE has a figure that is not referenced, so the refcheck puts a warning in your pdf with ? surrounding it.

enter image description here

\documentclass{article}
\usepackage{refcheck}

\begin{document}

\begin{figure}
 \centering
 \rule{20pt}{30pt}
 \caption{}
 \label{fig:myfig}
\end{figure}
\end{document}

As soon as you reference the figure, the ? will be removed. Note that this method does rely on each figure having a label.

Note that I have demonstrated refcheck with a figure, but it works in the same way for anything that has a label (table, chapter, equation, etc).

cmhughes
  • 100,947
8

Here's a hand-made solution:

Add the following to your preamble:

\makeatletter

\let\LaTeX@original@label\label
\let\LaTeX@original@ref\ref

\newcommand{\warnunused}{}

\renewcommand{\label}[1]{%
  \expandafter\global\expandafter\def\csname warning@#1\endcsname{%
    \typeout{warning: label #1 was not referenced.}}%
  \g@addto@macro\warnunused{%
    \csname warning@#1\endcsname}
  \LaTeX@original@label{#1}}

\renewcommand{\ref}[1]{%
  \expandafter\global\expandafter\def\csname warning@#1\endcsname{}
  \LaTeX@original@ref{#1}}

\makeatother

and the following directly before \end{document}:

\warnunused

Now on every LaTeX run, for each unreferenced label you get a line

warning: label (your label here) was not referenced.

on your standard output and in your refwarn.log.

If you prefer the warning directly in your document instead, remove the \typeout (and possibly add a \texttt around the #1 and a \\ at the end) in the warning macro.

celtschk
  • 2,438