#!/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])

#!/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.)

**Installation**

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 setup.py install

tar zxvf markdown2Mathjax-x.x.x.tar.gz

cd markdown2Mathjax-x.x.x

python setup.py install

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

*pip install markdown2Mathjax*

**Usage**

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.

**Explanation**

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:

*tmp=sanitizeInput(test)*

reconstructMath(tmp[0],tmp[1])==test

reconstructMath(tmp[0],tmp[1])==test

should always evaluate to true.

Last updated on

**April 13th, 2012**