2

There is the following piece of code on the official Sphinx documentation:

.. math::
   :nowrap:

   \begin{eqnarray}
   y    & = & ax^2 + bx + c \\
   f(x) & = & x^2 + 2xy + y^2
   \end{eqnarray}

I tried for a long time to get it to work, until I changed it to:

.. math::
   :nowrap:

   $$
   \\begin{eqnarray}
   y    & = & ax^2 + bx + c \\\\
   f(x) & = & x^2 + 2xy + y^2
   \\end{eqnarray}
   $$

How could it be that the official Sphinx documentation is wrong? Or is something obscure happening here? Even Sagemath documentation seems to suggest that $$ need to be used (see "If you wish to explicitly not wrap the MATH block [...]") Or is this a bug?

(I use Sphinx 2.2.0 and in my index.rst file I use autofunction for the function whose docstring contains the equation above.)

bad_coder
  • 11,289
  • 20
  • 44
  • 72
user719220
  • 71
  • 2
  • 3
    The documentation does point out 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"). – mzjn Jan 23 '20 at 10:10
  • 2
    Not a bug. It works with doubled backslashes or raw string. `$$` is not needed. I use Sphinx 2.3.1. – mzjn Jan 23 '20 at 10:38

1 Answers1

3

The Sphinx documentation states 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"). That happens because Python will interpret the string before Sphinx has the opportunity to parse it.

So, if you want to insert math in a docstring, the following should work:

.. math::
   :nowrap:

   \\begin{eqnarray}
   y    & = & ax^2 + bx + c \\\\
   f(x) & = & x^2 + 2xy + y^2
   \\end{eqnarray}

You may simplify it further with:

.. math::

   y    & = ax^2 + bx + c \\\\
   f(x) & = x^2 + 2xy + y^2

The :nowrap only hints Sphinx that it should not wrap the provided equations in a LaTeX "split" environment (See Section 3 of the amsmath Manual). Instead, you must supply your own as indicated in the first example of this post.

André Anjos
  • 4,641
  • 2
  • 27
  • 34