Hi,

I wrote a perl script, renumber.pl, for renumbering equations in LyX (attached). It will take existing equation labels and renumber them according to the section heading. For example, in Chapter 1, section 4, subsection 2, equations will be labeled 1.4.2.1, 1.4.2.2, etc. The script will not create equations labels where there were none.

Usage (at a command prompt, assuming you have perl on your system):

perl renumber.pl oldfile.lyx newfile.lyx  [ oldprefix  [ newprefix ] ]

It searches oldfile.lyx for equation labels of the form "\label{eq:" where "eq:" is the default prefix. After renumbering equations (and all references to them) it saves the revised document in newfile.lyx. (newfile.lyx will be overwritten without prompting.)

If you use another prefix for equations, such as "EQ-", specify it in oldprefix, e.g.,

perl renumber.pl oldfile.lyx newfile.lyx EQ-

You can also specify a new prefix for equation labels, e.g.,

perl renumber.pl oldfile.lyx newfile.lyx EQ- Eqs:

If you want to experiment with it I suggest making a safe copy of your document first. Also note that this is one of my first attempts at perl scripting, so I may not have written this script in the most efficient manner.

I don't know if others will find it useful, but if so, is there a website where one can post contributions? I couldn't find it on the LyX wiki.

Thanks,

Gerard Ateshian

#!/usr/local/bin/perl -w
#
# Renumber equations in a LyX document.
# Usage:
# perl renumber.pl oldfile newfile [oldprefix [newprefix]]
#
# Search for equation labels of the form "\label{eq:" where "eq:" is the
# default prefix. If you use another prefix for equations, such as "EQ-",
# specify it in oldprefix, e.g.,
#
# perl renumber.pl oldfile newfile EQ-
#
# You can also specify a new prefix for equation labels, e.g.,
#
# perl renumber.pl oldfile newfile EQ- Eqs:
#
# by Gerard Ateshian ([EMAIL PROTECTED]), Feb. 8, 2007.

use strict;
my ($oldfile,$newfile);
my ($oldprefix,$newprefix,$tmpprefix);
my ($line,$all,%labels,@labels);
my ($chap,$sec,$ssec,$sssec);
my ($root,$eq,$oldlabel,$newlabel);

if ($#ARGV < 1) {
    print "usage: renumber oldfile newfile [oldprefix [newprefix] ]\n";
    exit;
}

# Extract command line arguments
$oldfile = $ARGV[0];
$newfile = $ARGV[1];
$oldprefix = ($ARGV[2] or 'eq:');	# Use 'eq:' as default prefix for equation labels
$newprefix = ($ARGV[3] or $oldprefix);	# Use old prefix as default for new prefix

# Open input and output files
open(OF, $oldfile) or die "$!";
open(NF, ">$newfile") or die "$!";

# Initialize
$tmpprefix = $oldprefix.'%';
$chap = 0;
$all = '';

# read in each line of the file
while ($line = <OF>) {
    $all .= $line;	# Store the entire file in the string $all
# Look for section headings or equation labels
    if ($line =~ /\\layout Chapter/) {
	$chap += 1;
	$root = $chap;
	$sec = 0;
	$eq = 0;
    } elsif ($line =~ /\\layout Section/) {
	$sec +=1;
	if ($chap != 0) {
	    $root = join('.',$chap,$sec);
	} else {
	    $root = $sec;
	}
	$ssec = 0;
	$eq = 0;
    } elsif ($line =~ /\\layout Subsection/) {
	$ssec +=1;
	if ($chap != 0) {
	    $root = join('.',$chap,$sec,$ssec);
	} else {
	    $root = join('.',$sec,$ssec);
	}
	$sssec = 0;
	$eq = 0;
    } elsif ($line =~ /\\layout Subsubsection/) {
	$sssec +=1;
	if ($chap != 0) {
	    $root = join('.',$chap,$sec,$ssec,$sssec);
	} else {
	    $root = join('.',$sec,$ssec,$sssec);
	}
	$eq = 0;
    } elsif ($line =~ /(\\label)(\{$oldprefix.*\})\\/) {
# Look for an equation label.
# This regexp assumes that the equation label is followed by a backslash,
# e.g., as in "\label{eq:5-2}\end{equation}".  It uses the backslash in \end
# to terminate the match.
	$oldlabel = $2;
	$eq += 1;
# Use a temporary prefix for the new label, in case the new prefix
# is the same as the old prefix, which might cause two different
# equations to temporarily share the same label.
	$newlabel = "{$tmpprefix$root.$eq}";
#	print "Old label: $oldlabel, New label: $newlabel\n";
# Store the equation label and its replacement in %labels
	$labels{$oldlabel} = $newlabel;
    }
}

# Check to see if any equation labels were found
@labels = keys %labels;
if ($#labels == -1) {die "No equation labels found!\n"};

# Do the global search and replace and save the results into a new file
print "Searching and replacing...\n";
for $oldlabel (@labels) {
    $newlabel = $labels{$oldlabel};
    $all =~ s/$oldlabel/$newlabel/g;
}
# Change the temporary equation prefix to the new prefix
$all =~ s/$tmpprefix/$newprefix/g;
print NF $all;

close(OF);
close(NF);

Reply via email to