How to create the numbers at the end of equation in python-sphinx?

Viewed 1131

With the following code ,we can create every equation containing number at the end.

\documentclass[12pt]{article}
\usepackage{mathtools}
\begin{document}
\begin{align}
    100 + x &= y            \\    
    \frac{y}{x} &\ge 1.3    \\
    (100+x)-(100+x)z &= y 
\end{align}
\end{document}

enter image description here

Now to write them in python-sphinx.

.. math::

    \begin{align}
        100 + x &= y            \\    
        \frac{y}{x} &\ge 1.3    \\
        (100+x)-(100+x)z &= y 
    \end{align}

After compiling it with make html,we get the below image:
enter image description here

You can see there is no numbers at the end of each line.
How to create the same equation with numbers at the end of each line in python-sphinx?

1 Answers

as @ilke444 commented, you can add \tag{1} to end of each line. And if you need refer to one specific line, you can add label in line.

 .. math::
   \begin{align}
        100 + x &= y    \label{a}   \tag{1}    \\    
        \frac{y}{x} &\ge 1.3 \label{b}  \tag{2}  \\
        (100+x)-(100+x)z &= y \label{c} \tag{3} 
   \end{align}

output:

enter image description here

So you can refer it like this:

refer it inline :math:`\ref{a}`

refer it for single line:

.. :math:: \ref{a}

Notice: label should be unique, otherwise output will not be rendered.

update: upgrade mathjax in sphinx now support auto numbering line as latex.

open index.html in _build directory and change mathjax script

<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>

change it to:

<script>
    window.MathJax = {
        tex: {
            tags: "ams"
        }
    };
</script>
<script
    type="text/javascript"
    id="MathJax-script"
    async
    src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
></script>

Now all line in align block will auto numbering.

related doc: Automatic Equation Numbering

Why python-sphinx behavior like this?

source code for numbering in sphinx

Python-Sphinx handle every math block as whole and assign numbering for whole math block. When generating html, it will ignore numbering within math block, and leave it to math rendering library (default is MathJax).

So indeed, the problem is why Mathjax don't support auto numbering line in align block, so you can refer to this answer

How to number and reference multiline equations?

By now, the only way is add tag to each line :(, no automatic way.


BTW

If you only want one label for whole math block

As far as I know, python-sphinx provides option for generate number for each math block (by using :label:

 .. math::
   \begin{align}
        100 + x &= y    \\    
        \frac{y}{x} &\ge 1.3  \\
        (100+x)-(100+x)z &= y 
   \end{align}
   :label: abc

output

enter image description here

Label (2) because sequence number is generated due to its position in whole document.

And you can refer to it by

refer to it by :eq:`abc`
Related