LatexRender

Uncategorized — Titus Barik on September 20, 2004 at 5:51 pm

While reading a text on analog and digital networks, I realized that there was absolutely no way to convey or even summarize the information as a standard weblog article. Source code excerpts are one thing, but mathematical formulas and graphs are an entirely different domain. For such tasks, HTML is not at all suitable. Consequently, what I needed was a LaTeX system for entering formulas and having them automatically convert to images, in a very similar manner to Wikipedia. Here’s how I did it. I think you’ll be pleased with the results.

After examining several LaTeX to GIF implementations, such as mimeTex, text2gif, and LatexRender, I decided on the latter because of its rendering quality and the ability to use an actual LaTeX backend. One might think that calling LaTeX from within PHP would incur a large overhead. It does, but LatexRender cleverly avoids re-generating images by caching them with MD5.

LatexRender provides an excellent PHP script, but unfortunately, has a few minor faults and limitations for the kinds of tasks that I wanted to do. Primarily, LatexRender is suited for mathematical typesetting. First and foremost, I needed a general LaTeX interface that could be accessed and simultaneusly coexist within my Text_Wiki entry system. The package also does not generate XHTML compliant code out of the box, or in my case, Wiki code. Thankfully, these issues were not insurmountable.

To change the HTML code that is output simply requires an edit to latex.php. And to generalize the ability of LatexRender to handle any LaTeX document, edit the wrap_formula function in class.latexrender.php.

With the exception of mimeTeX, The standard technique that these programs use is to call latex, convert the resulting dvi file to postscript, and to finally convert the postscript file to an image using ImageMagick. In fact, one could manually do these steps using the following code:

latex --interaction=nonstopmode my.tex
dvips -E my.dvi -o my.ps
convert -density 120 my.ps my.gif

And we’re done. The online journal system now supports embedded LaTeX. We can do mathematical expressions, such as:

and even charts or graphs, through the use of the PStricks family of packages:

With embedded LaTeX, blogging has just become a heck of a lot more fun. The icing on the cake: LatexRender has plugins for all major CMS systems, including WordPress.

2 Comments »

  1. "Blogging has just become a heck of a lot more fun."

    That sums it up well! You don’t have to understand the maths to appreciate the power of LaTeX in a blog entry.

    Comment by Steve Mayer — September 21, 2004 @ 6:09 pm
  2. I wrote the same code as yours with the PStricks packages included, but obtained the wrong-sized images. I would like to know your gs and ImageMagick versions and any other information on the way your latexrender code works with PStricks.

    Comment by Jun — May 8, 2005 @ 11:02 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

titus@barik.net | The Weblog of Titus Barik