Tormod Ravnanger Landet on 17 Oct 2008 10:56 pm
Using jsMath in MoinMoin with ReStructuredText
This is not a typical post for this blog, but it was just convenient to put the following here. Feel free to skip this post if you are reading this blog for the normal content and haven’t gotten here by searching for just this.
I spent a few hours at work today researching how best to add math (displayed equations) support to the docutils ReStructuredText parser for use with the MoinMoin wiki engine. I put my findings up here for the search engines to pick up in case more people need this.
The best solution today seems to be jsMath. It is more versatile than the various LaTeX -> dvi -> png solutions (and easier to set up). It is also much better supported than MathML.
Docutils, which is the library used by MoinMoin to render ReStructuredText to html, does not yet support math natively (as of version 0.6). Fortunately it can easily be extended with new directives. The only thing you need to do is add the following code to a Python file that is executed while rendering your wiki pages. The theme module is a good place, just stick the following at the bottom of your theme’s .py file:
# LaTeX support
from docutils.parsers.rst import directives, roles
from docutils import nodes
def latex_directive(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine):
latex = '\n'.join(content)
return [nodes.raw('', '\\['+latex+'\\]', format='html')]
latex_directive.content = 1
directives.register_directive('latex', latex_directive)
def latex_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
latex = rawtext.split('`')[1].replace('\\\\','\\') # Restore escaped backslashes
return [nodes.raw(rawtext, '\\('+latex+'\\)', format='html')], []
roles.register_canonical_role('latex', latex_role)
This is a bit of a hack as it uses the old style function-as-directive model, but hey, it works, and I did not even know of docutils directives until today, so working is most definitely a big plus
It is based on this, but simplified a whole lot due to using jsMath to render the LaTeX equations.
You also need to include
<script type="text/javascript" src="/path/to/jsMath/easy/load.js"></script>
somewhere in your html code. I put it in the page header with an ugly hack by adding the script to the output around line 680 of MoinMoin/theme/__index__.py. It can (and should) probably go in your theme’s .py file somewhere so that it does not disappear when MoinMoin is upgraded. I did not bother to do this for testing purposes, though.
Now you can write :latex:`e^{i\pi} + 1 = 0` in your wiki pages and it should be rendered to \(e^{i\pi} + 1 = 0\) in the final output (which seems like a small change, but believe me, it is easier this way then double backslashing the LaTeX code in the wiki page. You can also have displaymath equations with the following syntax:
.. latex::
e^{quation} g_{oes} = here
which is turned into
\[
e^{quation} g_{oes} = here
\]
The \( ... \) and \[ ... \] is picked up by jsMath and turned into beautiful equations (depending on field of work and definition of beautiful
). For non-javascript enabled browsers the code is left as is, so the equation can still be deciphered by LaTeX savvy readers. See here for examples of what jsMath can do.
So, in the end it was fairly easy to get good looking math support in MoinMoin. Hopefully docutils will come with jsMath support in the future. The new upcoming 0.5 version of Sphinx seems to do, and it is based on docutils and ReST. Shinx is a new documentation power tool for Python developers. Highly recommended to make documenting code a bit more fun.
Comments are disabled on this article due to problems with spam. Somehow spammers believe that link-spamming this page with praise for the article will send them lots of readers or google-points or something …
Tags: LaTeX, MoinMoin, Python, ReStructuredText2 Responses to “Using jsMath in MoinMoin with ReStructuredText”
on 05 Aug 2009 at 18:52 1.Robert Langlois said …
I wish I had discovered this blog post earlier; I came to the same conclusion after blindly searching for a while.
My only question (since I have not had time to implement this myself) is, why not put the math code in $x^2$? This seems like it works for both jsMath and latex but I am not sure how the ReST parser would handle it.
This may save you from having to write a directive.
Also, have you found some way to handle image URLs (preferably without a directive). Say you specify:
.. image: some-image.png
But you want the HTML document to refer to http://url/some-image.png while having the latex document refer to `figs/some-image.png`?
on 05 Aug 2009 at 20:34 2.Tormod Ravnanger Landet said …
Hi!
I think I implemented .. math::, not .. latex:: in the end. I did that to stay compatible with the Sphinx tool for Python documentation. I haven’t looked into the image problem as most of the wiki pages are text only and I do not perform latex conversion, only to and from Sphinx on a few occasions.