Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable math in sphinx?

I am using sphinx with the pngmath extension to document my code that has a lot of mathematical expressions. Doing that in a *.rst file is working just fine.

a \times b becomes:


works


However, if I try the same inside a *.py file for example in a module documentation like so:

"""
a \times b
"""

I end up with


does not work


Furthermore no amsmath functionality seems to work, either.
What do I need to do, to also have math formulas in my *.py documentations?

like image 238
Woltan Avatar asked Dec 05 '11 12:12

Woltan


People also ask

How do I use LaTeX math?

To Include mathematics in a document, you type the LaTeX source code for the math between dollar signs. For example, $ax^2+bx+c=0$ will be typeset as a x 2 + b x + c = 0 . If you enclose the code between double dollar signs, the math will be displayed on on line by itself.

Does Sphinx support LaTeX?

The specified LaTeX packages will be loaded before hyperref package and packages loaded from Sphinx extensions. If you'd like to load additional LaTeX packages after hyperref, use 'preamble' key instead.


1 Answers

Try putting a lower case 'r' before your docstring - like this:

def multiply(a,b):
    r"""
    returns a \times b
    """
    return a*b

I've never seen a raw literal string for a docstring before, but this will keep your \t from being interpreted as a <TAB> character.

like image 137
PaulMcG Avatar answered Sep 23 '22 13:09

PaulMcG