如何在 python-sphinx 中创建等式末尾的数字?

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

使用下面的代码,我们可以创建每个方程的末尾都包含数字。

\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}

现在将它们写在 python-sphinx 中。

.. math::

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

make html编译后得到下图:

你可以看到每行末尾没有数字。
如何在 python-sphinx?

的每一行末尾创建带有数字的相同方程式

如@ilke444 所评论,您可以在每行末尾添加 \tag{1}。如果需要引用某一特定行,可以在行中添加标签。

 .. 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}

输出:

所以你可以这样引用它:

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

refer it for single line:

.. :math:: \ref{a}

注意:标签必须是唯一的,否则将无法渲染输出。

更新:升级 sphinx 中的 mathjax 现在支持像 latex 一样的自动编号行。

打开 _build 目录中的 index.html 并更改 mathjax 脚本

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

改为:

<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>

现在 align 块中的所有行都将自动编号。

相关文档:Automatic Equation Numbering

为什么 python-sphinx 这样的行为?

source code for numbering in sphinx

Python-Sphinx 将每个数学块作为一个整体处理,并为整个数学块分配编号。生成html时,会忽略数学块内的编号,交给数学渲染库处理(默认为MathJax)。

所以确实,问题是为什么Mathjax不支持对齐块中的自动编号行,所以你可以参考这个答案

How to number and reference multiline equations?

到目前为止,唯一的方法是为每一行添加标签:(,没有自动方法。


顺便说一句

如果整个数学块只需要一个标签

据我所知,python-sphinx 提供了为每个数学块生成数字的选项(通过使用 :label:

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

输出

标签(2)因为序列号是根据其在整个文档中的位置生成的。

你可以通过

来参考
refer to it by :eq:`abc`