13

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}
yo'
  • 51,322
  • What about compatibility with hyperref? – Werner Oct 30 '14 at 18:24
  • 1
    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

2 Answers2

14

Use the refcheck package:

enter image description here

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

Werner
  • 603,163
  • 1
    After discovered this package I asked myself: is there something impossible to do in TeX? Maybe no. – Sigur Oct 30 '14 at 18:41
  • 1
  • 1
    I think it's important to mention that refcheck should be loaded after some other package is loaded. I tried to use it in my project by loading it as the very first package, but it didn't work. I then decided to load it last and now it works. I don't know what package(s) should be loaded before refcheck, but I'm certain there must be some such package. – Lluís Alemany-Puig Mar 16 '23 at 11:42
1

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}
\begin{equation}
\label{eq:first}
y=x
\end{equation}
\section{BOO}\label{boo}
\begin{equation}
\label{eq:second}
y=x^2
\end{equation}
In Section~\ref{bla}, I use equation \ref{eq:first}.

Unused labels are \unusedlabels
\end{document}

enter image description here