LaTeX Math Equation to PNG

24 03 2009

I went hunting a few days ago to find a program that would turn a LaTex typestted math equation into a png. I found Hyperlatex a few days ago, and it works pretty well. I made a bash file to use it, taking a LaTeX math equation as an argument (without the “$” or “$$” surrounding the eqn.). I put these contents into a file called mathtex: #!/bin/bash math=$1
file=”tranzitiona1″
pic=”eqn1″
echo “\documentclass{article}
\usepackage{hyperlatex}
\begin{document}
\W\begin{quote}
\begin{image}[900][900]{$pic} $” > file.tex echo math >> file.tex echo ”$ \end{image} \W\end{quote} \end{document}” >>$file.tex
hyperlatex -png $file convert$pic.png -resize 30% $pic.png rm$file.makeimage $file.log$file.dvi $file.aux$file.tex \$pic.ps

An example would be: ./mathtex “\frac{(r_1+r_2)^2}{r_1^2+2r_1r_2+r_2^2}=1\to(r_1+r_2)^2=r_1^2+2r_1r_2+r_2^2″
Produces eqn1.png:

Imagemagick is required for the resizing. I use a large resolution at first so that it doesn’t look ugly like the hyperlatex examples.

If you sudo gedit /usr/bin/ps2image, you can add “-background white” to the pnmtopng command on line 43 so that the background isn’t transparent.