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.