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

发布于 2024-08-30 06:31:01 字数 581 浏览 6 评论 0原文

当编写将使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

友欢 2024-09-06 06:31:01

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

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

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


At :numref:`label` you can see...

最终结果应类似于“在图 1.1 中您可以看到...”。该技术适用于默认 HTML 输出和 LaTeX 输出。

在您的 conf.py 文件中,确保设置标志 numfig = True。还有参考文献文本格式的配置选项(numfig_formatnumfig_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:

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


At :numref:`label` you can see...

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 flag numfig = True. There are also configuration options for the references' text format (numfig_format and numfig_secnum_depth).

References:

甜心 2024-09-06 06:31:01

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

The numfig extension does exactly this. I tried it and it worked for me.

一腔孤↑勇 2024-09-06 06:31:01

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

sys.path.insert(0, os.path.abspath('.'))

Then add 'numfig' to the extensions list”行。

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

.. _fig-main:

.. figure:: main.png

   This is the figure caption.

最后,您可以使用 :num:< 引用其图形编号/code> 指令,如下所示:

Refer to the main figure (Figure :num:`fig-main`).

To expand on the accepted answer, you can quickly get this set up as follows. Put the numfig.py file in your source directory. Then open conf.py and uncomment the line that says

sys.path.insert(0, os.path.abspath('.'))

Then add 'numfig' to the extensions list.

To use in your rst document, first label your figure (e.g., fig-main):

.. _fig-main:

.. figure:: main.png

   This is the figure caption.

Finally, you can reference its figure number using the :num: directive, like this:

Refer to the main figure (Figure :num:`fig-main`).
不再让梦枯萎 2024-09-06 06:31:01

我认为引用数字尚未在 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.

银河中√捞星星 2024-09-06 06:31:01

人们可以使用内联的原始乳胶代码。对于上面的示例,首先定义了原始 Latex 代码的角色,然后用于使用 \ref{} Latex 命令引用图形,并使用 为图形设置标签>\label{} 乳胶命令。

以下内容应该有效:

.. role:: raw-latex(raw)
     :format: latex

The lemmings are attacking, as can be seen in :ref:`figlem`
on figure :raw-latex:`\ref{pic:lem}`.

.. _figlem:

.. figure:: _static/lemming_invasion.* 

   They're coming! :raw-latex:`\label{pic:lem}`

请注意,\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:

.. role:: raw-latex(raw)
     :format: latex

The lemmings are attacking, as can be seen in :ref:`figlem`
on figure :raw-latex:`\ref{pic:lem}`.

.. _figlem:

.. figure:: _static/lemming_invasion.* 

   They're coming! :raw-latex:`\label{pic:lem}`

Note that the \label{} command will appear inside the caption in the tex file, but it is still acceptable, at least by pdflatex. Also note that there should be at least one space before :raw-latex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文