Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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>
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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