TeX to GIF or PNG
Converter for Web Documents

by John Walker

When preparing technical documents for the World-Wide Web, there's frequently a need to include equations in the text. Since HTML doesn't provide direct support for mathematical typesetting, equations must be included as inline images. If you use TeX or LaTeX to write conventional documents, you already know how to format equations in those languages. Wouldn't it be nice to painlessly use TeX equations in your Web documents?

If you want to convert an entire LaTeX document into HTML, use LaTeX2HTML, by Nikos Drakos. But if you just want to typeset some equations for a new Web document, it's cumbersome to first write your document in LaTeX, convert it into HTML, then add links and additional functionality in the HTML text. Also, it's nice to be able to control the size of an equation image and the resolution with which it's rendered equation-by-equation; this isn't possible with a bulk document translator such as LaTeX2HTML.

Textogif is a simple Perl program which uses the same software tools employed by LaTeX2HTML to compile individual equations in short LaTeX files into images suitable for embedding in Web documents.

To use textogif, write your equation (or anything else you can typeset with LaTeX) in a file like:


\int H(x,x')\psi(x')dx' = -\frac{\hbar^2}{2m}\frac{d^2}{dx^2}


The "\pagestyle{empty}" is required to avoid generating a huge image with a page number at the bottom. Then (assuming you have all the software prerequisites installed properly), you can simply say:

    textogif filename
to compile filename.tex to filename.gif, an interlaced, transparent background GIF file ready to use an an inline image. You can specify the base name, for example, "schrod", rather than the full name of the TeX file ("schrod.tex"), and use wild cards to process multiple files. TeX requires, however, that all files have an extension of ".tex". A sample <img> tag, including the image width and height is printed on standard error, for example:
    <img src="schrod.gif" width="508" height="56">
Here's what you'll see when you include the image for the equation:

\int H(x,x')\psi(x')dx' = -\frac{\hbar^2}{2m}\frac{d^2}{dx^2} \psi(x)+V(x)\psi(x)

GIF and PNG files created by textogif are interlaced to permit incremental display by browsers which provide that capability, and have a transparent background so they'll look good regardless of the background on which they're placed.

Command line options

Command line options allow you to control the size of the image generated for the equation, the degree of antialiasing used to avoid jagged edges in the text, the format of the generated image, and the background against which it is displayed. All options may be abbreviated to their shortest unique prefix.

-dpi nnn

The -dpi option is followed by an integer which controls the size of the generated image by giving the "dots per inch" for the image. The default of 150 creates images of reasonable size for most browsers, for example:

Larger values create larger images. A value specification of "-dpi 300" would double the size of the equation to:

while "-dpi 75" would create an image half the size of the default:

Smaller images like this often look better if set in bold face type. The LaTeX command "\bf" within the displaymath environment produces the more readable:


Select GIF format output. This is the default.

-grey n.nn

Specify the grey scale value of the background as a floating point number with 0 representing black and 1 white. The default value is 1 (white), which produces images which look best against a white background. If, for example, you use a 75% grey background (HTML colour code "#C0C0C0"), specifying "-grey 0.75" will create an image optimised for such a page.


Print how-to-call information including a summary of the options.


Select Portable Network Graphics (PNG) format output.

-res n.nn

The -res option is followed by a floating point number which specifies the degree of antialiasing used to smooth characters in the image. The specification is given in terms of the size of the actual output image divided by the size of the original image used to produce it. The default value, 0.25, produces an image with four times the desired dots per inch, then scales it, smoothing jagged edges in the process.

Smaller values for "-res" produce greater smoothing, but at the cost of increased memory and disc space requirements as the image is being generated. If your system's resources are insufficient to create an image, increase the value of "-res" until it can be successfully processed. Larger values of "-res" reduce the antialiasing and yield rougher contours in the text. A value of 1.0 disables antialiasing entirely. Here is the same equation processed with different "-res" settings.

q_0=\left[-\frac{1}{RH_0^2}\frac{d^2R}{dt^2}\right]_0           -res 0.25

q_0=\left[-\frac{1}{RH_0^2}\frac{d^2R}{dt^2}\right]_0           -res 0.5

q_0=\left[-\frac{1}{RH_0^2}\frac{d^2R}{dt^2}\right]_0           -res 1.0


Print version information.

Downloading and Installation

Textogif may be downloaded as a GZIP compressed Unix tar archive from the following links. Version 1.1 is the current release; it is compatible with current releases of GNU Ghostscript (having been tested most recently with version 6.52). Version 1.0, originally released in 1995, is compatible with older versions of Ghostscript (it was developed with version 2.6.1) which did not support direct export of image files.
Once you'be downloaded the archive, extract the textogif Perl program from it and place the program somewhere on your program search path. If Perl is installed on your system in a location other than the more or less standard /usr/bin/perl, you'll need to change the first line of textogif accordingly.

Software Prerequisites

Textogif is a small Perl program which relies upon the following software to do the real work. You'll need to have all the following packages properly installed on your system in order to use Textogif. The version numbers are those I had installed on my machine when I developed this program; later versions should work OK as long as they're upwardly compatible with those cited. Given the number of Unix-specific dirty tricks employed by Textogif, it is extremely unlikely it will work under other operating systems without modifications.
   Perl        5.8.0 (4.036 or above should work)
   TeX         3.14159 (Web2C 7.3.1)
   LaTeX2e     <2000/06/01> (Included with most TeX distributions)
   dvips       dvipsk 5.86 (Included with most TeX distributions)
   Ghostscript 6.52 (2001-10-20)
   Netpbm      9.24

by John Walker
May, 1995
Version 1.1: November, 2003

Prior Releases

Fourmilab Home Page