Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing figures with numbers in Sphinx and reStructuredText

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.

like image 546
Lucas Avatar asked Aug 30 '25 15:08

Lucas


1 Answers

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:

  • http://www.sphinx-doc.org/en/stable/config.html#confval-numfig
  • https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#cross-referencing-figures-by-figure-number
like image 67
André Anjos Avatar answered Sep 13 '25 18:09

André Anjos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!