Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using '\displaymath' directives in docstrings formulas

I am using the Sphinx documentation package to document a small Python tool kit that I am working on and I would like to describe the mathematical formulas that the various modules implement by listing them in LaTeX format in the Python docstrings.

It is pretty easy to achieve this with :math: or .. math:: directives for reStructuredText, for example:

.. math::
   \\displaymath \\sum_{i=1}^{\\infty} x_{i}

but the \\displaymath directive is just highlighted as red text when in a Python docstring. When used in a .rst file (e.g. index.rst) this works as expected, and the sub- and super-scripts for the summation are directly below and above the summation symbol.

Is this simply not supported for docstrings, or am I doing something wrong or not doing something that I need to be doing?

like image 244
si28719e Avatar asked Apr 10 '12 00:04

si28719e


People also ask

How do you specify the docstring of a function?

Declaring Docstrings: The docstrings are declared using ”'triple single quotes”' or “””triple double quotes””” just below the class, method or function declaration. All functions should have a docstring.

What should be included in docstring?

Module docstrings should include the following: A brief description of the module and its purpose. A list of any classes, exception, functions, and any other objects exported by the module.

Can docstrings include escape sequences?

Since docstrings are Python strings, escape sequences such as \n will be parsed as if the corresponding character—for example a newline—occurred at the position of the escape sequence in the source code.

Can docstrings be assigned to variables?

Yes, you can do that! You can actually 'document' lambdas and variables in a module by attaching docstrings to them.


1 Answers

You do not need \displaymath in Sphinx and no additional escaping backslashes for \sum and \infty.

The following example function renders fine in both html and latex output when called with .. autofunction:::

def test_func(x):
    """This function will try to calculate:

    .. math::
        \sum_{i=1}^{\\infty} x_{i}

    good luck!
    """
    pass

You have to use the .. math:: directive as inline math (:math:) doesn't work.

like image 159
bmu Avatar answered Oct 06 '22 01:10

bmu