Set alt attribute when publishing a figure

5 visualizzazioni (ultimi 30 giorni)
Jeff Mandel
Jeff Mandel il 20 Feb 2025
Modificato: Jeff Mandel il 22 Feb 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 Commento
Jeff Mandel
Jeff Mandel il 21 Feb 2025
Modificato: Jeff Mandel il 22 Feb 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)

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by