Set alt attribute when publishing a figure

4 visualizaciones (últimos 30 días)
Jeff Mandel
Jeff Mandel el 20 de Feb. de 2025
Editada: Jeff Mandel el 22 de Feb. de 2025
I am using Publish to write some documentation that will be included in a larger website. I would like to be able to properly caption my figures:
function testAlt
figure(1);
x=0:.1:2*pi;
y=sin(x);
plot(x,y);
set(gcf,'Tag','Sine wave');
Produces:
<img vspace="5" hspace="5" src="testAlt_01.png" alt="">
What I want is:
<figure>
<img vspace="5" hspace="5" src="testAlt_01.png" alt="">
<figcaption>Sine wave</figcaption>
</figure>
Which would render as:
Looking at toolbox/matlab/codetools/private/mxdom2simplehtml.xsl, the <img> template contains:
<xsl:attribute name="alt"><xsl:value-of select="@alt"/></xsl:attribute>
I can modify this file to include:
<figure>
...
<figcaption><xsl:value-of select="@alt"/></figcaption>
</figure>
and I'll get my caption. Only problem is that there doesn't seem to be a way to propogate any property of gcf into the XML. @alt seems like the natural choice, and since someone wrote the code to consume it, perhaps someone could write the code to produce it.
My other suggestion on this is that embedding the css style sheet in an xsl file in a folder named "private" is painful. Why not provide a configuration parameter "cssfile" that would import the specified file into the document's head?
<link rel="stylesheet" type="text/css"><xsl:attribute name="href">u<xsl:value-of select="@cssfile"/></xsl:attribute></link>
Where I can put:
figure {
display: inline-block;
margin: 20px;
}
figure img {
vertical-align: top;
}
figure figcaption {
caption-side: bottom;
padding: 10px;
font-weight: bold;
font-size: 110%;
text-align: center;
}
  1 comentario
Jeff Mandel
Jeff Mandel el 21 de Feb. de 2025
Editada: Jeff Mandel el 22 de Feb. de 2025
A bit of a kludge, but until @alt is available, change the xsl to:
<figcaption>Figure <xsl:value-of select="substring(@src,string-length($title)+2,2)"/></figcaption>
This takes the name of the image file (myfunction-xx.png) and pulls out the xx, so the first figure will be "Figure 01". Yes, I could probably figure out how to suppress the leading zero.
What would be a nice feature would be the ability to define an XML variable in the script, so you could cross reference to the figure, something like:
setXMLtag('fignum', 1);
setXMLtag('caption', 'Money as a function of time');
%%
% As can be seen in Figure @fignum, time is money
plot(time,money)

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by