在 Sphinx 和 reStructuredText 中用数字引用数字
当编写将使用 Sphinx 处理的 RST 时,我无法让 Sphinx LaTeX 输出使用图引用数字时的数字。例如,这段代码:
The lemmings are attacking, as can be seen in :ref:`figlem`.
.. _figlem:
.. figure:: _static/lemming_invasion.*
They're coming!
将被转换成这样:
旅鼠正在攻击,这是可能的 见他们来了!
/图片位于此处/
图1.1:他们来了!
但我想要的是引用数字的“标准”LaTeX 方式,如下所示:
旅鼠正在攻击,这是可能的 如图1.1所示
如何实现这一目标?我当前使用的代码是 Sphinx 手册推荐的代码,但它不会产生我想要的输出。
When writing RST that will be processed with Sphinx, I can't get Sphinx LaTeX output to use figure numbers when referencing figures. For instance, this code:
The lemmings are attacking, as can be seen in :ref:`figlem`.
.. _figlem:
.. figure:: _static/lemming_invasion.*
They're coming!
Will be converted into this:
The lemmings are attacking, as can be
seen in They're coming!/image goes here/
Figure 1.1: They're coming!
But what I want is the "standard" LaTeX way of referencing figures, like this:
The lemmings are attacking, as can be
seen in Figure 1.1
How do I achieve this? The code I'm currently using is what the Sphinx manual recommends, but it doesn't produce the output I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在最新版本的 Sphinx (1.3+) 中,对数字进行编号并从文本中引用它们变得更加容易,因为现在内置了对其的支持。
在您的文本中,您可以执行以下操作:
最终结果应类似于“在图 1.1 中您可以看到...”。该技术适用于默认 HTML 输出和 LaTeX 输出。
在您的
conf.py
文件中,确保设置标志numfig = True
。还有参考文献文本格式的配置选项(numfig_format
和numfig_secnum_depth
)。参考文献:
In the latest versions of Sphinx (1.3+), numbering figures and referencing them from text got a bit easier as support for it is now built-in.
In your text, you can do something like:
The end result should be something like "At Fig 1.1 you can see...". This technique works both with the default HTML output and the LaTeX output.
In your
conf.py
file, make sure to set the flagnumfig = True
. There are also configuration options for the references' text format (numfig_format
andnumfig_secnum_depth
).References:
numfig 扩展正是这样做的。我尝试过,它对我有用。
The numfig extension does exactly this. I tried it and it worked for me.
要扩展已接受的答案,您可以按如下方式快速进行设置。将
numfig.py
文件放入source
目录中。然后打开conf.py
并取消注释“Then add
'numfig'
to theextensions
list”行。要在您的
rst
文档中使用,首先标记您的图形(例如fig-main
):最后,您可以使用
:num:< 引用其图形编号/code> 指令,如下所示:
To expand on the accepted answer, you can quickly get this set up as follows. Put the
numfig.py
file in yoursource
directory. Then openconf.py
and uncomment the line that saysThen add
'numfig'
to theextensions
list.To use in your
rst
document, first label your figure (e.g.,fig-main
):Finally, you can reference its figure number using the
:num:
directive, like this:我认为引用数字尚未在 reST 中实现,但这里有解决方法 http:// /article.gmane.org/gmane.text.docutils.user/5623 让您更接近。
I think referencing Figures is not yet implemented in reST, but here is workaround http://article.gmane.org/gmane.text.docutils.user/5623 that gets you closer.
人们可以使用内联的原始乳胶代码。对于上面的示例,首先定义了原始 Latex 代码的角色,然后用于使用
\ref{}
Latex 命令引用图形,并使用为图形设置标签>\label{}
乳胶命令。以下内容应该有效:
请注意,
\label{}
命令将出现在 tex 文件的标题内,但它仍然是可以接受的,至少对于pdflatex
来说是这样。另请注意,:raw-latex
之前至少应有一个空格。One can use raw latex code, inline. For the example above, a role for raw latex code is first defined and than used to refer to the figure with the
\ref{}
latex command, and to set a label to the figure with the\label{}
latex command.The following should work:
Note that the
\label{}
command will appear inside the caption in the tex file, but it is still acceptable, at least bypdflatex
. Also note that there should be at least one space before:raw-latex
.