# Turn a warning (“Unused label”) into an error

The following example, a follow-up of Give an error if I don't reference a label, generates a warning:

\documentclass{article}
\usepackage{amsmath}
\usepackage{refcheck}

\begin{document}
\begin{figure}
% ...
\caption{A nice picture of my dog}\label{fig:dog}
\end{figure}
% As seen in figure~\ref{fig:dog}
\end{document}


The warning is:

Package refcheck Warning: Unused label fig:dog' on input line 8.


I would like to turn that warning (and only that warning, not others) into an error. I would prefer to do this inside LaTeX rather than through an external bash script parsing the log.

• Sounds like a bad idea: everytime you add a \label{foo}/\ref{foo} pair the first compilation would give an error (because two compilations are needed before \ref knows there's a suiting \label) – clemens Jul 5 '15 at 12:05
• @clemens ah good to know! There is no way to only give an error on the second compilation? – Xu Wang Jul 5 '15 at 13:22
• I don't know (today is the first day I heard about the refcheck package). But warnings are not too bad IMHO: 1) one should pay attention to warnings anyway and 2) an unused label doesn't do any harm, really. – clemens Jul 5 '15 at 13:36
• @clemens I agree in general. But some cases this would be very nice to have I think. – Xu Wang Jul 5 '15 at 13:42

This seems a strange thing to give an error on, it's normal to label anything that might conceivably be referenced, but anyway

\documentclass{article}
\usepackage{amsmath}
\usepackage{refcheck}

\makeatletter
\let\old@warning@rc@\@warning@rc@
\def\@warning@rc@#1{\x@warning@rc@#1\relax}

\AtEndDocument{%
\immediate\write\@auxout{\gdef\string\rc@unlab{Unused label}}}

\def\x@warning@rc@#1 #2 #3\relax{%
\def\rc@tmp{#1 #2}%
\ifx\rc@tmp\rc@unlab
\PackageError{refcheck}{#1 #2 #3}\@ehc
\else
\old@warning@rc@{#1 #2 #3}%
\fi}

\begin{document}
\begin{figure}
% ...
\caption{A nice picture of my dog}\label{fig:dog}
\end{figure}
% As seen in figure~\ref{fig:dog}
\end{document}


This checks that the start of the warning is Unused label and if so makes an error. The test macro holding the string Unused label` is defined indirectly by writing the definition to the aux file so it is not defined the first time you add this to the document, so that you have a chance for cross references to stabilize on the second run. You get a warning the first time, as intended for refcheck.