tags:

views:

108

answers:

2

Hello,

When writing a .tex document, I often have labeled equations. When finishing the document, I sometimes find that I haven't referenced all of the equations. So, I need to look for the equations which I haven't referenced, and disable numbering for those equations. How can I do this in Emacs?

Basically, I need to search for all \label{*}. Then, for each * I find, let me know if there is less than 1 corresponding \ref{*}.

Thanks. (I guess it really is time for me to learn LISP).

+5  A: 

Hacky Perl, suitable for a one-off. Neither tested nor proved correct.

The capture regex may grab both the entire match and the () match, I don't recall offhand. If it does, grab the odds for the job.

use strict; 
use warnings;

#standard slurp 
my ($fh, $file);
open $fh, "<", "mydatafile" or die("$!:mydatafile");
{
 local $/ = undef; 
 $file = <$fh>; 
 close $fh; 
} 


#grab all captures.
my @labels = ($file =~ /\\label{(.*?)}/msg);

#hashes are easier for existence checks
my %labels = map {$_ => 1 } @labels;

my @refs = ($file =~ /\\ref{(.*?)}/msg);
my %refs = map {$_ => 1 } @refs;

foreach (keys %labels)
{
 print "Error, $_ not referenced\n" unless $ref{$_}; 
}
Paul Nathan
Excellent. Thank you so much. This is an above and beyond answer.
stevejb
But what do you do with this? Put on your .emacs file? Sorry, I am a newbie...
Vivi
@Vivi: you'd put it into a text file - say, refcheck.pl - and then modify the "mydatafile" file to have your file operated on.
Paul Nathan
thanks! I guess I still have a long way to go before I can get by in Emacs :)
Vivi
@Vivi: You could do the same thing in Emacs. I'm just not as good as hacking elisp for this sort of things as I am with Perl.
Paul Nathan
Oh, that is good to know! I have only been using Emacs for 2 weeks, but I am planning on learning elisp. When I saw your post I thought maybe that maybe that wouldn't be enough, so I guess if I learn elisp I will have what I need :)
Vivi
+2  A: 

Or, you might find that the refcheck package suits your needs.

High Performance Mark