[Tex/LaTex] LaTeX –> (X)HTML with tex4ht: bad quality images of equations

tex4ht

I'm using tex4ht to produce XHTML files from a LaTeX file. The XHTML file I obtain uses images for some equations which tex4ht could not convert directly to XHTML.

Problem: images of equations have very bad quality.

What I want: Better quality images.

More info on attempts & system:

I'm using MikTeX 2.9 under Windows Vista. I go to the command line and type htlatex document.tex "xhtml" to get the resulting XHMTL file.

After doing some search on the web, I tried to change the file tex4ht.env as mentioned in here (changing -density 110x100 to -density 220x220 -geometry 50%) in all locations I could find in my system (within the MikTeX path and also in the AppData windows directory) but the results are the same. I obtain during the compilation of hlatex the following line (among others):

System call: mgs -sDEVICE=pngalpha -sOutputFile=Untitled11x.png
-r110x110 -dEPSC rop -dBackgroundColor=16#ffffff -dTextAlphaBits=2 -dGraphicsAlphaBits=2 -q -dbat ch -dNOPAUSE zzUntitled1.ps -c quit

In particular, I would guess that the -r110x110 means resolution was still set to 110×110, despite my changes to the file tex4ht.env.

Any suggestions on how to improve the quality of equations images?

Best Answer

The env file is stored in %texmf-home%/texmf-dist/tex4ht/base/unix or %texmf-home%/texmf-dist/tex4ht/base/win32, depending on your operating system. For testing purposes, it is best to make copy of that file in your working directory.

The structure of this file is little bit strange:

<tag>
Gsome command and the parameters
 Ganother command
</tag>
 <anothertag>
GNext command
 Gcommand again
 </anothertag>

Spaces are important. All lines with space at the beginning are ignored. Tagged sections are sometimes ignored. Quoting tex4ht.env itself:

Tagged script segments ... are scanned only if their names are specified within -ctag switches of tex4ht.c and t4ht.c. When -c switches are not supplied, a -cdefault is implicitly assumed.

In the example, only GNext command is actually executed. If you look at actual tex4ht.env file, you can see that there are several configurations for output of images. Default is

 <convert>
 G.png
 Gdvips -E -q -Ppdf -f %%1 -pp %%2 > zz%%4.ps
 Ggs -sDEVICE=pngalpha -sOutputFile=%%3 -r110x110 -dEPSCrop -dBackgroundColor=16#ffffff -dTextAlphaBits=2 -dGraphicsAlphaBits=2 -q -dbatch -dNOPAUSE zz%%4.ps -c quit
 Grm zz%%4.ps    
G.svg
Gdvips -Ppdf -mode ibmvga -D 110 -f %%1 -pp %%2 > zz%%4.eps
Gpstoedit -f svg zz%%4.eps %%3      
G.
Gdvips -E -Ppdf -mode ibmvga -D 110 -f %%1 -pp %%2  > zz%%4.ps
Gconvert -trim +repage -density 110x110 -transparent '#FFFFFF' zz%%4.ps %%3
 </convert>

lines

G.
Gdvips -E -Ppdf -mode ibmvga -D 110 -f %%1 -pp %%2  > zz%%4.ps
Gconvert -trim +repage -density 110x110 -transparent '#FFFFFF' zz%%4.ps %%3

means, that default conversion uses dvips command to convert picture to the eps file and then using convert to the final output format.

There is also section

<dvipng>
G.png
Gdvipng -T tight -x 1440 -D 96 -bg Transparent -pp %%2:%%2 %%1 -o %%3
G.gif
Gdvipng -T tight -x 1440 -D 96 -bg Transparent -gif -pp %%2:%%2 %%1 -o %%3
G.
Gdvips -Ppdf -mode ibmvga -D 110 -f %%1 -pp %%2 > zz%%4.ps
Gconvert -crop 0x0 -density 110x110 -transparent '#FFFFFF' zz%%4.ps %%3
Grm zz%%4.ps
</dvipng>

Which uses dvipng instead of convert and dvips and in my opinion the output is better. So to use this configuration, just erase spaces before <convert> and </convert>, and add space at the beginning of <dvipng> and </dvipng>

Another opinion is to use svg images instead of the png. Just add :

G.svg
Gdvisvgm -n -p %%2 -c 1.2,1.2 -s %%1 > %%3

Create the file myconfig.cfg:

\Preamble{xhtml}
\Configure{Picture}{.svg}
\begin{document}
\EndPreamble

and call:

htlatex file "myconfig"

Edit:

Another option is to to use make4ht build system. You can specify image conversion command in the build file, see the chapter on that matter