From offline LaTeX to online QuickLaTeX

For writing mathematics in WordPress using LaTeX I have been using Pavel Holoborodko’s nice QuickLaTeX plugin. It makes it very easy to convert offline LaTeX source code, possibly including custom macros, into (part of) a WordPress post. To reduce the effort a bit further, I wrote a tiny Python 3 script.

#!/usr/bin/env python

# This module is the interface to the Python regular expression engine.
# See for a tutorial.
import re

# This module is used to pass command line arguments.
# See
import argparse

# Parse input file name from command line.
parser = argparse.ArgumentParser()
parser.add_argument('infile', help='Convert LaTeX to QuickLaTeX for WordPress')
args = parser.parse_args()

f = open(args.infile, 'r')
latex =

# Discard preamble and title and select main text to work on.
p = re.compile(r'\\maketitle(.*)\\end{document}', re.S)
m =
text =

# Each tuple consists of a pattern and a replacement. Add as needed.
subs = [(re.compile(r'\\section\*?{(.*?)}'), r'<h3>\1</h3>'),
        (re.compile(r'\\subsection\*?{(.*?)}'), r'<h4>\1</h4>'),
        (re.compile(r'\\medskip'), r'<p></p>'),
        # Item listing enviroments
        (re.compile(r'\\begin{itemize}(.*)\\end{itemize}', re.S), r'<ul>\1</ul>'),
        (re.compile(r'\\item(.*)'), r'<li><p>\1</p></li>'),
        # Text formatting
        (re.compile(r'\\textit{(.*?)}'), r'<em>\1</em>'),
        (re.compile(r'\\emph{(.*?)}'), r'<em>\1</em>'),
        # Letters with accents
        (re.compile(r'\\`([aeiou])'), r'&\1grave;'),
        (re.compile(r'\\\'([aeiou])'), r'&\1acute;'),
        # Remove commented lines
        (re.compile(r'^%.*$', re.M), r'')]

for p, r in subs:
    text = p.sub(r, text)

# Pipe to xclip to get the output directly on the X11 clipboard.

As you can see, searching and replacement is done using Python’s regular expression module. For those replacements that work on just a single line of input, it would probably be more efficient to first break the input string into lines and then process them sequentially, but clearly efficiency is not really an issue here.

In order to use the script, store the above code into a file, say

that can be found in your path and give it executable permissions. Then run as

$ foo.tex | xclip

In this example the output is piped into the xclip utility, so afterwards you can just paste the contents of your clipboard into the WordPress editor. On systems without X11 (e.g. MS Windows) you should probably redirect standard output to an intermediate file.