Math in reStructuredText with LaTeX

Solution 1:

Since version 0.8 it is supported natively: You shouldn't use any workaround anymore. The syntax is also very simple. It is the same as latex math, but without the enclosing $$

So you can simply write the following for a math block

.. math::

   \frac{ \sum_{t=0}^{N}f(t,k) }{N}

Or if you want to write inline you can use this:

:math:`\frac{ \sum_{t=0}^{N}f(t,k) }{N}`

notice the delimiting backticks there.


UPDATE:

in newer versions it seems to be necessary to use a double-backslash for the math elements, so it's \\frac and not \frac

Solution 2:

There is native support for a "math" role and directive (using LaTex input syntax) since Release 0.8 (2011-07-07).

Solution 3:

This is what I do:

  1. Add a role for math at the beginning of your reST document:

    .. role:: raw-math(raw)
        :format: latex html
  2. Write your maths like

    :raw-math:`$$ \frac{s}{\sqrt{N}} $$`

    (use $$ ... $$ if you want it in a block, or $ ... $ if you want it inline.)

  3. Generate html output like this:

    rst2html --stylesheet=/path/to/my_beautiful_stylesheet.css my_file.rst \
    | tidy -q | /path/to/itex2MML > my_file.xhtml

    This generates the html with rst2html, tidies up with tidy and then converts the latex maths into MathML with itex2MML, outputting on an xhtml file.

Notes:

  1. Style sheet is optional
  2. For itex2MML go here.
  3. The extension of your html file should be xhtml or xml, otherwise the maths won't show in your browser.

Solution 4:

LaTeX and ReST are two solutions to the same problem (whole-document markup), and I'm not sure that one would be happy living inside the other.

  • LaTeX first, then ReST: LaTeX needs to precisely position typographical elements in order to lay out equations (and everything else). ReST wouldn't take the LaTeX output (PostScript or similar) as input.
  • ReST first, then LaTeX: You'd have to write an output formatter for ReST that output LaTeX code, and define extensions in ReST to prevent it from misinterpreting your equations or other explicit TeX. Your output from LaTeX will still be something akin to PostScript or PDF.

I've seen code for using a subset of TeX to generate a graphic equation which is then pulled into HTML or similar (for example,Trac's LaTeX Formula macro), which may be the best available solution.