Sphinx rst2pdf: Numbering figures

678 views Asked by At

I have some documents on Sphinx 2.x. In addition to an HTML version (make html), I want to export it in a PDF version as well, preferably using rst2pdf:

My documents have some figure images. Here's a snippet in index.rst.

.. figure:: ./_images/img01.png

   Caption for img01.


.. figure:: ./_images/img02.png

   Caption for img02.

I want to add "Figure <#>" in the beginning of every caption. Does anyone have any thoughts? Please note

  • numfig doesn't work with rst2pdf.

Update (3/22/2020). Add the following lines in conf.py.

numfig = True

numfig_format = {
    'figure': 'Figure %s.'
    }

numfig_secnum_depth = 1

In the HTML output it works.

HTML output with numfig

In the rst2PDF version, you don't see "Figure #".

rst2PDF output with the same numfig configuration

  • The :counter: directive doesn't work with Sphinx; I get the following error:
.. figure:: ./_images/img01.png

   Figure :counter:`figure`. Caption for img01.
$ sphinx-build -b pdf ./source/ ./build/
Running Sphinx v2.4.4
/home/sarah/sphinx_test/lib/python3.6/importlib/__init__.py:126: RemovedInSphinx30Warning: sphinx.environment.NoUri is deprecated.
Check CHANGES for Sphinx API modifications.
  return _bootstrap._gcd_import(name[level:], package, level)
Initiated sphinxcontrib-images backend: `sphinxcontrib_images_lightbox2.lightbox2:LightBox2`
building [mo]: targets for 0 po files that are out of date
building [pdf]: targets for 1 source files that are out of date
updating environment: [new config] 1 added, 0 changed, 0 removed
reading sources... [100%] index

Exception occurred:
  File "/home/sarah/sphinx_test/lib/python3.6/site-packages/docutils/nodes.py", line 439, in copy
    return self.__class__(reprunicode(self), rawsource=self.rawsource)
AttributeError: 'CounterNode' object has no attribute 'rawsource'
The full traceback has been saved in /tmp/sphinx-err-1bzghsm6.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
A bug report can be filed in the tracker at <https://github.com/sphinx-doc/sphinx/issues>. Thanks!
0

There are 0 answers