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



Look at picture


Nice, isn't it?


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

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

tex4ht generates

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

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

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


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.

  • could you please make a mwe? This might depend on surrounding code. – michal.h21 Sep 10 '16 at 10:18
  • @michal.h21 MWE added. – mmj Sep 10 '16 at 16:04

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


\ConfigureEnv{figure}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="figure">}\HtmlParOff}

\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:





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


\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
   <div class="figure">

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

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" />
<!--l. 14--><p class="indent" >   Nice, isn&#x2019;t it?

enter image description here

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.