reStructuredText中的参考图通过图号使用:numref:

时间:2017-05-29 16:40:08

标签: python-sphinx restructuredtext

我目前正在尝试设置reStructuredText模板。我想包括编号和参考数字。所以我按照sphinx文档(http://www.sphinx-doc.org/en/stable/markup/inline.html)中给出的说明进行操作。

首先我加入了

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`

使用make html

构建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.

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

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

:numref:`my-figure`

导致了类似的结果。

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

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

提前致谢。

2 个答案:

答案 0 :(得分:1)

假设您的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 ...

答案 1 :(得分:0)

您应该使用以下语法

Reference to the figure :numref:`(Fig. %s) <my-figure>`

正如http://www.sphinx-doc.org/en/stable/markup/inline.html?highlight=numfig#role-numref

所述

独立评论:您可以使用:name: my-figure指令figure代替.. _my-figure:标签。

关于:

  

我也试过

:numref:`my-figure`
     

导致了类似的结果。

我无法重现。它适用于我。语法越广泛

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

以上只需要自定义格式,这意味着添加括号。

相关问题