LaTex in WordPress with PHP5.x

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.

Tags: , , ,

8 Responses to “LaTex in WordPress with PHP5.x”

  1. Techno Cake says:

    i still don’t know what is the meaning of latex in this post?

  2. baxissimo says:

    LaTeX is a program for typesetting text. In particular it’s really good at formatting equations. I actually don’t have the plugin enabled on this blog yet, so I can’t show you an example, but you can read more about LaTeX and WordPress here: http://en.blog.wordpress.com/2007/02/17/math-for-the-masses/

  3. Bill,

    Thankyou for posting this.

    I’ve been having exactly the same problem, even down to using Dreamhost as my provider. I made the fix you suggested, but it didn’t work. I also experimented with the double backslash, with logging out of WP, deactivating and reactivating the plugin. The same basic problem persists – I’m still getting lots of “ormulabox” and so on in posts. The strange thing is that grepping for “ormulabox” in the WP install now yields no hits, so I don’t know where the error can be coming from. Any ideas?

  4. baxissimo says:

    @Michael,
    The string ‘ormulabox’ should still appear in wp-content/plugins/latexrender/class.latexrender.php after edits, so if you don’t have that file, or if that file no longer contains the string, then something is amiss. If it’s there but your grep isn’t finding it, then your grep command is probably not right.

  5. Bill,

    I omitted some context. The last thing I tried was downloading the latest version of LatexRender, which seems to have had this problem fixed. At the least, in this version, the formulaboxes etc seem to have been removed from class.latexrender.php, presumably as a way around this problem. I cut and paste the relevant changes to wrap_formula, but still, no luck. The kicker is that formulaboxes remain in the output, despite the fact that grep shows it’s nowhere in the source. This makes me think it’s a caching problem somewhere, but I can’t seem to find where.

  6. A final fact, which I only just noticed: the only formulas affected seem to be ones with an f in them. Other things parse okay.

    I am using version 1.0 of LatexRender, although the file class.latexrender.php has a header which says it’s version 0.8.

  7. Problem solved. I uninstalled everything, and did a completely fresh install, using the latest version. Works fine.

    Thankyou for your help – I’m guessing it was this post that ensured the latest version of LatexRender works with PHP 5.

  8. Ulf Hamster says:

    Hello Bill

    there is general way to handle the escape character problem. You can use PHP’s str_replace function to backslashes, i.e. take the formula string and add for any \n, \r, \t, \v, \f, \\, \$, \”, \[ und \x etc (that are all escape characters) an additional backslash.

    for example. Before passing $latex_formula to wrap_formula($latex_formula), run $latex_formula=latexescapedebug($latex_formula)

    function latexescapedebug($tex){
    $tex = str_replace(“\n”,”\\n”,$tex);
    $tex = str_replace(“\r”,”\\r”,$tex);
    $tex = str_replace(“\t”,”\\t”,$tex);
    $tex = str_replace(“\v”,”\\v”,$tex);
    $tex = str_replace(“\f”,”\\f”,$tex);
    $tex = str_replace(“\$”,”\\$”,$tex);
    $tex = str_replace(“\[“,”\\[“,$tex);
    $tex = str_replace(“\x”,”\\x”,$tex);
    $tex = str_replace(“\ “,”\\\ “,$tex);
    return $tex;
    }

    inside wrap_formula() you might try

    function wrap_formula($latex_formula) {

    $string .= “\begin{document}\n”;
    $string .= “$”.latexescapedebug($latex_formula).”$\n”;
    $string .= “\end{document}\n”;

    }

    I have not tried it so far. if it doesn’t work this will work (it works for me). Add the latexescapedebug inside the latex.php file and add this in the first line.

    function latex_content($text) {
    $text = latexescapedebug($text); //First Line

    }

Leave a Reply