2

Compiling (Tex Live 2016 under Windows 10) the following LaTeX MWE (mwe1.tex)

\documentclass{book}
\usepackage{graphicx}

\begin{document}

Look at picture

\begin{figure}
\includegraphics[width=5cm]{myimage.png}
\end{figure}

Nice, isn't it?

\end{document}

with (an image named myimage.png must be provided)

htlatex "mwe1.tex" "xhtml,charset=utf-8,fn-in" " -cunihtf -utf8"

tex4ht generates

...
<body 
>
<!--l. 7--><p class="noindent" >Look at picture
</p>
   <hr class="figure" /><div class="figure" 
>



<!--l. 10--><p class="noindent" ><img 
src="myimage.png" alt="PIC"  
 />

</p>
   </div><hr class="endfigure" />
<!--l. 13--><p class="indent" >   Nice, isn’t it?
</p>

</body>
...

I'd like to customize this HTML output by getting rid of the unneeded <p> tag wrapping <img...> and, if possible, by giving some meaningful value to the alt attribute.

I know that image is enclosed in a paragraph in LaTeX source, but I don't need it in HTML source (especially if it is placed around <img> instead of around <div class="figure">...</div>).

This answer deals with the subject but I wasn't able to find a solution by modifying it.

Can I achieve this result by a custom configuration file or by some other means?

Thanks in advance.

mmj
  • 1,702

1 Answers1

4

It is better to configure the figure environment and disable paragraphs inside it (I hope that you don't use paragraphs inside \caption command):

\Preamble{xhtml}

\begin{document}
\ConfigureEnv{figure}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="figure">}\HtmlParOff}
{\HCode{</div>}\HtmlParOn\par}{}{}
\EndPreamble

\HtmlParOff ... \HtmlParOn disables the paragraphs.

Regarding the image alt attribute, we can add new option for the \includegraphics command. We must create new package, say altgraphicx.sty, which will provide default definition of this key:

\ProvidesPackage{altgraphicx}

\RequirePackage{graphicx}

\define@key{Gin}{alt}{}

\endinput

Then, we must provide tex4ht configuration for this package, altgraphicx.4ht:

\define@key{Gin}{alt}{\Configure{GraphicsAlt}{#1}}
\endinput

\Configure{GraphicsAlt} is used to store contents of the alt attribute.

You can now use \includegraphics in the following way:

\includegraphics[width=20cm,alt={Image description}]{myimage.png}

Generated code:

<!-- language: lang-xml -->

<!--l. 6--><p class="noindent" >Look at picture
</p>
   <div class="figure">

   <a 
 id="x1-21"></a><hr class="float" /><div class="float" 
>


<img 
src="myimage.png" alt="Image description"   
width="569.05511pt" height="186.6778pt"  />
 <div class="caption" 
><span class="id">Figure 1: </span><span  
class="content">hello my image</span></div><!--tex4ht:label?: x1-21 -->

   </div><hr class="endfloat" />
   </div>
<!--l. 14--><p class="indent" >   Nice, isn&#x2019;t it?
</p>

enter image description here

michal.h21
  • 50,697
  • Thanks again. Since I already have a custom tex4ht configuration of figure environment, I just added \HtmlParOff and \HtmlParOn to my configuration and it worked. – mmj Sep 10 '16 at 17:42
  • @mmj see the updated answer – michal.h21 Sep 10 '16 at 18:26
  • The solution that allows specification of alt attribute works wonderfully, you are a wizard! – mmj Sep 10 '16 at 22:15