Python sphinx 使用:numref:

Python sphinx 使用:numref:,python-sphinx,restructuredtext,Python Sphinx,Restructuredtext,我目前正在尝试设置一个重新构造的文本模板。我想包括编号的数字和这些数字的参考资料。因此,我遵循了sphinx文档()中给出的说明 首先,我包括了这一行 numfig = True 在文件“conf.py”中。我在文件“rstTemplate.rst”中实现了该图及其引用,如下所示: .. _my-figure: .. figure:: images/scen-smartcity.* :scale: 50 % :alt: smartcity symbol :align:

我目前正在尝试设置一个重新构造的文本模板。我想包括编号的数字和这些数字的参考资料。因此,我遵循了sphinx文档()中给出的说明

首先,我包括了这一行

numfig = True
在文件“conf.py”中。我在文件“rstTemplate.rst”中实现了该图及其引用,如下所示:

.. _my-figure:

.. figure:: images/scen-smartcity.*
    :scale: 50 %
    :alt: smartcity symbol
    :align: center

    This is the caption of the figure (a simple paragraph).

    This is the legend of the figure

Reference to the figure :numref:`(Fig. %s) my-figure`
当我使用
makehtml

Running Sphinx v1.6.1
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 1 source files that are out of date
updating environment: 0 added, 2 changed, 0 removed
reading sources... [100%] rstTemplate
rstTemplate.rst:: WARNING: duplicate label my-figure, other instance in ><path-to-file>\rstTemplate.rst
<path-to-file>\rstTemplate.rst:: WARNING: duplicate label my-figure, other instance in <path-to-file>\index.rst
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] rstTemplate
rstTemplate.rst:41: WARNING: undefined label: (fig. %s) my-figure
<path-to-file>\rstTemplate.rst:41: WARNING: undefined label: (fig. %s) my-figure
generating indices... genindex
writing additional pages... search
copying images... [100%] images/scen-smartcity.svg
copying static files... done
copying extra files... done
dumping search index in English (code: en) ... done
dumping object inventory... done
build succeeded, 6 warnings.
这导致了类似的结果

有人知道为什么这里引用不起作用吗

还有一点很有趣:上面提到的所有内容都在我的文件“rstTemplate.rst”中,我通过
将其包含在文件“index.rst”中。。include::rstTemplate.rst
。在html构建之后,我收到了文件'index.html'和'rstTemplate.html'。与“index.html”版本不同,“rstTemplate.html”中的图形标题中不包含“Fig.1”。这可能与这里的问题有关吗


提前感谢。

您应该使用以下语法

Reference to the figure :numref:`(Fig. %s) <my-figure>`
这导致了类似的结果

我无法复制。对我来说没问题。更广泛的语法

:numref:`(Fig. %s) <my-figure>`
:numref:`(图%s)`

以上仅用于自定义格式,这意味着在此处添加括号。

假设您的
conf.py
包含以下内容:

import sys
import os
html_theme = 'sphinx_rtd_theme'
numfig = True
您的
index.rst
包含:

.. toctree::
   :maxdepth: 2
   :caption: Home
   :hidden:

 mychapter
以下是章节RST文档的工作示例
mychapter.RST

.. figure:: images/my-image.png
   :name: my-custom-label

This is a caption of the image

This is a reference to :numref:`my-custom-label` bla bla ...
这表现为:

This is a reference to Fig.1 bla bla ...

我建议先解决重复标签警告,然后看看未定义标签警告是否仍然存在。谢谢viliam,这很有效。我想这就是我在index.rst文件中包含章节的方式。numref不能与
一起使用。。include::rstTemplate.rst
但在使用
时。。目录树:::maxdepth:2:标题:主页:隐藏:rstTemplate
.. figure:: images/my-image.png
   :name: my-custom-label

This is a caption of the image

This is a reference to :numref:`my-custom-label` bla bla ...
This is a reference to Fig.1 bla bla ...