What is the point of this program?
WordPress offers a very simple way to write a mathematical formula in a post, one writes “$ latex e^{-t^2}$”, and out comes . It was for this convenience that in theory moved from Blogger to here.

Anyone who has tried writing a relatively long mathematical exposition on WordPress, however, has faced various inconveniences, including:

The online editor one has to use to write a post is unsuitable to write anything longer than a couple of hundred words. The editing window is tiny, saving and previewing is sometimes very slow, and there are some design flaws. (Saving a post makes the editor forget where you are in the post and resets the cursor to the beginning.)

For inline mathematics, one has to always write $ latex . . . $, instead of $ . . .$, and it is easy to forget to do so. Previewing a post, finding such mistakes, correcting them, and saving and previewing again is a pain. (See above point.)

Simulating displayed equations becomes more cumbersome, and one has to do something like

<p align="center"> $ latex \displaystyle . . .$ </p>

Simulating numbered displayed equations and referring to them is really bad, when one is used to the convenience of LaTeX. The numbering and the referring has to be done by hand, and if one rearranges the exposition, the numbers (and, more annoyingly, the references) have to be changed by hand.

Some WordPress themes (including the one I use), typeset LaTeX equations in gray. It is possible to make them black by adding &fg=000000 at the end of every LaTeX equation. Note the difference between and .

Simulating something like \begin{theorem} ... \end{theorem} suffers from similar problems (manual numbering and referring) and one has to be careful to maintain a uniform style.

When posting lecture notes for a course, there is the additional problem that one needs to convert the posts back to LaTeX if one wants to collect the notes into a single document and make it available online (as I have done for some past complexity courses).

There is, of course, an obvious solution: write mathematical posts in a (subset of) LaTeX, and then use a conversion program to turn the LaTeX into a format suitable for cutting and pasting into WordPress.

This way

it is easier to later package the mathematical content in the form of printable lecture notes;

the post can be written with a nice text editor, and a partially written post can be easily previewed at any time;

there is the convenience of latex, including automatically numbered equations and theorems, and symbolic references via \label and \ref.

Since this is a problem faced by anybody who writes math in WordPress, I assumed that such a program had been already written (probably several times) but I could not find one after extensive googling.

So I decided to write it myself.

Since I did not have a lot of time to devote to this project (if I were not going to have a working first version in a day I was going to give it up), I do not remember anything about compilers (last studied 15 years ago), and, to put it mildly, I don’t have a lot of programming experience, the final product is not coded in a particularly elegant way. It does, however, more or less work.

It handles LaTeX macros that do not involve parameters. It allows any set of theorem-like environments, which can be numbered with any combination of counters. (For example Theorems and Lemmas can share the same counter, but Remarks are numbered independently.) It recognizes math environments such as $…$, $$…$$, \[…\], and \begin{equation}… \end{equation}. (It does not recognize eqnarray and align, which are not supported by WordPress. In a future version, I plan to support eqnarray via HTML tables.) It support LaTeX tables defined via tabular, and it supports \label{…}, \ref{…} and \eqref{…}, which work in the expected way. A quirk of my primitive parsing is that {\em …} and {\bf …} cannot be nested, and there can be no curly bracket in their scope.

17 thoughts on “Converting LaTeX to WordPress”

LiveJournal’s syndication (its RSS internal RSS aggregator so that LJ users can see your posts on their friend-lists) is barfing on the single quotes in your html img tags, or maybe on the comma after the title parameter in these tags — see http://syndicated.livejournal.com/luca_feed/ for examples. Any chance of a fix?