在Sphinx和reStructuredText中使用数字引用数字

Luc*_*cas 30 restructuredtext python-sphinx

在编写将使用Sphinx处理的RST时,我无法在引用数字时使用Sphinx LaTeX输出来使用图号.例如,这段代码:

The lemmings are attacking, as can be seen in :ref:`figlem`.

.. _figlem:

.. figure:: _static/lemming_invasion.* 

   They're coming!
Run Code Online (Sandbox Code Playgroud)

将转换成这个:

旅行者正在攻击,正如他们即将到来的那样!

/ image到这里/

图1.1:他们来了!

但我想要的是"标准"LaTeX引用数字的方式,如下所示:

旅鼠正在攻击,如图1.1所示

我该如何实现这一目标?我目前使用的代码是Sphinx手册推荐的代码,但它不会产生我想要的输出.

And*_*jos 23

在最新版本的Sphinx(1.3 +)中,编号数字和从文本引用它们变得更容易,因为它现在内置了对它的支持.

在您的文字中,您可以执行以下操作:

.. _label:
.. figure:: images/figure.*


At :numref:`label` you can see...
Run Code Online (Sandbox Code Playgroud)

最终的结果应该是"在图1.1中你可以看到......".此技术适用于默认HTML输出和LaTeX输出.

在您的conf.py文件中,请确保设置标志numfig = True.还有参考文本格式(numfig_formatnumfig_secnum_depth)的配置选项.

参考文献:

  • 更新的链接:http://www.sphinx-doc.org/en/master/usage/restructedtext/roles.html#cross-referencing-figures-by-figure-number (2认同)

jte*_*ace 18

numfig扩展正是这样做的.我试过了,它对我有用.

  • 如果给出快速参考的语法示例,那将是很好的. (4认同)
  • 这现在内置在 sphinx 中。 (2认同)

red*_*rry 6

要扩展已接受的答案,您可以快速进行如下设置.将numfig.py文件放在您的source目录中.然后打开conf.py并取消注释所说的行

sys.path.insert(0, os.path.abspath('.'))
Run Code Online (Sandbox Code Playgroud)

然后添加'numfig'extensions列表中.

要在您的rst文档中使用,请先标记您的图形(例如fig-main):

.. _fig-main:

.. figure:: main.png

   This is the figure caption.
Run Code Online (Sandbox Code Playgroud)

最后,您可以使用:num:指令引用其图号,如下所示:

Refer to the main figure (Figure :num:`fig-main`).
Run Code Online (Sandbox Code Playgroud)

  • 现在这是内置的,内联角色被称为: numref http://www.sphinx-doc.org/en/stable/markup/inline.html#cross-referencing-figures-by-figure-number (2认同)