# Is there a way to force LaTeX to issue a warning if a figure is never referenced? [duplicate]

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?

-

## marked as duplicate by diabonas, lockstep, Martin Scharrer♦Dec 4 '11 at 12:56

Are all figures labeled? –  Marco Daniel Dec 3 '11 at 20:14
Yes, in my case all of the figures are labeled. –  nickb Dec 3 '11 at 20:14

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.

\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).

-
That's a neat package I'd never heard of. Thanks! –  Alan Munn Dec 3 '11 at 20:34
That's quite useful. However, is there a way to have refcheck loaded, and not have the <fig:myfig> in the margin, once the figure is referenced. –  Peter Grill Dec 3 '11 at 20:34
@PeterGrill Good question, not sure! I usually just comment out %\usepackage{refcheck} when I'm done checking –  cmhughes Dec 3 '11 at 20:42

Here's a hand-made solution:

\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.}}%
\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.

-