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.
Kalidoss, 24 June 2010: With your script file, except deleting labels from tex file and inserting \nonumber commands into equations where necessary is working with me. Where have I gone wrong? I am interesting in finding and deleting \label{...} in $$..$$, \[ .. . \] and {eqnarray*} environments. How to achieve this behaviour?
Björn, 30 November 2010: What the script does is essentially a selective search and replace. It is not keeping track of the type of environment where a label is defined or if that label even occurs in math-mode at all. What you seem to be asking is surely possible as well, but a different approach would be needed. I would use e.g. perl to go through the source and pick out the environments you are interested in and then delete the label definition. Regular expressions are your friend ;)
Edwin, 20 December 2010: How to findout the equation labels comes in listed environment in TeX file. Any script for this?
Björn, 22 December 2010: Not at the moment. But one way of writing such a script is to first find all labels (e.g. by using parts of the script above) and then checking for each of them if they appear in the type of environment that you are interested in.