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

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 comments

Comments feed for this article

February 21, 2009 at 8:39 pm

D. EppsteinLiveJournal’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?

February 21, 2009 at 8:44 pm

lucaThat’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 … $

February 21, 2009 at 9:21 pm

Notes of Lecture 4 « Error Correcting Codes: Combinatorics, Algorithms and Applications[…] to Luca Trevisan’s program to convert LaTeX to wordpress suitable formet, the notes are also produced below. (Citations are […]

February 21, 2009 at 9:24 pm

noneI 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?

February 21, 2009 at 10:33 pm

HYou can indirectly use align environment in wordpress by using \begin{array} \end{array} in wordpress latex. So for instance this’ll work: .

February 22, 2009 at 4:20 am

rgrigI’ve been using hevea in the past: http://hevea.inria.fr/

February 22, 2009 at 5:37 am

dweebydoofusThanks a lot for all of your hardwork. I’ll be using your program too and tell you how my experience is going.

February 22, 2009 at 11:59 am

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

February 22, 2009 at 1:42 pm

dweebydoofusI found out that in order for the array environment to work we have to write all of it with no line breaks. In this page: http://kogler.wordpress.com/2008/03/21/latex-multiline-equations-systems-and-matrices/ there a lot of good examples

March 15, 2009 at 10:56 pm

LaTeX 轉成 Wordpress « XOO’s[…] 下載與詳細資料：http://lucatrevisan.wordpress.com/2009/02/21/converting-latex-to-wordpress/ […]

March 30, 2009 at 10:42 pm

sssCOOL……………..

June 24, 2009 at 3:24 pm

Wordpress and Latex « One problem a day[…] an excellent program for converting raw tex into WordPress readable tex, which you can download here. Without this program, this blog would probably not be possible since I write my solutions in […]

January 4, 2010 at 1:17 pm

LaTeX in WordPress « Eikonal Blog[…] Converting LaTeX to WordPress – http://lucatrevisan.wordpress.com/2009/02/21/converting-latex-to-wordpress/ […]

January 17, 2010 at 2:37 am

Using LaTeX in WordPress « God, Your Book Is Great !![…] for most of my needs. There are some inconveniences using LaTeX in WordPress . For a discussion see here. For more power users, Luca has written a Python tool to convert a LaTeX file to a form ready to be […]

April 29, 2010 at 9:27 pm

The core of a cooperative game « Christian Marks[…] improve recall, at the risk of subjecting the reader to extraneous cognitive load. Also, WordPress behaves erratically enough that it is fair and appropriate to interject expletives into posts that have […]

August 27, 2011 at 7:27 pm

Shajahan ESneed some help from you..

in posting mathematical formulae in wordpress..

cud you please giv ur id? mine is itsshajahan[at]gmail[dot]com

May 4, 2013 at 12:33 am

mathtuition88this is really helpful. I will be using it to post on my blog.

I hope one day wordpress can support full LaTeX though. :P