#!/usr/bin/perl use strict; use warnings; # # Last updated: 31 Mar 2007 # # Fixed a bug preventing processing when filename contains underscore # # Requirements: the geometry and graphicx LaTeX packages # # # ########################## # HOW TO USE THIS FILE: ########################## # # 1) Rename this file by dropping the .txt suffix # and put it into your Library/TeXShop/Engines folder. # # 2) Run the Terminal application (which you find in the Utilities folder) # and, in the Terminal window, type: # # cd ~/Library/TeXShop/Engines # # then, press the Enter key and type: # # chmod +x metafun.engine # # and press Enter again. The above command makes this script executable. # # 3) Switch to TeXShop, open a MetaPost file, # choose the metafun engine and typeset! # ####### # HINT ####### # # Write: # # %!TEX TS-program = metafun # # at the very beginning of your document: # this will let TeXShop automatically switch to the right engine! # ($ARGV[0] =~ /.mp$/) or die "Sorry, your MetaPost source file must have a .mp suffix"; my ($filename) = ($ARGV[0] =~ /^(.*)\.mp$/); print "Going to process $filename...\n"; system("mptopdf $ARGV[0]") == 0 or exit(1); # Combine output pdf files opendir(DIR, '.'); my @mps = map { ($_ =~ /$filename-\d+\.pdf/) ? $_ : () } readdir(DIR); close(DIR); # Sort filenames by their numerical suffix @mps = sort { pack('J', $a =~ /-(\d+)\.pdf/) cmp pack('J', $b =~ /-(\d+)\.pdf/) } @mps; my $hsize = 0; my $vsize = 0; my $f; my $n; # Determine the largest horizontal/vertical size of a bounding box foreach $f (@mps) { ($n) = ($f =~ /^.*-(\d+)\./); # Open the file and determine its bounding box open(EPS, $filename . '.' . $n) or die "Oh oh, cannot open $f\n"; my $line; while ($line = ) { # break the loop when the bounding box is found last if ($line =~ /BoundingBox/); } close(EPS); if (defined $line) { my ($llx, $lly, $urx, $ury) = ($line =~ /BoundingBox:\s+(.+)\s+(.+)\s+(.+)\s+(.+)/); print "Bounding box of $f: $llx $lly $urx $ury\n"; if ($hsize < $urx - $llx) { $hsize = $urx - $llx; } if ($vsize < $ury - $lly) { $vsize = $ury - $lly; } } } $hsize = 495 if ($hsize == 0); $vsize = 742 if ($vsize == 0); $hsize += 100; $vsize += 100; # Make pdf output my $latex = '\documentclass{minimal}'; $latex .= '\usepackage[paperwidth=' . $hsize . 'pt,paperheight=' . $vsize . 'pt,margin=1cm]{geometry}'; $latex .= '\usepackage[pdftex]{graphicx}'; $latex .= '\begin{document}'; foreach $f (@mps) { my $title = $f; $title =~ s/[\$\_\#\&\%]/\\$&/g; # Escape special characters with backslash $latex .= '\begin{center}\textbf{' . $title . '}\bigskip' ."\n\n" . '\noindent\includegraphics{' . $f . '}\end{center}\newpage'; } $latex .= '\end{document}\end' . "\n"; open(LATEX, "| pdflatex -no-shell-escape -jobname=$filename") or die "Cannot execute pdflatex"; local $SIG{PIPE} = sub { die "pdflatex pipe broken" }; print LATEX $latex or die "Cannot write output pdf file"; close(LATEX) or die "Cannot close pipe: $! $?"; ## Remove intermediate files #print "Removing intermediate files... "; #foreach $f (@mps) { # system("rm $f"); # my ($n) = ($f =~ /^.*-(\d+)\./); # system("rm $filename.$n"); #} #print "Done!\n"; print "Generated pdf for figures: @mps\n"; exit(0);