Last month, I have written a program that converts a LaTeX document into a format that is ready to be copied and pasted into the WordPress editor.

I have been using it to post the notes of my cryptography class here, as well as some other posts.

Terry Tao has tested it on a couple of posts. Thanks to his feedback, the current version, while surely bug-filled and very limited, is stable enough to be used by other people. It is now available to anybody who might be interested.

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 e^{-t^2}. 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:

  1. 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.)
  2. 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.)
  3. Simulating displayed equations becomes more cumbersome, and one has to do something like

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

  4. 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.
  5. 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 X and X.
  6. 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.

To get a sense of how it works, and of what is still missing, here is the unedited output of the program given the latex source of my Max Cut paper (the most recent arxiv submission).

About these ads