Rajesh Kumar

Optimizing life, one day after the next

My new testbed

05 Jun 2005

Latex2PNG for PHP works!

$$ \frac{a_b}{c_d} $$

If I wanted to insert a math equation, I would type something like this: \$\$ \frac{a_b}{c_d} \$\$ Okay, This is just too cool. If I wanted to insert even more cool math code:

$$ x+y = z$$
$$ z+a = b$$
$$\therefore x+y+a=b$$

Some geek math code:

$$\sum_{k=1}^nk=n(n+1)/2$$

Cool, I can even incorporate PHP code within my documents. Like this:

1
2
3
4
5
6
7
8
9
<?php
function php_colorize($data) {
    
preg_match_all('/<\?php.*\?>/si'$data$lat);
    foreach(
$lat[0] as $php) {
        
$data str_replace($phphighlight_string($phptrue), $data);
    }
    return 
$data;
}
?>

What's more, I can also include chemical equations! For example:

$$\frac{1}{2}\,Cu^{2+}\,+\,OH^{-}\,\rightarrow\,\frac{1}{2}\,Cu(OH)_{2}$1.2$

Latex2PNG is a PHP port of my brother's latex2png, originally written in LISP. The algorithm is the same, even if nasty, what with all the hard-coded LaTeX markup. The only difference I guess is the ability to pass the scaling factor within the equation itself. You can also "escape" from latex2png mode by escaping the dollar signs with back-slashes. I haven't cared to implement inline equations. I don't use them anyway. Note that the script uses some file-system functions, notably copy and unlink. Do not contact me if a few dear files of yours begin to disappear from your hard-drive.

You'll need PHP5 for the file_put_contents() function (which you can get from PEAR::Compat anyway).

Grab the source (while it's hot) here.

« New HomepageFri and Sat happenings »

[ about | all posts | subscribe | resume | contact ]