Wednesday, October 9, 2013

Find multiply-defined labels in a complex Latex project

Slower way
Whenever a label is defined multiple times in Latex, there will be a general warning, and also earlier in the log file the specific instances of the labels will be mentioned.

Nonetheless, you may also directly look in the project files for such duplicate labels if you want to be fancy. Here is a one-liner perl script that will do the job and was originally shared by Walt Mankowski. Run this script in the directory that your latex files reside.
perl -nE 'say $1 if /(\\label[^}]*})/' *.tex | sort | uniq -c | sort -n
You will see a list of the duplicate labels. Then use grep to find those labels. For example if one of the duplicate labels is \label{sec:party_structure}, then you can use this command:
grep "\label{sec:party_structure}" *.tex

Faster way
You can also combine grep and uniq for a faster solution. To see the number of repetitions, try
grep -o \\label{.*} *.tex | uniq -c
or to see only the duplicate labels, try
grep -o \\label{.*} *.tex | uniq -d
This solution was given here.

No comments:

Post a Comment