Editing LaTeX with Emacs - finding unused \ ref

When writing a .tex document, symbols are often assigned. When I finish a document, sometimes I find that I did not refer to all the equations. So, I need to look for equations that I have not referenced, and turn off the numbering for these equations. How can I do this in Emacs?

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

Thank. (I think it's time for me to learn LISP).

+3
source share
2 answers

Hacky Perl, suitable for single use. Neither verified nor proven.

, (), , . , .

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{$_}; 
}
+5
+3

Source: https://habr.com/ru/post/1748461/


All Articles