, LaTeX rendererPHPLatex is fairly simple PHP script that makes TeX rendering easier, initially made for latex.knobs-dials.com.
At the core it just invokes latex, dvips, and convert.
Since each such execution can take a few seconds,
it caches the resulting images, which means that later requests for exactly the same TeX are served from files, making it low-cost (mostly a little IO)
to leave the function call in the PHP, which should make both using TeX on a page and changing it later a low-brainer, and easy on your resources.
It does have a few requirements, though you'll likely meet all of those on modern *nix-with-tetex installations.
Aside from some details like that, and some initial setup -- and the fact it just is a bit of a hack -- it seems to just work.
Note that external invocation so is always potentially unsafe in an untrusted environment (the utilities will run as your webserver user).
For a safer but rather more restrictive variation, check the ocaml parser that mediawiki (as in wikipedia) uses.
Released under the GPL. Comments, praise, complaints, bugs, fixes and whatnot all encouraged.
Contact address:
As I don't consider this a very serious script (heck, it's PHP) and it currently fulfils my needs, I am not actively working on it. I probably will work on a feature suggestions I think are neat, so those are welcome.
I am considerering rendering in higher resolutions to have images be sharper in printing, although I would have to test whether this can be done elegantly and check that nothing reacts weirdly (browsers, OS settings, etc.). If anyone has played with something like that, I welcome your notes.
I may have messed up the transparency for PNGs and the trimming for GIFs. I'll look at this some time.
<? print texify('wh^{e^{e^{e^{e^{e_{e_e}}}}}}'); ?>

Since you write inside PHP strings, backslashes are interpreted, so double them. Also, PHP wants to interpret dollar characters as $variables, so escape those too -- PHP doesn't seem to allow anything nicer for either of these cases.
<?
print texify("
$$\\begin{align}
(a+b)^3 & = (a+b)^2(a+b)\\\\
& = (a^2+2ab+b^2)(a+b)\\\\
& = (a^3+2a^2b+ab^2) + (a^2b+2ab^2+b^3)\\\\
& = a^3+3a^2b+3ab^2+b^3
\\end{align}$$
"); ?>

...should just work. The following are test strings for
the automatic vertical-image-alignment feature (which still needs work) in 0.4:
,
,
,
,
,
,
, some normal text,
,
,
, some more normal text,
,
,
,
,
and this is normal text again.
You can specify the text and background colors in RGB (uses the color package). The background color is transparent in the PNG (though IE6 and before had very shoddy PNG support), so you can fade to various background colors if you want., for example
<? print texify('$\sqrt{2}$', 90, 1.0,0.6,0.1, 1.0,1.0,1.0); ?>
<? print texify('$\sqrt{2}$', 90, 1.0,0.6,0.1, 0.0,0.0,0.0); ?>
| On white | ![]() ![]() |
| On dark gray: | ![]() |
The 90 there is the rendering resolution; 90 is the default, and you can use up to 300, to help avoid cases
where the raster step takes unreasonable CPU and memory. (You could change the cap of 300 in the source, of course)
This is 300dpi:
The following uses pstricks and colortab, and demonstrates how to include packages:
<? print texify("
\\definecolor{lightergray}{gray}{.875}
\\newcommand*\\lightergray{\\color{lightergray}}
\\begin{tabular}{|lc|r|}
\\hline
\\LCC \gray & \lightgray & \lightergray \\\\
rabbit & 12 & sold \\\\
frog & 3.5 & pending \\\\ \\ECC
\\hline
\\end{tabular}",
90,
0.0,0.0,0.0,
1.0,1.0,1.0,
"\\usepackage{pstricks,colortab}"); ?>

(...for the development version)
README for PHPLaTeX
Renders LaTeX into images by calling LaTeX itself from PHP. Rather a hack that way...
Requirements
- PHP (>=4.3.0 because it uses sha1())
- tetex and ghostscript (for latex, dvips) (tetex should be a relatively new setup;
this uses packages color, amsmath, amsfonts, amssymb,
and the extarticle document class)
- imagemagick (for image conversion)
Setup/Installation
- Have latex (probably tetex), imagemagick, ghostscript installed.
(paths to these are currently hardcoded and absolute; check them against e.g. "which dvips" )
- Put phplatex.php somewhere from which you can include it.
- In each directory you will be *calling* the script from, create subdirecties 'tmp' and 'images'
and make that PHP (or rather, apache) can create files in those directories.
It should be enough to do:
# mkdir tmp images
# chown apache:apache tmp images
(...whatever user/group applies to your system; may also be www-user, www-data, apache2, or such)
Use
- Include it with something like include('phplatex/phplatex.php')
- call texify(texstring, dpi, r,g,b, br,bg,bb, extraprelude)
Only the tex-string is required, which is pasted into a template LaTeX document.
This means you can use almost arbitrary TeX, including but not limited to '$math$' strings.
That function will return an string containing <img src="...">,
which you'll probably PHP-print into the document to show the image.
Maintenance
- You can empty the tmp and image directory at will, since tmp sometimes contains a few leftovers,
and images may contain old images that are never accessed.
Features
- Allows specifying rendering resolution (default:90, capped at 300 for memory reasons)
- Allows use of colors. (default: black on white, specifically 0.0,0.0,0.0 on 1.0,1.0,1.0)
- Allows inclusion of extra TeX packages (that is, extra prelude text)
- Caches generated images (based on document string) so asking for already-generated TeX is
just a filesystem check; you can leave the texify() calls in the PHP as this is pretty cheap.
- Generates PNGs. (Could also generate GIFs; I seem to remember a minor practical problem with it)
- Relies on image trimming instead of trusting dvips' bounding box.
- The background color is made transparent in the png. Since text is antialiased, small differences
between this and your actual html background color don't matter much.
- CSS-based image lowering to have inline text show up halfway decently.
Caveats
- Initial generation takes a while, perhaps a second per image. (hence the image cache)
If you hit the PHP time limit, you may need to refresh a few times before everything is built.
- Fails for very large images (hence the resolution cap)
- Fails when you include TeX that typesets as two pages or more (try \small and/or a higher resolution)
Arguables
- You can use arbitrary tex. This is a feature as well as a security issue.
Know exactly what this means and/or use at your own risk.
Like (non-safe-mode) PHP, the processes can do everything the web server user can.
- Requires recent TeX version as it uses extarticle and includes color, amsmath, amsfont, and amssymb.
- Uses \nonstopmode, meaning latex will fix errors it can, so you can get away with some bad TeX.
- The (default) Computer Modern fonts don't render as well at relatively low resolutions as, say,
pslatex fonts (Times, Helvatica, Courier), due to thickness and antialiasing, so change fontset to taste.
Sharpening helps only a little, and can look worse when colours or shades are involved
(I tried -unsharp 1x1+1+0).