Adding title attribute to an image in rst
Asked Answered
B

3

6

Seems like restructured text markup is very limited when it comes to image options:

The following options are recognized:
alt : text
height : length
width : length or percentage of the current line width
scale : integer percentage (the "%" symbol is optional)
align : "top", "middle", "bottom", "left", "center", or "right"
target : text (URI or reference name)

Is it possible to set some custom attributes like title via reStructuredText markup?

e.g.

.. image:: foobar.jpg 
    :title: mouse over text, hi!

Would output:

<img src="foobar.jpg" title="mouse over text, hi!"></img>
Bloodworth answered 29/1, 2018 at 8:58 Comment(1)
Possible duplicate of How I can add attribute to img tag in PelicanHenryhenryetta
A
3

According to docutils, the following options are recognized: alt, height, width, scale, align, target, class, and name. No title.

Options include rewriting in the client with JavaScript or work with the docutils team to make a feature request and implement it.

Admeasure answered 29/1, 2018 at 9:39 Comment(2)
Yes, I pointed this out in my question. Having some javascript to move alt attribute to title is a great idea though! I'll consider a PR to docutils too :)Bloodworth
The alt attribute isn't really intended to be a caption, the way a title sometimes is, but to describe the image in case it can't be displayed; either because it did not load or for screen readers. e.g. <img src="images/dinosaur.jpg" alt="The head and torso of a dinosaur skeleton; it has a large head with long sharp teeth" title="A T-Rex on display in the Manchester University Museum"> developer.mozilla.org/en-US/docs/Learn/HTML/…Indochina
F
5

You can use figure

.. figure:: picture.png

   This is the caption of the figure.
Fireback answered 1/7, 2021 at 12:37 Comment(1)
This results in the caption showing up below the image. While this can be very useful, it is different from adding a title attribute in <img> (which makes the text show up when hovering the mouse over the image).Weiman
A
3

According to docutils, the following options are recognized: alt, height, width, scale, align, target, class, and name. No title.

Options include rewriting in the client with JavaScript or work with the docutils team to make a feature request and implement it.

Admeasure answered 29/1, 2018 at 9:39 Comment(2)
Yes, I pointed this out in my question. Having some javascript to move alt attribute to title is a great idea though! I'll consider a PR to docutils too :)Bloodworth
The alt attribute isn't really intended to be a caption, the way a title sometimes is, but to describe the image in case it can't be displayed; either because it did not load or for screen readers. e.g. <img src="images/dinosaur.jpg" alt="The head and torso of a dinosaur skeleton; it has a large head with long sharp teeth" title="A T-Rex on display in the Manchester University Museum"> developer.mozilla.org/en-US/docs/Learn/HTML/…Indochina
M
3

Alternatively you could simply use the "raw" directive and directly use your ideal target html

.. raw:: html

    <img src="foobar.jpg" title="mouse over text, hi!"></img>
Meteoric answered 26/8, 2022 at 16:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.