如何在 Sphinx 中正确使用 numref table?

How to properly numref table in Sphinx?

我正在尝试通过 Sphinx 文档中的编号引用 table。我给 table 命名,然后尝试使用 numref,但它错误地显示了 table 的标签("modules_classes")而不是它的编号。

.. _modules_classes:

.. table:: Modules and their corresponding abstract classes

[table content]

Modules and their corresponding abstract classes are presented on table :numref:`modules_classes`.

我也试过这样做,网上找了个例子,结果都是一样的:

.. table:: Modules and their corresponding abstract classes
    :name: modules_classes

在我看来,我不需要导入 numfig 包,因为它默认包含在 Sphinx 中。使用命令 :ref: 显示 table 的标题,所以我知道引用是正确的。有人可以指出我所做的错误吗?

补救方法是在conf.py中设置numfig = True。如果没有此设置,您应该会在 运行 sphinx-build 时看到一条消息,上面写着 "WARNING: numfig is disabled. :numref: is ignored"

这适用于 HTML 和 LaTeX 输出。参见 https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-numfig