使用 :numref 通过图形编号引用 reStructuredText 中的图形:

sta*_*der 4 restructuredtext python-sphinx

我目前正在尝试设置一个 reStructuredText 模板。我想包括编号的数字和对这些数字的引用。所以我遵循了 sphinx 文档(http://www.sphinx-doc.org/en/stable/markup/inline.html)中给出的说明。

首先我包括了这条线

numfig = True
Run Code Online (Sandbox Code Playgroud)

在文件“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`
Run Code Online (Sandbox Code Playgroud)

当我使用构建 html 文件时 make html

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.
Run Code Online (Sandbox Code Playgroud)

然后文件 index.html 显示图形、标题和对它的引用,如下所示: 输出 index.html

所以标题显示了一个很好的'图。1' 但使用的参考:numref:显然不起作用。我也试过

:numref:`my-figure`
Run Code Online (Sandbox Code Playgroud)

这导致了类似的结果。

有人知道为什么参考在这里不起作用吗?

同样有趣的是:上面提到的所有内容都在我的文件 'rstTemplate.rst' 中,我通过.. include:: rstTemplate.rst. 在 html 构建之后,我收到文件“index.html”和“rstTemplate.html”。与 'index.html' 版本不同,'Fig. 'rstTemplate.html' 中的图标题中不包含 1'。这可能与这里出了什么问题有关?

提前致谢。

Vil*_*mko 6

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

import sys
import os
html_theme = 'sphinx_rtd_theme'
numfig = True
Run Code Online (Sandbox Code Playgroud)

并且您index.rst包含:

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

 mychapter
Run Code Online (Sandbox Code Playgroud)

这是章节 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 ...
Run Code Online (Sandbox Code Playgroud)

这呈现为:

This is a reference to Fig.1 bla bla ...
Run Code Online (Sandbox Code Playgroud)

  • 谢谢 viliam,那行得通。我认为这是我在 index.rst 文件中包含章节的方式。numref 不适用于 `.. include:: rstTemplate.rst`,但是当使用 `.. toctree:: :maxdepth: 2 :caption: Home :hidden: rstTemplate` 时 (2认同)