LaTeX in WordPress

This:

\displaystyle\frac12 < \left\lfloor \mathrm{mod} \left( \left\lfloor \frac{y}{17}\right\rfloor 2^{-17\lfloor x\rfloor -\mathrm{mod}(\lfloor y\rfloor , 17)},2\right)\right\rfloor

is called Tupper’s self-referential formula. If you graph it over particular values of x and y, it makes a picture of itself, and that is quite neat. Follow the link for details.

And this is a property of the unit impulse:

\displaystyle\int_{-\infty}^t\delta(\tau)d\tau = u(t)

But that’s not really the point. The point is the mathematical notation; you can integrate \LaTeX mathematical notation in your WordPress blog very easily, using Steve Mayer’s LatexRender plugin.

And the point here is that you can install that plugin even more easily and correct a nasty bug in the offset beta functionality, using my LatexRender installation script (or the BSD version).

Go to the wp-content/plugins subdirectory of your WordPress installation, and then either:

wget http://fugato.net/wp-content/install-latexrender.bash
bash install-latexrender.bash

or, if your webserver is running BSD (or anything with the same kooky version of sed as my hosting provider’s BSD installation):

wget http://fugato.net/wp-content/install-latexrender.bsd.bash
bash install-latexrender.bsd.bash

and with any luck, the script will set everything up right. Then you’ll just need to activate the plugin LatexRender in your WordPress admin panel, and try it out by putting something like [tex]e^{i\pi}=-1[/tex] in a blog entry. That should render the classic e^{i\pi}=-1.

This is tested working fine in WordPress versions 2.0 through 2.5.1 (the current stable version, which this blog is running).

The script assumes that your blog is located at the root of your website. If it isn’t, then you must provide the base path as an argument to the script. E.g. if your blog is at http://o.com/all/ye/faithful/, then you must run the script like this:

bash install-latexrender.bash /all/ye/faithful

and it should work right.

Some formulas may be slightly misaligned with the text baseline. You can work around this by manually surrounding them with stuff like <span style="vertical-align: -0.5px;"> ... </span> on a case-by-case basis (assuming you edit your blog entries in straight HTML, not the rich text editor).

