Tuesday, May 11, 2010

Deleting unused equation numbers from LaTeX code

Suppose you've written a long LaTeX document containing many many numbered formulas, like for example a scientific paper. Assuming that you have used AUCTeX' fantastic macro completion it may well be the case that many of the numbered equations that you have entered are never referenced. How to get rid of the unused equation numbers? Unfortunately, emacs itself (i.e., reftex) does not seem to offer a remedy. So I came up with a little bash script that finds all labels, all references, matches them up to see which ones are not actually used and then deletes those from your tex file (yes, it deletes all unused labels, not only unused equation labels). Moreover, it even inserts \nonumber commands in equations, so not only will the label definitions disappear, but also the line numbers.

Here's the code. The script takes a TeX file as command line argument and outputs the modified TeX source at the standard output.

 
#!/bin/bash 
 
# Copyright (C) 2010 by Bjoern Rueffer, Time-stamp: <2010-05-11 16:19:49 bjoern>
 
if [ $# -ne 1 ]; then 
    cat <<EOF  
Usage: $0 filename.tex
 
This will output a cleaned version of filename.tex on the standard
output. Every \label{...} definition with an unused label is deleted.
 
Note: Lines with references to labels should NOT contain the symbol #. 
Otherwise bad things will happen...
 
EOF
    exit 1
fi 
 
# do a sanity check 
grep \# $1 |grep 'ref{.*}' && (echo Error: An input line containing a \ 
reference also contains the; echo character '#'. Currently this is \ 
not supported by this script.;) >&2 && exit 1
 
# isolate list of all defined labels
grep \\label\{.*\} $1 | sed -e 's/^.*\\label{\(.*\)}.*$/\1/' \ 
 | sort -u > /tmp/labels
 
# find all references to labels, sorry this is a bit ugly
grep ref\{.*\} $1 | sed -e '
s/\(.*ref{[^}]*}\).*/\1/
s/ref{[^}]*}/FIRSTMARKER&/
s/^.*FIRSTMARKER//
s/ref{\([^}]*\)}/#\1#/g
s/#//
s/#[^#]*#/ /g
s/#//
' | xargs -n 1 echo | sort -u > /tmp/references
 
# identify the labels that have not been referenced
grep -vf /tmp/references /tmp/labels > /tmp/unusedlabels
 
# now delete labels from tex file and insert \nonumber commands
# into equations where necessary
rm -f /tmp/scriptfile
cat /tmp/unusedlabels \ 
    |xargs -n 1 -I % echo /%/s/\\label{%}/\\nolabelhere /g >> /tmp/scriptfile
echo /\\begin{document}/i\\>> /tmp/scriptfile
echo '\\def\\nolabelhere{\\leavevmode\\ifmmode\\nonumber\\else\\fi}%' \ 
    >> /tmp/scriptfile
echo >> /tmp/scriptfile
sed -f /tmp/scriptfile $1 
 
# clean up
rm -f /tmp/scriptfile /tmp/references /tmp/unusedlabels /tmp/labels
 

Björn Rüffer, Copyright 20092011
Disclaimer: The author of this page cannot accept any responsibility for content of pages that this page provides hyperlinks to. Also the author cannot accept responsibility or liability for any damage caused by following or not following instructions given on this page. This page, its contents and style, are the responsibility of the author and do not represent the views, policies or opinions of any other party. The author will not be held responsible for comments posted by third parties on this site. Inappropriate comments will be removed. There is no warranty for any program or source code provided here, to the extent permitted by applicable law. Except when otherwise stated in writing the copyright holders and/or other parties provide the program “as is” without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program is with you. Should the program prove defective, you assume the cost of all necessary servicing, repair or correction.