Converting LaTeX to WordPress

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).

17 thoughts on “Converting LaTeX to WordPress

  1. That’s horrible :(

    Unfortunately the image links with the single quotes are created by WordPress itself, after one (or one’s conversion program) writes something like $ latex … $

  2. Pingback: Notes of Lecture 4 « Error Correcting Codes: Combinatorics, Algorithms and Applications

  3. I find it very hard to read really mathy posts in blog form. What is the advantage to doing things this way, rather than just posting a link to a pdf?

  4. You can indirectly use align environment in wordpress by using \begin{array} \end{array} in wordpress latex. So for instance this’ll work: \begin{array}{ll} 1&\leq 2 \\ &\leq 3\end{array}.

  5. Thanks “H”, this is new, using arrays with more than one column in WordPress used to produce a “formula does not parse” error.

  6. Pingback: LaTeX 轉成 Wordpress « XOO’s

  7. Pingback: Wordpress and Latex « One problem a day

  8. Pingback: LaTeX in WordPress « Eikonal Blog

  9. Pingback: Using LaTeX in WordPress « God, Your Book Is Great !!

  10. Pingback: The core of a cooperative game « Christian Marks

  11. need some help from you..
    in posting mathematical formulae in wordpress..
    cud you please giv ur id? mine is itsshajahan[at]gmail[dot]com

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s