Archive for July, 2008

LaTex in WordPress with PHP5.x

Thursday, July 31st, 2008

I’ve been using the wp-latexrender plugin on my WordPress blogs on DreamHost here for a while now.  The other day something prompted me to switch my DreamHost account to use PHP5 instead of the default PHP4.  Today I come to work and find equations in new posts are all full of garbage text with words like “ormulabox” and “ormulawidth” and “ormulaheight”.

Turns out PHP5.2 added “\f” as a special escape sequence.  So the php code that was working fine with LaTeX commands like “\formulabox” in double quoted strings, suddenly started breaking.  The \f’s became formfeeds, and what’s left (“ormulabox”, etc.)  was being interpreted by LaTeX as just regular words to put in the output.

So you could fix it by just turning those \f’s into \\f’s, but that would break latexrender on PHP4 installs.

So the fix to keep things working with both PHP4 or 5 is to put the bulk of those strings in single quotes.   So replace the wrap_formula function in wp-content/plugins/latexrender/class.latexrender.php with the following:

    function wrap_formula($latex_formula) {
        $string  = "\documentclass[".$this->_font_size."pt]{".$this->_latexclass."}\n";
        $string .= '\usepackage[latin1]{inputenc}' ."\n";
        $string .= '\usepackage{amsmath}' ."\n";
        $string .= '\usepackage{amsfonts}'."\n";
        $string .= '\usepackage{amssymb}'."\n";
        $string .= '\pagestyle{empty}' ."\n";
        $string .= '\newsavebox{\formulabox}' ."\n";
        $string .= '\newlength{\formulawidth}' ."\n";
        $string .= '\newlength{\formulaheight}' ."\n";
        $string .= '\newlength{\formuladepth}' ."\n";
        $string .= '\setlength{\topskip}{0pt}' ."\n";
        $string .= '\setlength{\parindent}{0pt}' ."\n";
        $string .= '\setlength{\abovedisplayskip}{0pt}' ."\n";
        $string .= '\setlength{\belowdisplayskip}{0pt}' ."\n";
        $string .= '\begin{lrbox}{\formulabox}' ."\n";
        $string .= "$\\ ".$latex_formula."$\n";
        $string .= '\end{lrbox}' ."\n";
        $string .= '\settowidth {\formulawidth}  {\usebox{\formulabox}}' ."\n";
        $string .= '\settoheight{\formulaheight} {\usebox{\formulabox}}' ."\n";
        $string .= '\settodepth {\formuladepth}  {\usebox{\formulabox}}' ."\n";
        $string .= '\newwrite\foo' ."\n";
        $string .= '\immediate\openout\foo=\jobname.depth' ."\n";
        $string .= '    \addtolength{\formuladepth} {1pt}' ."\n";
        $string .= '    \immediate\write\foo{\the\formuladepth}' ."\n";
        $string .= '\closeout\foo' ."\n";
        $string .= '\begin{document}' ."\n";
        $string .= '\usebox{\formulabox}' ."\n";
        $string .= '\end{document}' ."\n";

        return $string;
    }

And that does the trick.  This was a problem as of Aug 1, 2008, with version v0.8 of class.latexrender.php.

Hello world!

Tuesday, July 29th, 2008

Hello, this blog, in contrast to my other blog, will be about tech stuff, research ideas, and other geeky stuff.