sphinx-doc / sphinx

The Sphinx documentation generator
https://www.sphinx-doc.org/
Other
6.59k stars 2.12k forks source link

Labels for images with :only: directive #4242

Open epetragl opened 7 years ago

epetragl commented 7 years ago

Subject: Labels for images with :only: directive

Problem

Hello, I would like to add a label to an image with only directive. I'm trying different combinations but I still get compilation errors.

Procedure to reproduce the problem

Here one example of what I'm trying to do.

Version A)

.. _img_label:

.. only:: html

  .. figure:: imgname.png
    :width: 800px
    :align: center
    :alt: This is the image caption

    This is the image caption

.. only:: latex

  .. figure:: imgname.png
    :width: 400px
    :align: center
    :alt: This is the image caption

    This is the image caption

Version B)

.. only:: html

  .. _img_label:

  .. figure:: imgname.png
    :width: 800px
    :align: center
    :alt: This is the image caption

    This is the image caption

.. only:: latex

  .. _img_label:

  .. figure:: imgname.png
    :width: 400px
    :align: center
    :alt: This is the image caption

    This is the image caption

Error logs / results

Error for Version A)

WARNING: undefined label: img_label

Error for Version B)

WARNING: Duplicate explicit target name: "img_label".

Expected results

The goal is to have an image which shall have different dimensions depending on the desired output format (html or pdf). This image shall posses an unique label in order to be correctly cited inside my document using the :numref:img_label command.

Environment info

dbisu commented 2 years ago

I'm hitting this same problem on Sphinx v5.1.1 Are there any solutions to this issue?

michele-riva commented 5 months ago

Problem still persist as of Sphinx v7.3.7

I'm happy to help with a PR, but I would need some pointers.