Last October I described my algorithm for rendering LaTeX equations for use on this blog: “Rendering Equations in Movable Type”. For kicks, here are instructions on how to manually run the algorithm. The only thing missing is the middle alignment part of the algorithm.
First create a file, called eqn.tex using the following skeleton. Insert your LaTeX equation code where it says to.
\nonstopmode
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath,amsfonts,amssymb,wasysym,latexsym,marvosym,txfonts}
\usepackage[pdftex]{color}
\pagestyle{empty}
\begin{document}
\fontsize{12}{24}
\selectfont
\color{white}
\pagecolor{black}
\[
%
% EQUATION GOES HERE
%
\]
\end{document}
Next process the file with pdflatex eqn.tex, which will render your equation as eqn.pdf. Now we will use the convert command line tool from the ImageMagick library to turn eqn.pdf into eqn.png.
convert \( -density $DENSITY eqn.pdf -trim +repage \) \
\( +clone -fuzz 100% -fill $FG -opaque black \) \
+swap -compose copy-opacity -composite \
\( +clone -fuzz 100% -fill white -opaque $BG +matte \) \
+swap -compose over -composite eqn.png
Here DENSITY, FG, and BG are user tunable variables. DENSITY tells convert how many pixels per inch to use when rendering the pdf to a png. This determines how big of a png you have for a given rendered equation. Note that a density of 300 is print quality, 96 is windows monitor standard, and 72 is the mac standard. FG is the color of the equation text in the final image, and BG is the color of the background. Setting BG to none will make the background transparent.
Now for an explanation of the options:
\( -density $DENSITY eqn.pdf -trim +repage \) \ loads the pdf into memore, removes excess background, and saves it into the image stack position 0.
\( +clone -fuzz 100% -fill $FG -opaque black \) \ copies the image at position 0, fills it with the FG color, and saves it at position 1. We clone and fill to ensure that image 1 has the same dimensions as the trimmed image in position 0.
+swap -compose copy-opacity -composite \ uses the black-and-white values in position 0 to determine how transparent pixels are in 1, clears the stack, and saves the result to pos 0. We actually swap positions 0 and 1 to put them in the right order for the -composite operator.
\( +clone -fuzz 100% -fill white -opaque $BG +matte \) \ copies image 0 again and fills it with the BG color, discarding all transparency information.
+swap -compose over -composite eqn.png overlays image 0 onto image 1 and saves result to eqn.png.
And now you have an equation rendered as a png that can be included on any webpage.


When I upgraded ImageMagick from 6.3 to 6.4, your handy convert script stopped working. After some tinkering, this version arose from the ashes to wreak equations upon the HTML pages:
convert \( -density $DENSITY eqn.pdf -trim +repage \) \ \( -clone 0 -negate -background $FG -channel A -combine \) \ \( -clone 0 -background $BG -channel A -combine \) \ -delete 0 -compose dst-over -composite eqn.png
Just trying to give a little back.
Blessings, TwP
You’ll have to filter out the extraneous backslashes from the above snippet of command line goodness. The original was broken across four lines, and the comment formatting swallowed the newlines [sigh].
TwP
Tim’s solution reformatted:
For black text on white backgrounds this solution works as well:
It’s a modified version of Tim’s solution.
forgive me for being clueless, but how do I modify the values of $DENSITY, $FG, and $BG? (so that I can use this command more generically) I think it was something like commandHere $YOURVAR=:value, but I can’t get it to work.
I’d like to use this to generate black text on a transparent background but I’m a bit lost. I tried man convert and my head nearly exploded. Powerful, but would take a bit of time to figure out.
current command: convert \( -density 100 KCL_eqns_1.pdf -trim +repage \) \( -clone 0 -fill black -colorize 100% \) \( -clone 0 -background white -channel A -combine \) -delete 0 -compose over -composite eqn.png
this puts out a png with black text and white background.
I’ve been spending all night trying to get a latex png for a question on a forum. I love how linux can send you off on unending tangents such as this one. :P
Just set them as environmental variables. If you use Bash, you can do:
DENSITY=100 convert …
Windows has its on ways to set and retrieve environmental variables.
Update