[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[mgp-users 01086] Yet another embedded LaTeX preprocessor



  Hello,

just to send (attached) yet another preprocessor for embedded LaTeX,
called 'mgplatex'. Patches and suggestions welcome. mgplatex -h says :

NAME mgplatex - Compile latex code embedded in MagicPoint code

    Creates a MagicPoint file file.mgp from a file containing MagicPoint
    code with inlined LaTeX code.

SYNOPSIS mgplatex [options] file

    Reads file, transforms LaTeX code enclosed in %beginlatex-%endlatex or
    \beginlatex-\endlatex blocks into mgp code and writes the result in
    file.mgp, which may then be read by mgp.

    In the process, a directory is created (default : file.auxfiles), in
    which .tex files are created and transformed into encapsulated
    postscript (.eps) images by latex and dvips.

    If a %latexpreamble-%endlatexpreamble block is present, it is inserted
    before "\begin{document}". By default, "\usepackage[latin1]{inputenc}"
    is inserted.

EXAMPLE
    The following block

      %beginlatex  0 60 30
      A little bit of \LaTeX : 
      \[ A(x) = \int_{0}^{\infty} e^-\frac{x}{2} dx \]
      %endlatex

    is transformed by mgplatex into :

      %image "file.auxdir/latex-0.eps" 0 60 35

    where file.auxdir/latex-0.eps is the image that shows the output of
    LaTeX.

    It is possible to use latex packages by specifying them in preamble. The
    following example uses the color and dsfont packages :

      %latexpreamble
      \usepackage[latin1]{inputenc}
      \usepackage{dsfont}
      \usepackage{color}
      %endlatexpreamble
      %beginlatex  0 30 5
        $$ \textcolor{red}{\mathbf{X}} \in \mathds{R}^3 $$
      %endlatex

OPTIONS
    -d dirname
        Put generated files in dirname. By default, uses file.auxfiles.

    -f  Force compilation of latex code, even if it is unchanged since last
        time it was compiled.

    -v  Be verbose.

    -h  Show this help and exit.

SEE ALSO
    mgp, <http://www.mew.org/mgp/>, latex and dvips.

    Embedding latex in mgp is an old wheel that is often reinvented on
    mgp-user@mew.org. Luigi Rizzo made a on-the-fly preprocessor [1] and so
    did Stephan Buchert [3], who modified an the script tex2eps.sh by
    Sylvain Pion. Anders Logg made an offline preprocessor mgptex [2].
    mgplatex should be able to read the embedded code of mgptex.

     [1] http://www.mew.org/ml/mgp-users/msg00253.html
     [2] http://www.csl.sony.co.jp/person/nishida/mgp-users/msg00290.html
     [3] http://www.csl.sony.co.jp/person/nishida/mgp-users/msg00419.html


-- 
Etienne Grossmann ------ http://www.isr.ist.utl.pt/~etienne
#!/usr/bin/perl -w

=head1 NAME mgplatex - Compile latex code embedded in MagicPoint code

Creates a MagicPoint file F<file.mgp> from a F<file> containing MagicPoint
code with inlined LaTeX code.

=head1 SYNOPSIS mgplatex [options] F<file>

Reads F<file>, transforms LaTeX code enclosed in B<%beginlatex-%endlatex> or
B<\beginlatex-\endlatex> blocks into mgp code and writes the result in
F<file.mgp>, which may then be read by F<mgp>.

In the process, a directory is created (default : F<file.auxfiles>), in
which .tex files are created and transformed into encapsulated postscript
(.eps) images by B<latex> and B<dvips>.

If a B<%latexpreamble-%endlatexpreamble> block is present, it is inserted
before C<\begin{document}>. By default, C<\usepackage[latin1]{inputenc}> is
inserted. 

=head1 EXAMPLE

The following block

  %beginlatex  0 60 30
  A little bit of \LaTeX : 
  \[ A(x) = \int_{0}^{\infty} e^-\frac{x}{2} dx \]
  %endlatex

is transformed by mgplatex into :

  %image "file.auxdir/latex-0.eps" 0 60 35

where F<file.auxdir/latex-0.eps> is the image that shows the output of
LaTeX.

It is possible to use latex packages by specifying them in preamble. The
following example uses the color and dsfont packages :
  
  %latexpreamble
  \usepackage[latin1]{inputenc}
  \usepackage{dsfont}
  \usepackage{color}
  %endlatexpreamble
  %beginlatex  0 30 5
    $$ \textcolor{red}{\mathbf{X}} \in \mathds{R}^3 $$
  %endlatex

=head1 OPTIONS

=over 4

=item -d F<dirname>

Put generated files in F<dirname>. By default, uses F<file.auxfiles>.

=item -f

Force compilation of latex code, even if it is unchanged since last time it
was compiled.

=item -v

Be verbose.

=item -h

Show this help and exit.

=back

=head1 SEE ALSO

B<mgp>, L<http://www.mew.org/mgp/>, B<latex> and B<dvips>.

Embedding latex in mgp is an old wheel that is often reinvented on
mgp-user@mew.org. Luigi Rizzo made a on-the-fly preprocessor [1] and so did
Stephan Buchert [3], who modified an the script B<tex2eps.sh> by Sylvain
Pion.  Anders Logg made an offline preprocessor B<mgptex> [2]. B<mgplatex>
should be able to read the embedded code of B<mgptex>.


 [1] http://www.mew.org/ml/mgp-users/msg00253.html
 [2] http://www.csl.sony.co.jp/person/nishida/mgp-users/msg00290.html
 [3] http://www.csl.sony.co.jp/person/nishida/mgp-users/msg00419.html

=cut

$verbose = 0;
$directory = "";
$latexpreamble = "\\usepackage[latin1]{inputenc}\n";
$force = 0;

exec "perldoc -t $0|cat" unless $ARGV;

while ($ARGV[0] =~ /^-/) {
    my $opt = shift @ARGV;
    if ($opt =~ /v/) { $verbose = 1 }
    if ($opt =~ /d/) { $directory = shift @ARGV }
    if ($opt =~ /f/) { $force = 1 }
    if ($opt =~ /h/) { exec "perldoc -t $0|cat" }
    die "Unknown option '$opt'" unless /[vdfh]/;
    
}


$infile = shift @ARGV;

$outfile = "$infile.mgp";

$directory = "$infile.auxdir" unless $directory;

open FF, "<$infile" or die "Can't read file '$infile'";
open GG, ">$outfile" or die "Can't write file '$outfile'";

unless (-d $directory) {
    print "Creating '$directory'\n" if $verbose;
    mkdir $directory or die "Can't create directory '$directory'";
}

$counter = 0;			# Counter for latex expressions / files

				# CD to directory
chdir $directory or die "Can't cd to '$directory'";

$outpipe = $verbose ? "" : " &> /dev/null" ;

while (! eof(FF)) {
    $_ = <FF>;

    my ($keep, $latex, $latexsize) = ("","","");
    if (/^\s*(?:\\|\%\s*)beginlatex\s*(.*)/) {

	$latexsize = $1;

	my $basename = "latex-$counter";

	while (1) {
	    $_ = <FF>;
	    last if  /^\s*(?:\\|\%\s*)endlatex/;

				# Size of latex image

	    if (/^\%\s*latexsize\s*(.*)/) {
		chomp ($latexsize = $1);

	    } else {
				# Correct \input{} for changed directory
		s|\\input{(.*)}|"\\input{". &localize($1) ."}"|eg;

		/^\%\s*(center|leftfill|rightfill)/ ? $keep : $latex .= $_;
	    }
	    if (eof(FF)) {warn "beginlatex without endlatex"; last}
	}
	print "Keep  >>$keep<<\nLatex >>$latex<<\n" if $verbose;

	print GG $keep;		# Print kept code
	print GG qq(%image "$directory/$basename.eps" $latexsize\n);
				# Create latex file

	$latexfile = "$basename.tex";
	$tmplatex = "tmplatex.tex";
	open HH, ">$tmplatex" or 
	    die "Can't create latex file '$tmplatex'\n";

	print HH <<EOLATEX;
\\documentclass[12pt]{article}
$latexpreamble
\\begin{document}
\\thispagestyle{empty}
EOLATEX

	print HH $latex, '\end{document}' , "\n";

	close HH;

	$counter++ ;
				# Check whether compilation is necessary

	if (! $force && -e $latexfile && 
	    ! system ("cmp $latexfile $tmplatex >/dev/null")) {
	    if ($verbose) {
		print "Skipping unchanged file '$latexfile'\n";
	    }
	    next
	} else {
	    rename $tmplatex, $latexfile or 
		die "Can't rename temp '$tmplatex' to '$latexfile'";

	    if ($verbose) {
		print "Compiling '$latexfile'\n";
	    }
	}

				# Compile latex file
	die "Problems running latex" if 
	    system "latex $latexfile $outpipe";

	die "Problems running dvips" if 
	    system "dvips -q -E $basename.dvi -o $basename.eps $outpipe";

				# Todo : remove temp files

				# End of reading LaTeX code ##########

				# Read LaTeX preamble ################
    } elsif (/^\s*(?:\\|\%\s*)latexpreamble/) { 
	
	$latexpreamble = "";

	while (1) {
	    $_ = <FF>;
	    last if  /^\s*(?:\\|\%\s*)endlatexpreamble/;
	    $latexpreamble .= $_;
	    if (eof(FF)) {
		warn "latexpreamble without endlatexpreamble";
		last;
	    }
	}
				# End of reading LaTeX preamble ######

    } else {			# Pass mgp code through
	print GG $_;
    }
}
close FF;
close GG;
exit 0;				# That's it

				# change \input{x} to \input{../x}
sub localize {
    my $f = shift;
    $f =~ s{^}{../} unless $f =~ m{^/};
    $f;
}