Editing LaTeX with Emacs - search for unused \ref
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 LIS开发者_如何学CP).
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{$_};
}
Or, you might find that the refcheck package suits your needs.
精彩评论