Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math expression in docstring is not rendered correctly by Sphinx

I've been trying to figure out what's wrong with this expression in my docstring. I'm using the sphinx.ext.mathjax extension in python sphinx v1.2b. The docstring:

.. math::

    w_k^* = \min_{w_k} \ell_k(w_k) + \lambda\left(\alpha||w_k||_1 
    + \frac{1}{2}(1-\alpha) ||w_k||^2\right)

This is what appears: enter image description here

But it continues to generate this strange warning and not render the expression at all:

WARNING: Block quote ends without a blank line; unexpected unindent.

Strangely enough, if I remove \alpha, \left, \right, \frac symbols, the expression renders fine without warnings. Not sure why \lambda would be supported and not \alpha.

like image 707
bluecat Avatar asked May 09 '13 18:05

bluecat


1 Answers

From the Sphinx documentation:

Keep in mind that when you put math markup in Python docstrings read by autodoc, you either have to double all backslashes, or use Python raw strings (r"raw").

This is needed so that LaTeX commands, such as \alpha, are interpreted correctly (\a and a few other sequences have special meaning in a string literal).

This is the raw version of the docstring in the question, with triple quotes, prepended by r:

r"""
.. math::
 
    w_k^* = \min_{w_k} \ell_k(w_k) + \lambda\left(\alpha||w_k||_1 
    + \frac{1}{2}(1-\alpha) ||w_k||^2\right)
"""
like image 168
mzjn Avatar answered Sep 25 '22 13:09

mzjn