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:

echo “\documentclass{article}
\[” > $file.tex
echo $math >> $file.tex
echo ” \]
\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 $

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.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: