LJ Archive

LaTeX2HTML: Publish Science to the Web

Michael Yuan

Issue #92, December 2001

Tailoring math-intensive documents to fit the Internet.

Ironically, 20 years after the advent of the World Wide Web, it is still a struggle to publish math-intensive scientific documents on the Web. New standards like MathML have been out, but before the popular web browsers support them, the best way to reach a maximum audience is still to put math formulae as in-line graphics in HTML documents.

In the off-line world, TeX/LaTeX packages are known to produce the best formatted scientific documents. LaTeX is, in fact, a standard format for many academic journals. For scientists and researchers, the best of two worlds is to write in LaTeX and then publish on the Web. LaTeX2HTML is the perfect tool for that.

LaTeX2HTML originally was written by Nikos Drakos in 1993 as an open-source project. It was improved and enhanced by many members of the Open Source community. LaTeX2HTML converts LaTeX documents to standard HTML format. Since HTML is a text format, it converts all the nontext elements into in-line graphics. It is a Perl application and runs on many platforms including most flavors of UNIX/Linux and Windows.

In this article, I first give an example of how to convert a simple LaTeX document to HTML and discuss how the program handles in-line graphics and style files. I then explain the LaTeX2HTML-specific LaTeX commands/environments. Finally, I discuss a more creative use of LaTeX2HTML—using it on the server side. For simplicity, the term formula refers to anything that is in LaTeX's math mode. That includes simple in-text math symbols, “displaymath” formulae and numbered equations.

A Simple Example

The usage of the command is very straightforward. If you want to publish your LaTeX document, mydoc.tex, to the Web, you run

latex2html -local_icons mydoc.tex

LaTeX2HTML creates a new subdirectory (mydoc/) and puts all HTML files and image files in it. You can now copy mydoc/ under the document tree of your web server, and your document is available at http://yourserver/path/mydoc/index.html.

The title, headings and emphasized text are marked up properly with HTML tags. All the formulae/tables/figures and anything that normal HTML cannot display are converted into in-line graphics.

If mydoc.tex has several sections/subsections, LaTeX2HTML generates an HTML file for each section and a table of contents with links to each section. Each page has a navigation bar with links to next/prev sections and the table of contents. If you want only one big HTML file, use the -split 0 command-line switch.

LaTeX2HTML needs to know where to find the icon images on the navigation bar. The simplest solution is to use the -local_icons command-line switch, which instructs LaTeX2HTML to copy the navigation icons into the directory mydoc/. You also can use custom navigation icons by replacing mydoc/*motif*.gif files with your own.

In-Line Graphics

I use math formulae as an example to explain how LaTeX2HTML handles in-line graphics. It does the same thing for floating objects such as figures and tables.

LaTeX2HTML scans through mydoc.tex and extracts all math formulae into a file called images.tex. In images.tex, each formula is on a separate page. It then calls latex images.tex and dvips -S 1 -i to generate a one-page PostScript file for each formula. Ghostscript is used to convert and resize PostScript files into GIF or PNG images. LaTeX2HTML will remember the image names and insert the links back to HTML files generated from mydoc.tex.

There are several command-line switches to control the image generation process. For example, switch ps_images instructs LaTeX2HTML to use links to external PostScript files rather than in-line graphics.

Style Files

LaTeX2HTML handles style files differently for text and in-line graphics. LaTeX2HTML ignores any style file when it generates main text HTML files from mydoc.tex. However, style files often define new commands and environments that cannot be ignored. As a workaround, LaTeX2HTML allows users to rewrite the style file in Perl format. When LaTeX2HTML encounters a style file, it searches for the Perl translation and incorporates it into the main script. Fortunately, many popular style files already have been translated into Perl. To write such translations requires a working knowledge of the LaTeX2HTML internal structure. It is a difficult task, but you have the source code.

We want formulae, figures and tables to look exactly the same as they would print out on paper, with all the custom symbols, fonts and spacing. So, images.tex includes all the style files used by mydoc.tex. Since images.tex is processed by LaTeX, the style files are handled correctly.

Write for the Web

LaTeX2HTML is powerful in converting existing documents into web format. LaTeX2HTML offers a set of custom LaTeX commands and environments that allow you to include your own HTML markups in the text. You can take full advantage of normal HTML: you can add web forms, clickable image maps, external links/graphics or even Java applet/script to your document. For example, if you want to include a link to an outside web page in your generated HTML pages, you can just insert the following to mydoc.tex:

\htmladdnormallink{link title}{http://yourlink}

or more generically,

<A HREF="http://yourlink">link title</a>
If you want to print out a paper version of that HTML-enhanced document, you can include style file html.sty and then process it with normal LaTeX. Most of those LaTeX2HTML-specific commands/environments are ignored by LaTeX.

On the Server Side

LaTeX2HTML also can be used at the server side to facilitate web-based mathematical communications. Listing 1 demonstrates a Perl function that takes in a math-mode LaTeX string and returns an HTML markup for an image that displays the formula. There are other ways to do it more efficiently, but this is just an illustration of what LaTeX2HTML can do. It can be used in chat rooms or bulletin boards to allow the user to input and broadcast complex math formulae.

Listing 1. Perl Function Converting a LaTeX String to an HTML Markup


email: juntao@astro.as.utexas.edu

Michael Yuan is a PhD candidate in Astrophysics at University of Texas at Austin. He studies remote quasars (20-plus billion light years away) to understand the history and evolution of our universe. When he is not observing quasars, he enjoys developing useful software using earthly languages such as Java and Perl.

LJ Archive