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
4 comments:
Comments are moderated to prevent abuse and may not be made public immediately.