PHP\LaTeX, LaTeX renderer

Overview

PHPLatex is quick and dirty PHP script that makes TeX rendering easier. It was initially made for latex.knobs-dials.com.

Does have a few requirements, though you'll likely meet all of those on modern *nix-with-texlive installations.
Aside from the fact it fundamentally is a bit hackish, it seems to work well enough.

At the core it just invokes latex, dvips, and convert.
And stores the resulting image, so that asking for the same TeX will generate once, then read from disk. This means you can 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.

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 are encouraged. Contact address: scarfboy$\!\ @\!~$gmail.com

Ideas / requests for comments

This thing fulfils my own needs, so I am not actively working on it. Suggestions are welcome, of course.

I am considerering having it always render 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.

Download

Examples

   <? print texify('wh^{e^{e^{e^{e^{e_{e_e}}}}}}'); ?>
   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 (though you can get away with $$ as-is). PHP does not 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}$$
     "); ?>

        $$\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} $$

TeX text in HTML text

...should work well enough for normal-sized text. PHPLatex 0.4 has some simple logic that adjusts the HTML vertical offset of the image based on obvious ascenders, descenders (needs more work and is fiddly at best, but seems to work decently so far).

For example: Paraglider, ooo, o,o, text, Fuji dojo, (o-w), o\#3, some normal text, $\int{x}$ `$dx'$', w/o/o, mojo \{1,2,4\}, some more normal text, www, Jojo [brick] oo, katie80, Times in TeX, and this is normal text again.

Colors, Resolution

The function function definition is:
    texify($texstring, $dpi='90', $r=0.0,$g=0.0,$b=0.0, $br=1.0,$bg=1.0,$bb=1.0,$extraprelude="", $trans=FALSE)

The default dpi is 90. Capped at 300 to be nice to resources, but you can change that.
The default foreground is black, and background is white.
extraprelude is primarily useful for inclusion of extra packages.
trans controls whether the background color will also be transparent - this is a semi-broken experiment at the moment, though

While you can use any coloring TeX you want, the texify() call specifically lets you set the text and background colors in RGB (uses the color package).

<? # Orange on white and dark gray
  print texify('$\sqrt[3]{2}$', 200, 1.0,0.6,0.1, 1.0,1.0,1.0);
  print texify('$\sqrt[3]{2}$', 200, 1.0,0.6,0.1, 0.3,0.3,0.3);
?>

$\sqrt[3]{2}$$\sqrt[3]{2}$

Tables, packages

The following demonstrates how to include packages, here pstricks and colortab:

 <?
  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}");
 ?>

\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}