Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have two files named a.rst and b.rst, both of which contain a good deal of text. In a.rst, I define a figure:

.. figure:: ../images/some-image.png
    :scale: 70%
    :align: center
    :alt: Some Text
    
    Some Caption

I would like to have the same image and caption in b.rst with the same figure number, But repeating the above code gives me a new figure.

As a compromise, I can refer to this image in b.rst using the :numref: directive, but that does not resolve to the figure. It only displays the name as a piece of code.

I understand that these are two question, but I think they are sufficiently related. How can I repeat or reference a figure defined in a rst file in another file.

Edit to elaborate on the expected output: I want the resulting files to have the following content:

  • a.html:
Fig. 1 + caption
Fig. 2 + caption
Fig. 3 + caption
  • b.html:
Fig. 4 + caption
Fig. 2 + caption
Fig. 5 + caption

Effectively, this would add the figure to b.rst not as a separate entity, but merely as a mirror of what was in a.rst. This is similar to what was discussed here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
265 views
Welcome To Ask or Share your Answers For Others

1 Answer

Assuming you want to repeat the exact same content in two different files, put that content into a separate file, then include that file wherever you want it to appear.

includeme.rst

Note document root relative path.

.. figure:: /images/some-image.png
    :scale: 70%
    :align: center
    :alt: Some Text
    
    Some Caption

a.rst and b.rst

Below this paragraph should appear an image.

..include:: /includeme.rst

EDIT

To remove the figure number, set numfig to False. This will avoid the incongruent figure numbering, but won't solve it. I think that's the best you can achieve, as Sphinx automatically numbers figures (and other objects) otherwise.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...