LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-08-2005, 07:58 AM   #1
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
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...
 
Old 02-08-2005, 07:45 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I think we'll need the Perl src to figure(!) it out
 
Old 02-09-2005, 03:17 AM   #3
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Original Poster
Rep: Reputation: 31
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 05:37 PM
cgi perl : I cant get perl to append my html file... the_y_man Programming 3 03-22-2004 05:07 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM
can i set ~/public_html/perl as a perl directory for apache? doublefailure Linux - Networking 1 07-09-2002 04:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration