10

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 a generated image, 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 many LaTeX files, which can be updated, this would mean having to specify all 'alt' again, even for minor updates on the original files.

1 Answer 1

8

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

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

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

\newtoks\eqtoks  
\def\AltMath#1${\eqtoks{#1}%  
\Picture*[\HCode{\the\eqtoks}]{ align="middle"}$#1$\EndPicture$}  
\Configure{$}{}{}{\expandafter\AltMath}  

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:

\Preamble{xhtml}
\begin{document}
\def\MyDisplay{\leavevmode\EndP\HCode{<!--l. \the\inputlineno-->\Hnewline}\IgnorePar\Tg<div class="math-display">}
\def\EndMyDisplay{\HCode{<!--l. \the\inputlineno-->\Hnewline}\Tg</div>}
\def\AltMathII#1{%
\Picture*[\HCode{\the\myalt}]{}$$#1$$\EndPicture$$%
}
\def\AltMath#1$${\AltMathII{#1}}
\def\AltMathI#1\]{\AltMathII{#1}\egroup\EndMyDisplay}  
\Configure{$$}{\MyDisplay}{\EndMyDisplay}{\expandafter\AltMath}
\Configure{[]}{\MyDisplay$$\AltMathI}{$$}  
\Css{.math-display{
text-align:center;
margin: 5px;
}}
\EndPreamble

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

\ProvidesPackage{myalt}
\newtoks\myalt
\newcommand\mymathinfo[2]{%
    \NextPictureFile{#1}
    \myalt{#2}
}

Now you can use

\documentclass{article}
\usepackage{myalt}
\begin{document}
Hello world
\mymathinfo{hello}{hello world}
$$a=\frac{\sqrt{4b}}{c}$$
\mymathinfo{world}{cool}
$$  
 \forall x \in X, \quad \exists y \leq \epsilon
$$
\end{document}

compile with

htlatex myfile "myalt"

resulting in:

<!--l. 8--><p class="noindent" >Hello world
</p><!--l. 10--> 
<div class="math-display">
<img 
src="hello.png" alt="hello world"  />
<!--l. 10--> 
</div>
<!--l. 12--> 
<div class="math-display">
<img 
src="world.png" alt="cool"  />
<!--l. 14--> 
</div>
3
  • 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
    Commented Feb 10, 2012 at 15:50
  • 1
    I edited my answer to work with [ .. ]
    – michal.h21
    Commented Feb 10, 2012 at 18:10
  • That was just perfect! :)
    – Daniel
    Commented Feb 10, 2012 at 20:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .