Search code examples
pythondjangodjango-templatesrestructuredtextdocutils

Math directives in Django ReStructuredText


I am using Django template restructuredtext filter to display an rst file in my template. I have docutils installed, and have added django.contrib.markup to settings.py. Displaying rst text works fine.

Now, I want to add some math to the rst file:

test.rst:

.. math::
    16 \div 2

The equation is :math:`16 \div 2`.

Update:: I fixed this partially. Initially, the errors were Unknown directive. I was trying to add math directives to the template, but then realized that docutils 0.10 supports math directives by default. Hence just upgraded to docutils-0.10.

However, now what I see is:

\begin{equation*} 16 \div 2 \end{equation*}
The equation is \(16 \div 2\)

The the rst file works fine if I do rst2html and check it in the browser. The difference is that the html file loads MathJax javascript while Django template does not add the js file.

So I am guessing that some setting or config is missing...

Would really appreciate a helping hand here!


Solution

  • Fixed this with 2 things:

    1. Initial errors were Unknown directive. I was trying to add math directives to the template to load latex, but then realized that docutils 0.10 supports math directives by default. Hence just upgraded to docutils-0.10.

    2. After that the error was that I was seeing the latex math code enclosed in \begin{equation*} and \end{equation*} tags. Comparing the output of rst2html and django template filter, I realized that the MathJax javascript needed to do latex is not added by django restructuredtext filter. So, in my template file section, I manually added:

      <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

    Now, it works!