Mathematics in a Blog Post

As some of you know, I was trained as a mathematician. It's odd, then, that my posts don't have more mathematics in them. The reason for that is that this blog is mostly about computers and development so there's not a lot of high level mathematics to discuss. What mathematics I do include are usually things like polynomials that are easily handled with sub- and superscripts.

Still, from time to time I have wanted to include something a bit more complicated but could never figure out how to integrate the math in a visually pleasing way. My two posts on Shamir's Secret Sharing finally motivated me to figure out how to offer typeset mathematics on the blog page.

I'm abashed that it took me so long. The process is so easy that I really have no excuse for not doing it years ago. Here, for others wishing to have mathematics in their blogs, is what to do. This post is mainly aimed at the Org mode → org2blog → WordPress workflow that I use (see here for details) but I've tried to indicate how other workflows can also integrate mathematics into a blog post.

There are two parts to the problem:

  1. Getting the mathematics to display on your blog
  2. Getting the mathematics to display locally so you can easily preview the result

The easiest way to address both problems is with MathJax. It's a bit of JavaScript that gets downloaded to the client's computer and takes care of typesetting the mathematics. All this happens transparently so your blog readers don't have to do anything. You don't have to do much of anything either. The folks at the MathJax project maintain the MathJax CDN, a network of computers that will serve the JavaScript for you. Thus there is no need to have your own local copy (although that's possible if you like).

The easiest way to get this working on WordPress is to install a plugin (although even this isn't strictly necessary). I'm using LaTeX for WordPress but there are several others. The MathJax site has instructions for using MathJax with several common blogging platforms or how to add it to your theme file if you'd rather do that.

In my case, I installed the plugin and everything just worked1. To get mathematics you simply mark up your LaTeX with \(...\) for in-line expressions and \[...\] for displayed expressions. For example,

\[f(a)= \frac{1}{2\pi i}\oint_{\Gamma} \frac{f(z)}{z-a}dz\]

yields the beautiful Cauchy Integral Formula

f(a)= \frac{1}{2\pi i}\oint_{\Gamma} \frac{f(z)}{z-a}dz

To get the typesetting locally, you have to arrange for the MathJax JavaScript to be loaded. With Org-mode, I simply added

#+HTML_HEAD: <script type="text/javascript"
#+HTML_HEAD:  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js">
#+HTML_HEAD: </script>

near the top of the source file. If you're using a different system (why, for goodness sake?) you have to arrange to get the <script> markup into your generated HTML. Once you've done that the mathematics should appear locally when you preview your post.

Very simple, no? As I say, I'm abashed that it took me so long.

Footnotes:

1

If you're using org2blog to publish your Org mode blog posts, you need to turn off org2blog's automatic translation of the mathematics markup with

(setq org2blog/wp-use-wp-latex nil)

This entry was posted in Blogging and tagged , . Bookmark the permalink.
  • I am a mathematician too that uses org-mode to put content on the web. I would be interested to hear if you have any ideas to export theorems in a nice way.

    For example, currently in beamer export there is an option to include theorems in org markup. It would be great if they could also be made to export to html, and even better if they could be numbered and referenced.

    Thanks for the tip about including mathjax per document. In my octopress blogs, I define some code to load it for all posts, even those that don't include math. That is certainly inneficient.

    • jcs

      I haven't needed that so I don't know. I just messed around with \newtheorem but MathJax didn't recognize it. Of course, I didn't try very hard so maybe there's a way. Try asking on the Org mode list. If there's a way, someone there will know.

  • Yes, MathJax is really nice. Out of curiosity: what did you specialize in in maths?

    • jcs

      Functional Analysis. My thesis area was approximately Functional Analysis->Banach Spaces->Operator Theory.

      • i would love to see your math posts now and then

  • Phil

    Maybe I've misunderstood, but it sounds like this approach requires readers to have Javascript enabled?

    Surely you want the end result to be inline MathML markup (which I'm fairly sure any modern browser will render).

    • Thanks to the Quick Javascript Switcher Google Chrome plugin, I can easily see that MathJax falls back to an image such as this one in the absence of Javascript.

      • jcs

        LaTeX for WordPress is doing that. If it can't load the MathJax JavaScript or JavaScript is disabled, it builds images on the fly.

        • Phil

          That's pretty cool, then :)

  • the "type=" is optional these html5 days. most sites don't include it.

  • Michael

    This project just came out a day or two ago: http://khan.github.io/KaTeX/

    • jcs

      Yes, I saw that yesterday. It looks pretty amazing.

  • What version of WordPress are you using?

    I ran into this https://github.com/punchagan/org2blog/issues/160.

    Perhaps it is time to check out the latest release of wp and org2blog.

    • jcs

      I'm running WP 4.0 and version 0.5 of org2blog/wp. I should probably update org2blog/wp. I've been running this setup with several versions of WP and maybe 1 or 2 versions of org2blog/wp.