I'm using tex4ht to translate LaTeX to (X)HTML. Tex4ht automatically generates images of equations it could not translate directly into text.

Problem: the filename of the image generated, as well as the corresponding 'alt' text is meaningless.

Want: A way to embed in the LaTeX code the filename of image generated, as well as the 'alt' text.

Partial solution: I managed to define the filename by using the command? \NextPictureFile{NameToBeUsed} just before an equation. But I'm unable to specify the 'alt' text. Any suggestions?

Of course, an option is to edit the generated HTML pages by hand to specify the 'alt' text. But since this is to be done on a large number of LaTeX files, which can be updated, this would mean having to specify all 'alt' again, even for minor updates on the original files.


Tex4ht uses command \Picture ... \EndPicture to convert enclosed text to images. Command of the form

\Picture*[alt text]{align="center}
some text

will generate

<img src="myfile01x.png" alt="alt text" align="center />

with myfile01x.png containing text "some text".

Acording to tex4ht faq, you can use similar code

\Picture*[\HCode{\the\eqtoks}]{ align="middle"}$#1$\EndPicture$}  

to typeset TeX code of the math as alt text. We can modify it to work with display math and to use our own macro containing alt text.

Create file myalt.cfg:

\def\MyDisplay{\leavevmode\EndP\HCode{<!--l. \the\inputlineno-->\Hnewline}\IgnorePar\Tg<div class="math-display">}
\def\EndMyDisplay{\HCode{<!--l. \the\inputlineno-->\Hnewline}\Tg</div>}
margin: 5px;

then myalt.sty containing macro, which sets the alt text and filename:


Now you can use

Hello world
\mymathinfo{hello}{hello world}
 \forall x \in X, \quad \exists y \leq \epsilon

compile with

htlatex myfile "myalt"

resulting in:

<!--l. 8--><p class="noindent" >Hello world
</p><!--l. 10--> 
<div class="math-display">
src="hello.png" alt="hello world"  />
<!--l. 10--> 
<!--l. 12--> 
<div class="math-display">
src="world.png" alt="cool"  />
<!--l. 14--> 
  • That worked just fine (and improved a lot my understanding of tex4ht)! :) Only one last question. Some of the LaTeX files are generated by Scientific Workplace (a frontend for LaTeX. Some contributors of the LaTeX files use it) which uses "\[ ... \]" for equations instead of "$$..$$". How can I modify the code above to deal with the later case? I tried some variants (replacing "$$" with "\[" and "\]") without success. – Daniel Feb 10 '12 at 15:50
  • 1
    I edited my answer to work with [ .. ] – michal.h21 Feb 10 '12 at 18:10
  • That was just perfect! :) – Daniel Feb 10 '12 at 20:04

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.