Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
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

2 Answers 2

up vote 18 down vote accepted

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

share|improve this answer
    
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:

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.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.