LaTeX Equations and Graphics in PHP

Make the difficulties of displaying mathematical equations on the Web a thing of the past by embedding LaTeX content in your pages.
Plotting Examples

Though LaTeX is a mathematical typesetting powerhouse, it also is capable in other arenas with the help of packages such as PSTricks. These plots are provided courtesy of Herbert Voss. On his Web site (see Resources), you can find further examples of using PSTricks to test the LaTeX rendering system. Getting some of his more-advanced examples to display correctly, however, may require considerable effort.

Figure 5. Example: Plot of 10x ex, and 2x

   linewidth=0.5pt]{-4}{0.9}{10 x exp}
   linewidth=0.5pt]{-4}{3}{2 x exp}
   linewidth=0.5pt]{-4}{2.05}{2.7183 x exp}
\rput(-4,-1){\color{white}bounding box\normalcolor}

Figure 6. Example: Ceil Function

     (\i,\i)(! \i\space 1 sub \i)}%

Available Implementations

Several implementations of LaTeX renderers are available on the Web today, some of which work better than others. Steve Mayer, for example, now maintains Benjamin Zeiss' original LaTeX renderer for PHP. Mayer also has written several plugins for common Weblog systems, including WordPress. If you want a pluggable solution for your site, this is the one I recommend.

Additionally, John Walker provides textogif, a Perl program that uses the LaTeX2HTML tools to render images in either GIF or PNG format by way of CGI. Finally, John Forkosh provides mimeTeX, written using C through CGI. Its advantage is that it does not require LaTeX or ImageMagick but does so at the expense of rendering quality.


Integrating LaTeX with your wiki or Weblog at first may seem like a daunting task. Once you get the hang of it, however, you'll wonder how you ever lived without it. Using this model, you also can see how other languages might be embedded within PHP in addition to LaTeX. Other ideas to consider include using Gnuplot to generate plots, Octave to evaluate complex expressions or POV-Ray to render 3-D scenes.

Today, the topics represented by the Weblog community largely are disproportionate. Indeed, many technical writers outside the field of programming have stayed away from Weblogs simply because the means to convey their ideas easily do not exist. I hope that the use of LaTeX rendering systems for the Web will bridge this critical gap.

Resources for this article:

Titus Barik is an IT consultant for small businesses. He's also an active Weblogger and technical bookworm. You can visit his Weblog at



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Another aproach to LaTeX equations with PHP

Filipi Vianna's picture

Some time ago I had faced the same problem, but I didn't use the same rendering steps. Instead of rendering to PS and then converting to PNG, I had used dvipng to convert the DVI directly to PNG.

The code is at my personal blog.

Hope it also helps somebody.

Filipi Vianna

A bit of trouble

Skylar Saveland's picture

I have been looking for something like this off and on for months but I am a programming newb with most experience in python; this article has me crash-coursing php.

I think I am having problems naming the .php files, placing them in the right place and/or setting the correct permissions. I know exactly where convert, latex and dvips are, that is not the problem. I have put everything in the document root in separate files, each listing.

wrap.php7870l3.qrk I have just named wrap.php, I can't find any logic for the '7870l3.qrk' part.

If I name the second listing/file 'render.php' I get the follwing error:
Warning: require(render.class.php) [function.require]: failed to open stream: No such file or directory in /var/www/render_example.php on line 26

Fatal error: require() [function.require]: Failed opening required 'render.class.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/render_example.php on line 26

If I name the second listing 'render.class.php' I get a different error:
class render { var $LATEX_PATH = "/usr/bin/latex"; var $DVIPS_PATH = "/usr/bin/dvips"; var $CONVERT_PATH = "/usr/bin/convert"; var $TMP_DIR = "/usr/home/biouser/texwebtmp"; var $CACHE_DIR = "/usr/home/biouser/texwebtmp/cache"; var $URL_PATH = ""; function wrap($text) { ... } function transform($text) { ... } function render_latex($text) { ... } }
Fatal error: Class 'render' not found in /var/www/render_example.php on line 33

Something is going on here... I would b so excited and grateful if i could get this to work!!!

uninformed question

matt's picture

This looks exactly like what I need. I am trying to set up a simple wiki to collaborate with co-writers, and am trying to find how to embed latex capabilities. I am a rookie in programming though, and cannot figure out some things. Specifically, "You need to let PHP know where your tools are located and provide a directory where PHP can write temporary files and store its cache."==> What tools? I see I need latex and dvips etc., but what exactly are these as tools? I use latex, but don't know exactly what I need to upload to the server.



David Sankey's picture


I'm starting to implement this into a math worksheet website I'm (slowly) building. It's superior to html formatting because it doesn't vary from browser to browser. I had to alter it a bit because I'm making worksheets for print and the images weren't printing well. I changed the density to 300. Then, I get the image height using "identity" and reduce it by 40% using the height attribute in the image tag.

The images on screen are a bit choppy but the prints look great. My students are sure to appreciate it!

Would you like me to post a link or give you other credit?

Thanks a bunch!

Dynamic Base URL

Carlos S.'s picture

It may be a good idea to add a function to get the base URL instead of hard writing it on the source code.
it may be something like:

$base_url = 'http' . (isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] == 'on' ? 's' : '' : '');
$base_url .= '://' .$_SERVER['HTTP_HOST'];
if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) {
$base_url .= "/$dir";
$URL_PATH= $base_url . "/cache"


Web CAS, another LaTeX+PHP on Apache

Anonymous's picture

Although I has not read the interesting article, a project of SourceForge, WMI (Web Mathematic Interactive,, may be the subject that readers are interested. WMI integrates almost the powerful CAS in Linux world, e.g. Maxima, Octave, Gnuplot and Maple etc. within LAMP envirnment. Don't forget to visit the official site.


summentier's picture

How did you know that the article was interesting if you hadn't read it? I did read it and it was great help!