Tuesday, March 24, 2009

Pettry rendered LaTeX equations using PHP

When I was writing a simple scientific webapp, during my undergrads studies, I needed to generate some equations to be shown by the app. I was already familiar with LaTeX equation formatting syntax, so I decided to use this nice peace of software.

So, after "googling" a little, I found the imgtex, written by Koji Nakamaru, which is a fast CGI script, written in perl. What I did, was port it to PHP.

To run it, you must have a LaTeX distribution and the dvipng software both installed on the same machine which you will run the PHP script.

Here is the PHP code:

To use this code, you just have to pass the LaTeX commands through GET to the PHP. For example,

http://localhost/imgtex.php?res=300&cmd=x=\frac{-b\pm\sqrt{-4ac}}{2a}

The res variable sets the resolution for the generated image and the cmd specifies the LaTeX command. This way, the above URL will produce the following image:

2. Hi, do you mind posting more detailed instructions? I don't understand how to use this. What should I write before the PHP script (as it is incomplete at the beginning)? Do I define $res =$_GET["res"];?