6

I'm trying to get numbered figures to work on my Sphinx documentation project using latexpdf output. I installed the Sphinx numfig.py extension found here https://bitbucket.org/arjones6/sphinx-numfig

However, whenever I use the :num: tag that is supposed to provide a cross reference with the figure's number I instead get the following

rst

.. _fig_logo:


.. figure:: logo.*

        Example of a figure

Reference to logo :num:`figure #fig_logo`

Generates output:

Reference to logo figure ??

Am I doing something wrong?

bmu
  • 35,119
  • 13
  • 91
  • 108
John Lotacs
  • 1,184
  • 4
  • 20
  • 34

1 Answers1

7

Seems that if you have an underscore in your label name (like in fig_logo) sphinx replaces this with a minus (-, this makes sense as latex sometimes behaves strange in cases whit underscores), while the reference still uses the underscore. For that reason latex can not find the label referenced.

This is the resulting tex code generated by sphinx:

\includegraphics{logo.png}
\caption{Example of a figure}\label{index:fig-logo}\end{figure}

Reference to logo \hyperref[index:fig_logo]{figure  \ref*{index:fig_logo}}

(Note the difference between fig-logo as label and fig_logo as reference.)

If you replace the underscore by a minus for instance

.. _fig-logo:


.. figure:: logo.png

        Example of a figure

Reference to logo :num:`figure #fig-logo`

the tex code looks like this:

\includegraphics{pandas_bar.png}
\caption{Example of a figure}\label{index:fig-logo}\end{figure}

Reference to logo \hyperref[index:fig-logo]{figure  \ref*{index:fig-logo}}

and in the pdf generated this is resolved to

Reference to logo figure 1

Update

If you don't want to change all the labels you can update numfig: it should be sufficient to add the line

target = target.replace('_', '-')

just before line 27 in your copy of the extension.

I opened an issue on bitbucket.

bmu
  • 35,119
  • 13
  • 91
  • 108
  • Is there a possible fix to the extension? there are a lot of labels that would need changing. – John Lotacs Feb 26 '13 at 21:41
  • Does this imply that Sphinx's rst->latex conversion treats the '-' and '_' the same all the time? – John Lotacs Mar 01 '13 at 13:39
  • I don't know. I was looking through the source code of sphinx, but I didn't found any replace call for the moment. I'm not sure if this is done in sphinx or in the underlying `docutils`. However in general latex is sensitive to underscores, so I would try to avoid undersores. – bmu Mar 01 '13 at 15:20
  • The underscore-to-hyphen substitution is done in the `docutils.nodes.make_id()` function. – mzjn Mar 01 '13 at 17:45