markdown2Mathjax 0.3.8

Extend markdown2 for use with mathjax
markdown2Mathjax is a simple package that allows you to use latex style math generated by MathJax in conjunction with the popular markdown format. There are two implementations of markdown in python, this package is designed to be used with the more widely used and actively maintained of the two, called markdown2. Typical usage is as follows:

#!/usr/bin/env python

from markdown2Mathjax import sanitizeInput, reconstructMath
from markdown2 import markdown

tmp = sanitizeInput(sometxt)
markdedDownText = markdown(tmp[0])
finalOutput = reconstructMath(markedDownText,tmp[1])

(where "sometxt" is a string containing the markdown+mathjax text.)


markdown2Mathjax is hosted on pypi, so can be installed either from source, using:

tar zxvf markdown2Mathjax-x.x.x.tar.gz
cd markdown2Mathjax-x.x.x
python install

or using pip (or easy_install if you'd prefer):

pip install markdown2Mathjax


By default, math is marked by single $ signs for inline math and double $$ signs for equations. However, any delimiting strings may be used by changing the keyword arguments to the sanitizeInput and reconstructMath functions. Note that by default mathjax does NOT use single dollars signs to delimit inline math, so if you use the defaults for this package, you will have to instruct mathjax to treat it appropriately.


The sanitizeInput function finds any instances of math (as determined by the supplied delimiters) and replaces them with a placeholder. This placeholder can be specified by the user, but it is recommended you use the supplied default of "$0$". The placeholder has to be something that won't be processed by markdown, and won't ever be PRODUCED by markdown. The first of these requirements is easy enough to meet, the second can be somewhat more subtle. If the placeholder already exists in the supplied user text, it is noted by sanitizeInput and will be left alone when reconstruction math after markdown processing.

Each time a valid math block is encountered and replaced by a placeholder, the math is saved in a list (called codeblocks) which is then returned by the sanitizeInput function. Each entry in codeblocks is prefixed with either 0 (for a literal occurance of the placeholder in the text), a 1 for inline math or a 2 for a separate equation.

The sanitized string from sanitizeInput can then be marked up by markdown without fear of your math being mangled. Finally, reconstructMath replaces each of the placeholders in the marked up text with the appropriate codeblock. This final html can then be displayed with appropriate mathjax javascript headers.

Another way to think about this is than reconstructMath is basically the inverse of sanitizeInput. So without using markdown in between, nothing should change between the two (except when reconstructMath also does some extra cleanup of the math codeblocks). That is:


should always evaluate to true.

last updated on:
April 13th, 2012, 17:56 GMT
developed by:
Matthew Young
license type:
GPL v3 
ROOT \ Text Editing&Processing \ Markup


In a hurry? Add it to your Download Basket!

user rating



Rate it!

Add your review!