#! /usr/bin/perl -w # Draws a png of a passed latex math code # Released under the GNU Public License # (c) 29, January 2001 by Don Armstrong # Inspired by tex2im version 1.3 use strict; # or die! use CGI::Carp qw/fatalsToBrowser/; use CGI; use File::Temp qw/tempfile tempdir/; use FileHandle; my $q = new CGI; #default values my $default = { background => 'white', foreground => 'black', format => 'png', resolution => '150x150', latex => 'x_0^2+y_0^2=r_0^2' }; if (defined $q->param('format') and $q->param('format') =~ /^(\w+)$/) +{ $default->{format} = $1; } if (defined $q->param('resolution') and $q->param('resolution') =~ /^( +\d+x\d+)$/) { $default->{'resolution'} = $1; } foreach my $param_key ('background','foreground','latex') { my $param_value = $q->param($param_key); if (defined $param_value and $param_value =~/[\w\d]/) { $default->{$param_key} = $param_value; } } $default->{latex_shell} = << 'EOL'; \documentclass[12pt]{article} \usepackage{color} \pagestyle{empty} \pagecolor{<background>} \begin{document} \begin{eqnarray*} {\color{<foreground>} <latex> }\end{eqnarray*} \end{document} EOL # Slap in the latex and defaults into the latex shell $default->{latex_shell} =~ s/\<(foreground|background|latex)\>/$defaul +t->{$1}/ge; # Generate a temporary dir to deal with latex output files, have my $temp_dir = tempdir(CLEANUP => 1); die "Unable to create temp dir" if (! -d $temp_dir); chdir $temp_dir; my $latex_file = new FileHandle; $latex_file->open("> latex.tex") or die "Unable to open $temp_dir/late +x.tex"; print {$latex_file} $default->{latex_shell}; $latex_file->close; `latex -interaction=batchmode latex.tex> /dev/null`; `dvips -o $temp_dir/latex.ps -E $temp_dir/latex.dvi 2> /dev/null`; `convert -density $default->{resolution} $temp_dir/latex.ps $temp_dir/ +latex.$default->{format}`; my $converted_file = new FileHandle; $converted_file->open("< $temp_dir/latex.$default->{format}") or die " +Unable to open $temp_dir/latex.$default->{format}"; print $q->header(-type=>"image/$default->{format}"); print <$converted_file>; $converted_file->close;

In reply to latexmath2png.pl by dondelelcaro

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.