# Get list of unused labels [duplicate]

I have a long article (~20 pages) with lots of equations being numbered and stuff. For a good cleanup, I would like to unnumber the unused equations. I am aware I can hide the unused numbers, but I would like to clean the thing up and not rely to the feature of mathtools. Therefore, is it possible to somehow display the list of unused labels?

On the following MWE, it should show (in the log, in bash, whatever) that boo was unused:

\documentclass{article}
\begin{document}
\section{BLA}\label{bla}
\section{BOO}\label{boo}
In Section~\ref{bla}.
\end{document}


## marked as duplicate by yo', Manuel, lockstep, Werner, barbara beetonOct 30 '14 at 19:15

• What about compatibility with hyperref? – Werner Oct 30 '14 at 18:24
• You could grep the defined labels from the text and match them to the list of the used references. The gap between both lists is the list of unused references. – Uwe Ziegenhagen Oct 30 '14 at 18:25
• @UweZiegenhagen This is possible for \bibitem ... \cite since that information is in aux, and it's a one-liner in bash. However, \ref keeps no trace in aux... – yo' Oct 30 '14 at 18:27
• @Werner I'm willing to switch of hyperref for those 5 minutes needed for the cleanup ;) – yo' Oct 30 '14 at 18:27

Use the refcheck package:

\documentclass{article}
\usepackage{refcheck}
\begin{document}
\section{BLA}\label{bla}
\section{BOO}\label{boo}
In Section~\ref{bla}.
\end{document}


Unused \labels are also marked in the .log. The above MWE outputs

Package refcheck Warning: Unused label boo' on input line 5.


in the .log. The visual queues in the output can be turned on/off using the [norefs] option. It would also work for \cites (with the accompanying showcites/nocites boolean package option).

• After discovered this package I asked myself: is there something impossible to do in TeX? Maybe no. – Sigur Oct 30 '14 at 18:41
• @Sigur Yes. – yo' Oct 30 '14 at 18:49

A homegrown version. It redefines \label and \ref and then use \unusedlabels at the end to see which labels are unused. Once the process is done, you can remove the redefinitions.

\label is redefined to make a concatenated space-separated string of all the labels.

\ref is redefined to go through the concatenated list, item by item, and compare the item to the \ref argument, and proceeds to rebuild the concatenated list. If the compare is true, it does nothing, thereby removing the item from the list. If the compare is false, it adds the item back onto the reconstituted list.

Anything left on the list at the end is unused.

\documentclass{article}
\usepackage{etoolbox}
\let\svlabel\label
\let\svref\ref
\def\unusedlabels{}
\renewcommand\label[1]{\svlabel{#1}\global\edef\unusedlabels{\unusedlabels$<$#1$>$ }}
\renewcommand\ref[1]{\svref{#1}%
\edef\teststring{$<$#1$>$}%
\edef\tmp{\unusedlabels}%
\def\unusedlabels{}%
\expandafter\refhelper\tmp\relax%
}
\def\refhelper#1 #2\relax{%
\edef\expandcase{#1}%
\ifdefstrequal{\teststring}{\expandcase}{}{\edef\unusedlabels{\unusedlabels#1 }}%
\if\relax#2\relax\else\refhelper#2\relax\fi
}

\begin{document}
\section{BLA}\label{bla}
$$\label{eq:first} y=x$$
\section{BOO}\label{boo}
$$\label{eq:second} y=x^2$$
In Section~\ref{bla}, I use equation \ref{eq:first}.

Unused labels are \unusedlabels
\end{document}
`