37

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.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
Lucas
  • 6,328
  • 8
  • 37
  • 49

5 Answers5

40

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:

André Anjos
  • 4,641
  • 2
  • 27
  • 34
19

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

jterrace
  • 64,866
  • 22
  • 157
  • 202
  • 1
    This didn't work for me initially (for HTML), but I used the fix by 'rrieber' here: https://bitbucket.org/arjones6/sphinx-numfig/issue/6/notimplementederror-unknown-node-num_ref and was able to get it to work. Thanks! – Devan Williams Feb 19 '13 at 17:21
  • How did you get it to work? The diff file provided by rrieber doesn't match the numfig.py I grabbed from that bitbucket repository. – John Lotacs Feb 22 '13 at 16:27
  • Try the version I updated here: https://github.com/jterrace/sphinxtr/blob/master/extensions/numfig.py (remove references to figtable and subfig modules) – jterrace Feb 22 '13 at 17:43
  • 6
    it would be nice if a syntax example was given for quick reference. – ryanjdillon Aug 22 '13 at 16:02
  • 2
    This is now built in to sphinx. – Gringo Suave Feb 12 '16 at 02:00
  • The link in the answer is broken. But as @GringoSuave mentioned this is now built into Sphinx. See this answer for more info: https://stackoverflow.com/a/36969111/2961550 – bicarlsen Jun 12 '21 at 12:43
7

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`).
redcurry
  • 2,381
  • 2
  • 24
  • 38
  • 2
    This is now built in and the inline role is called: numref http://www.sphinx-doc.org/en/stable/markup/inline.html#cross-referencing-figures-by-figure-number – Gringo Suave Feb 12 '16 at 02:01
0

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.

0

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.

Matti Pastell
  • 9,135
  • 3
  • 37
  • 44