LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl and its buffer (though) (https://www.linuxquestions.org/questions/programming-9/perl-and-its-buffer-though-287645/)

enemorales 02-08-2005 07:58 AM

Perl and its buffer (though)
 
Hi there...

I work with latex and use several figure files that I create with xfig so I have .fig files that need to be converted to .pstex and/or .pstex_t to be included. This is done with "fig2dev". Since I change the figures, I use a Makefile to reconvert them on-demand as needed. However, recently I've found myself creating and deleting several figures a lot, so to maintain the Makefile became not only tedious, but a source of mistakes. I decided to write a perl script to parse the latex file to find the figure inclusions and create the Makefile. So far, everything was ok. The remaining problem was that figures have to be magnified (size-changed), and this information is not in the latex file but depend on how they look: I choose the size manually in the Makefile. Therefore, I need the script first to parse the old Makefile to look for existing definitions and use them to write new ones. This also worked "ok": If I run

Code:

tex2make document.tex
it get the previous definitions and put them in the new file. I'm sending everything to the standard output, hoping to run

Code:

tex2make document.tex > Makefile
but in this case the script fails to read the old Makefile and moreover I get no Makefile. I've tried changing the name of the Makefile inside the script so It opens "Makefile.old", but this does not work either.

Do you have any idea about what could be happening here? Thank you very much in advance...

chrism01 02-08-2005 07:45 PM

I think we'll need the Perl src to figure(!) it out :)

enemorales 02-09-2005 03:17 AM

Well, I was thinking that my explanation wasn't good at all...

Code:

#!/usr/bin/perl
# tex2make
# Creates a Makefile that compiles the TeX and figures.

# First, we collect the figures in the text file.
# Two possibilities for now:
#  - Things in \ingludegraphics{...} clause and
#  - \input{...} within a figure environment
my $texfile = (shift or (die "TeX file must be specified."));
if( -f $texfile ) {
                open( TEX, "< $texfile" );
} else {
                die "$texfile does not exist."
}

my @figs;
my @lines;
my $inFig = 0;
my $nfigs = 0;
my $line;

while( $line=<TEX> ) {
                if($line =~ /\\includegraphics{(.*?)}/) {
                                $figs[$nfigs++] = $1;
                                $lines[$nfigs-1] = $line;
                }
                $inFig = 1 if($line =~ /\\begin{c?figure}/);
                $inFig = 0 if($line =~ /\\end{c?figure}/);
                if($inFig and ($line =~ /\\input{(.*?)}/) and
                        not ($line =~ /^%/) ) {
                          $figs[$nfigs++] = $1;
                          $lines[$nfigs-1] = $line;
                }
}

close TEX;


# Now we check for a Makefile in the current dir.
# If it exists, we use it for the magnification ratios
my %figsMags = &getPrevious();

# This is for debuggind only
#print "$nfigs figures were detected:\n";
#for( my $i=0; $i<$nfigs; $i++) {
#  print "$i: $figs[$i] in line $lines[$i]\n";
#}

print STDERR "I've found $nfigs figures in $texfile\n";

# Now, we write the file.
print "# THIS MAKEFILE WAS GENERATED WITH tex2make 0.1\n\n";
# First, we need a list of the figures first
$list = "";
for my $fig (@figs) {
                $list .= " $fig";
}
print "FIGS = $list\n\n";

# We start with the PS dependency
my $file = &file_name($texfile);
print "$file.ps: $file.dvi \$(FIGS)\n";
print "\tdvips -p $file.ps $file.dvi\n\n";
# DVI...
print "$file.dvi: $texfile $file.bbl \$(FIGS)\n";
print "\tlatex $texfile && latex $texfile\n\n";
# The bibliography...
print "$file.bbl: $file.bib\n";
print "\tbibtex $file\n\n";
# A PDF dependency also...
print "pdf: $file.ps\n";
print "\tps2pdf $file.ps\n\n";

# Now we generate the dependencies for individual figures

for my $fig (@figs) {
                my $path = &file_path($fig);
                my $type = &file_type($fig);
                my $file = &file_name($fig);
                my $mag = ($figsMags{$fig} or 1.0);
                print STDERR "$fig: path=$path, type=$type, file=$file\n";
                unless( $type eq "pstex_t" ) {
                                print "$fig: $file.fig\n";
                                print "\tfig2dev -L $type -m $mag $file.fig $fig                                            \n\n";
                } else {
                                print "$fig: $file.fig $file.pstex\n";
                                print "\tfig2dev -L $type -m $mag -p $file.pstex                                              $file.fig $fig\n";
                                print "$file.pstex: $file.fig\n";
                                print "\tfig2dev -L pstex -m $mag $file.fig $fil                                            e.pstex\n\n";
                }
}

# A "clean" target is provided also
print "clean:\n";
print "\t-rm $file.{aux,dvi,ps,pdf,tex~}\n";
for my $fig (@figs) {
                my $type = &file_type($fig);
                print "\t-rm $fig\n";
                if( $type eq "pstex_t" ) {
                                my $file = &file_name($fig);
                                print "\t-rm $file.pstex\n";
                }     
}
print "\n";

print "\n# END OF Makefile\n";

# Extracts relative path if exists
sub file_path() {
                my $file = shift;
                return $1 if ($file =~ m|(.*)/|);
}

# Gives filetype among the recognized formats
sub file_type() {
                my $file = shift;
                return $1 if ($file =~ /.*\.(eps|pstex_t|pstex|eepic|epic|tex)/)                                            ;
}

# Gives filename (with relative path but not extension)
sub file_name() {
                my $file = shift;
    my $type = &file_type($file);
    return substr($file,0,length($file)-length($type)-1);
}

# Recovers list of files in Makefile
# Returns hash with keys=names of files and values=magnification ratios
sub getPrevious() {
                my %mags;
                my $n = 0;
                if( -f "Makefile" ) {
                                print STDERR "Old Makefile found. Extracting mag                                            nification ratios.\n";
                                open MAKEFILE, "< Makefile";
                                while( <MAKEFILE> ) {
                                                if (/fig2dev/) {
                                                                chomp;
                                                                my @line = split                                            ;
                                                                my $file = $line                                            [$#line];
                                                                shift(@line) unt                                            il ($line[0] eq "-m");
                                                                my $mag = $line[                                            1];
                                                                $mags{$file} = $                                            mag;
                                                                $n++;
                                                }
                                }
                                close MAKEFILE;
                }
                print STDERR "Found $n magnification definitions in old Makefile                                            .\n";
                return %mags;

Thank you!


All times are GMT -5. The time now is 07:33 PM.