Thursday 1 April 2010

(La)TeX to PNG

I've had a bit of a U-turn since my previous blog post on the subject of MathML. As my brother kindly pointed out, the best (in terms of compatibility at least) solution for displaying complicated mathematical equations in a web browser is simply to use images.

The reason I had previously rejected this approach is the added complexity involved in producing these image files. But after weighing up the advantages and finding myself with some free time in which to install Cygwin, this problem has now largely been mitigated.

For the interested, here are the technical details. You're going to need some kind of TeX installation. If you're a Windows user, I'd recommend using Cygwin and installing the various tetex packages. You'll also need ImageMagick to handle the image file conversion.
  1. Write your equation in TeX format in the following template:

  2. \documentclass{article}
    \pagestyle{empty}
    \begin{document}
    % equation goes here: $$  $$
    \end{document}

  3. Lets assume you've saved your TeX file as eqn.tex. Next compile the TeX to DVI using texi2dvi:

    texi2dvi eqn.tex

  4. Then convert the DVI file to encapsulated PS format:

    dvips -E eqn.dvi

  5. Finally convert the PS file to a PNG (or other image format) using Image Magick:

    convert -density 200x200 eqn.ps eqn.png
Credit due to Micahel Mazack's website for pointing me in the right direction. As usual, if you run into any difficulties feel free to leave a comment. And here's an example of the kind of results we get:


No comments:

Post a Comment