# [OS X TeX] Finding duplicate labels - a perl script

Mon Jun 24 13:02:50 EDT 2013

Anyone who has written a very long math document has probably encountered the problem of accidentally creating duplicate labels. Right now I have 352 labels, and I have run into this problem several times. I stumbled on a way of detecting exactly which labels have duplicates here:

http://www.mawode.com/blog/blog/2012/03/21/finding-duplicate-latex-labels/

It involves running this perl script in the terminal:

perl -nE 'say $1 if /(\\label[^}]*})/' *.tex | sort | uniq -c | sort -n Just substitute the full path to your .tex file for the "*.tex" string in the above. You can do that by first copy/pasting perl -nE 'say$1 if /(\\label[^}]*})/'          <-------make sure the ending space is there

then dragging your file to the terminal, and finally copy/pasting

| sort | uniq -c | sort -n

and running the script. It creates a sorted list of all your labels together with the number of occurrences of each within the terminal window. Finding duplicates is just a matter of scanning the list for counts > 1. It's also very fast.

It seems to me that an Applescript could be created to run this perl script from BBEdit or TeXShop, finding the path of the front .tex document, assembling the perl script, and then passing it on and running it in the terminal, essentially making it a one-click operation. Unfortunately, I'm not handy enough with Applescript to figure out how to do this.

Richard Séguin