Search code examples
python-sphinxrestructuredtextdocutils

Reference a custom figure type in Sphinx


If I try to reference a custom figure class

class MyFigure(
    sphinx.util.docutils.SphinxDirective, 
    docutils.parsers.rst.directives.images.Figure
):
    pass

def setup(app):
    app.add_role('fig', MyFigure)

Then use it like

.. fig:: foo.png
    :name: myfig


Look at :ref:`myfig`.

Any reference will fail with

WARNING: undefined label: myfig

How to fix this?


Solution

  • docutils.parsers.rst.directives.images.Figure is not the right class to customize because it doesn't handle the name attribute. Use Sphinx's patched version, sphinx.directives.patches.Figure, instead.