27 Responses to “LaTeX in WordPress”

  1. Skúli Says:

    This is awesome, totally f**king great, I and hardly ever use that word unless something really warrants it. I’ve just gotta try this out and I can’t wait to get my admin to run your script, so I’m trying it here: [tex] F : \mathbb{R}^n \longrightarrow \mathbb{R} ; \boldsymbol{x} \mapsto F( \boldsymbol{x} [/tex].

  2. GÞB Says:

    Hehe, doesn’t work in comments unless you enable it manually in the plugin code. My script doesn’t do that. But it’s straightforward to do manually; the plugin readme explains how.

  3. Skúli Says:

    I read the joke, serving as the introduction to the LaTeX Renderer good new… and its starting to bug me. Tucker self-referential formula? It says it makes a picture of itself, do you mean the formula? Does it make a picture of the formula text? And further, “if you plot it over particular value of [tex] x,y [/tex]“, which particular values are they?

    Sorry for being so pedantic, but I’d really like to try it, and I’ve got a lot of time on my hands… hehe

  4. GÞB Says:

    If you have a lot of time on your hands, surely you had time to notice ;) that I provided a link to further info about Tupper’s self-referential formula. It’s no joke, it’s real and quite neat.

  5. Dave Says:

    Does the plugin work with wordpress 2.1?

  6. GÞB Says:

    Yep; this blog is running Wordpress 2.1.

  7. Matthijs Sypkens Smit Says:

    I got the following error on my Debian installation:
    install-latexrender.bash: line 31: conditional binary operator expected
    install-latexrender.bash: line 31: syntax error near `=~’
    install-latexrender.bash: line 31: `if [[ `pwd` =~ ".+"${PLUGINS_REL_PATH}'$' ]]’
    bash-version: GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
    I uncommented that particular section and then it seemed to work (since I was in the right directory).

  8. GÞB Says:

    Right, I guess you need a bash that supports the regexp match operator.

    Could you possibly patch it so that it makes the same check in a way that works in your bash? I would incorporate that change. I’d rather not remove the check altogether; I want to keep this script on the cautious side.

  9. Matthijs Sypkens Smit Says:

    replacing line with
    if [[ "${PWD: -18}" == "${PLUGINS_REL_PATH}" ]];
    works for me. Don’t know how portable it is or what the risk that this construction doesn’t work with yet other setups.

  10. GÞB Says:

    Hm, are you sure about that? From the Bash reference manual:

    ${parameter:-word}
    If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

    That doesn’t sound like it does what we want here.

  11. GÞB Says:

    Oh sorry, you’re doing substring expansion, since the second operand is numeric:

    ${parameter:offset}
    ${parameter:offset:length}
    Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter starting at the character specified by offset.

    That sounds about right. I’ll try it out and make the change.

  12. GÞB Says:

    Matthijs’ suggestion was applied and made robust to changes in PLUGINS_REL_PATH; the line is now:

    if [[ "${PWD: -${#PLUGINS_REL_PATH}}" == "${PLUGINS_REL_PATH}" ]]

    so now there are no known (to me) issues with this script.

  13. Suman Says:

    I am getting the following error message! Any clue?

    Warning: copy(a25f9ada91d715ec1a4c89d961a729ff.gif): failed to open stream: No such file or directory in /home/sumansan/public_html/dynamicequations-org/wp-content/plugins/latexrender/class.latexrender.php on line 314

    Warning: unlink(/home/sumansan/www/dynamicequations-org/wp-content/plugins/latexrender/tmp/a25f9ada91d715ec1a4c89d961a729ff.gif): No such file or directory in /home/sumansan/public_html/dynamicequations-org/wp-content/plugins/latexrender/class.latexrender.php on line 338

  14. GÞB Says:

    Nope, no clue, sorry. :) I just made the installation script; you could try asking the author.

  15. Steve Says:

    I am the author. Suman: it looks like the gif is not being created. If you aren’t getting any other error messages it could be because ImageMagick can’t find Ghostscript. Have a look at http://sixthform.info/steve/wordpress/index.php?p=34 for how to put this right.

  16. LightShadow Says:

    Hello, Sir
    I try to implement wp-latexrender to bbpress by rewrite some parameters. But latexrender alwaly recognize
    “\ ” as ”\\” for an example if I type [tex]\Sigma[/tex] then the rendered tex file will show
    …..
    $\\Sigma$

    Can you take a glance about that ?

  17. GÞB Says:

    I just made the installation script. You could try asking the latexrender author, Steve Mayer, but my (blind) guess is that the backslashes are doubled by bbPress itself, not by latexrender. You probably need to find a way to tell bbPress not to do that — or add something to latexrender to unescape the text. It is probably not too hard, if you are willing to get into it.

  18. Steve Says:

    I agree that doubling slashes must be done by bbPress, presumably for use in its database. If you can’t stop bbPress doing it then in latex.php try:
    after the line
    $latex_formula = $tex_matches[1][$i];
    add
    $latex_formula = stripslashes($latex_formula);
    and see what happens.
    Alternatively (but not both at the same time ) in the same place you could try
    $latex_formula = str_replace(”\\”,”\”,$latex_formula);

  19. LightShadow Says:

    Steve, Thank you very much, It works !!

  20. Suresh Says:

    Hi,
    thanks for the script. One question I have is that when I finally render latex, I don’t get an inline image like yours, rather I get a “raised” image with a drop shadow, which is ok, but doesn’t look right for inline things. Do you know what might be causing that ?

  21. GÞB Says:

    No, I don’t know what might cause a drop-shadow … does it look like that in all browsers? Can you post a link to it? My first thought is browser weirdness.

  22. Adeel Khan Says:

    Suresh: the drop-shadow is probably caused by something in your theme’s CSS. Maybe you could post a link…

  23. Chris Williams Says:

    The script currently fails to fetch the plugin. The path to the wp-latexrender zip file seems to have changed from
    http://sixthform.info/steve/wordpress/uploads/wp-latexrender.zip
    to
    http://sixthform.info/steve/wordpress/wp-content/uploads/wp-latexrender.zip

  24. GÞB Says:

    Thanks — updated.

  25. Rich Says:

    I… I think I love you. This is incredibly handy; thanks!

  26. pelesl Says:

    I just started hosting a series of blogs for my projects through Dreamhost.com, which makes it a snap to install WordPress. I searched for how to put LaTeX in entires and ended up here. I used your script and it worked on the first try (well, second, but the first time I pointed it to the wrong directory). Fantastic work.

    Also it was great to find out about the self-plotting function. Fascinating.

  27. Steve Says:

    Does that mean that Dreamhost.com has LaTeX installed or is that only on dedicated servers? I’m sure that many users would be interested in a host that offers LaTeX.