Vertical positioning and scaling of (not only) math images with tex4ht
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


warning: this package is experimental


With tex4ht, it is possible to convert LaTeX files to various output formats. Certain elements with difficult formating, typically math content, can be converted to images. Unfortunately, when these images are used in the running text, they can have wrong vertical placement or wrong size.

This package enables to precisely place these converted images, in terms of the vertical alignment and correct setting of the dimensions.


There are four steps needed for correct use of this package

  1. In your document, use package mathdimen For math you must use \( \) or \[ \] commands. commands $ and $$ are not supported
  2. Create custom file tex4ht.env and configure dvi to image conversion
  3. [Optional] Create custom configuration file and change the appearance of tags
  4. You must run first latex or pdflatex and then htlatex (you must run LaTeX only when you change some math, this step is needed for saving of correct dimensions)

Sample file


Sample text with some math inside \(c=\sqrt{a^2+b^2}\). Text now continues. \(\sqrt[n]{1+x+x^2+x^3+\ldots}\)

Output without matdhimen:

You can see wrong vertical position

and with mathdimen, using svg image for math

Slightly better vertical alignment

Configure image conversion with tex4ht.env

Conversion of pictures from TeX's dvi format to outher formats like png or svg is configured in the file called tex4ht.env.

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:

Gsome command and the parameters
 Ganother command
GNext command
 Gcommand again

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

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


Gdvips -E -Ppdf -mode ibmvga -D 110 -f %%1 -pp %%2  >
Gconvert -trim +repage -density 110x110 -transparent '#FFFFFF' %%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

Gdvipng -T tight -x 1400 -D 72 -bg Transparent -pp %%2:%%2 %%1 -o %%3
Gdvipng -T tight -x 1400 -D 72 -bg Transparent -gif -pp %%2:%%2 %%1 -o %%3
Gdvips -Ppdf -mode ibmvga -D 110 -f %%1 -pp %%2 >
Gconvert -crop 0x0 -density 110x110 -transparent '#FFFFFF' %%3

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>

With mathdimen, it is possible to use one image format as the preferred and other as the fallback, for example prefer the svg and if the viewer doesn't support svg, use png as fallback. Configuration for such case can be:

Gdvipng -T tight -x 1400 -D 92 -bg Transparent -pp %%2:%%2 %%1 -o %%3
Gdvisvgm -n -p %%2 -c 1.2,1.2 -s %%1 > %%3.svg

Sample tex4ht.env configuration can be found in the sample directory.

If you want to use the images with device, that has bigger screen resolution than PC monitor, for example some e-ink device, you can set the resolution of the image to higher values. You should set higher resolution also in the case, if you want to be able to resize the html page, only be aware that in some browsers (IE), subsampled images don't look good.

Configuration of the hooks

tex4ht can be configured using so called hooks. mathdimen provides some hooks, allowing you to change some aspects of the process.

To configure the hooks, you can create custom configuration file, for example myfile.cfg

\Preamble{xhtml, charset=utf-8}
\Configure{EmptyMath}{\Tg<object data="\GetPictureName .png.svg" type="image/svg+xml" style="height:\height; vertical-align:-\depth;"/>}{}{}
\Configure{PictureMath}{}{\Tg</object>}{ style="height:\height; vertical-align:-\depth;"}

There are three commands that you can use with \Configure{AltMath}:

  • \EmptyMath - for referencing existing images
  • \PictureMath - for convesion math into image
  • \MathmlMath - for math without conversion - with mathml option it can be mathml

Every one of these commands can be also configured. Command \Tg<tagname> inserts html tag, we use inline css to set vertical alignment and height using \depth and \height commands. All dimensions are in em. The formula for computing the dimensions is saved dimension in pt / ConvertPoints. Default ConvertPoints configuration is \Configure{ConvertPoints}{10}{em}. This sample configuration will generate image of this form:

<object data="sample0x.png.svg" type="image/svg+xml" style="height:1.248em; vertical-align:-0.16573em;"/><img 
src="sample0x.png" alt="    √ -2----2
c =   a  + b  "  type="image/svg+xml" style="height:1.248em; vertical-align:-0.16573em;" /></object>.

For output in ePub3 format

<epub:switch >
    <epub:case required-namespace="">
        <math xmlns=""> 

    <epub:case required-namespace="">
        <img src="picname.svg" type="image/svg+xml" style="height:1.2em; vertical-align:-0.3em;"/>

         <img style=" {baseline-positioning-code} " src="picname.png" style="height:1.2em; vertical-align:-0.3em;" 
                  alt=" {math speech text} "/>

one can use similar configuration


and configure MathmlMath, EmptyMath and PictureMath to output the right tags


First of all, run

latex sample

on your file, so the dimensions are saved into sample.mtd file. Then run

htlatex sample basename_of_your_configfile

in case you want mathml, utf8 and your config file is named mycfg.cfg try this:

htlatex sample "mycfg, mathml, charset=utf-8" " -cunihtf -utf8"


  • support for display math is missing
  • sometimes there are roundingerrors in dimensions calculations
  • I don't know, how to add support for $ and $$ commands


  • add more lowlevel details into the documentation
  • do more testing