nextnext page arrow

myphysicslab – displaying math in html

displaying math on web pages with html is challenging. there are many approaches. here are some notes on what i've learned and the solutions i'm using (for now).

goals and requirements

what solution you choose for displaying math depends on your goals and requirements. here are some factors to consider:

how i display math on the web

my reviews of the various options for displaying math on the web are given below. to cut to the chase, here is the solution that i came up with.

i use raw html and css (cascading style sheets) for most of the math display on myphysicslab. in addition i'm using a modified version of gladtex to create bitmap images of the fancier equations.

i would prefer to be writing math as latex expressions within my html files that is then pre-processed to mathml. but mathml seems to be not quite ready for wide-spread use. mathml is built-in for very few browsers, so a user must download a special player which may not be available for all systems or browsers.

i sometimes have as many as 50 or more equations on a page, so i felt that converting them all to images would result in the page loading too slowly. also, i would like to allow the user to change the font size of the text and math in the browser – but images do not adapt to font size changes. and the print quality is better with text-based math than with images.

my primary target is html pages, so i want a solution that lets me write html. therefore i stayed away from tools where you write the entire page in latex which is then converted to html. i felt that latex to html conversion would not give me enough control over the resulting html.

i use a macro pre-processor to define macros that i use in creating the html math. for example i can write
     #define eqn_start      <span class="display_eqn">
which defines a macro that i can place before each equation. this makes for less typing, and if i need to change what comes before each equation i can do so in one place – by changing the macro (and re-processing the pages). i can also make macros that take one or more arguments. for example
     #define sup(1)      <sup>#arg#</sup>
is a macro for making a superscript. the #arg# is replaced by whatever is passed in to the macro.

i wrote my own macro pre-processor but there are probably better ones out there, such as htmlex. (if you are interested i'm happy to provide my macro pre-processor – written in c – under an open source license, just email me.)

since i use italics for variable names, i usually have several macros like this:
     #define _x      <i>x</i>
then i can just refer to _x in the html file, and the pre-processor turns that into the italicized version automatically.

writing math in html and css

you can look at the html source of any of the pages on my physics simulation website to see examples of writing math with html. (keep in mind that i use macros when authoring the math, so the actual html is more dense than what i work with). you can also look at the css style sheet for this website. here i'll explain some of the techniques i use.

a good reference for rendering math in html is jukka korpela's website. i use many of the basic techniques he describes. some are obvious such as using <sub> for subscripts, <sup> for exponents, or <b> for vectors.

a nice trick for small fractions that i found on jukka's website is this:
     <sup>1</sup>&frasl;<sub>2</sub>
which is rendered by html as 12. you can create a macro to make this easier to type. the fraction slash character &frasl; renders this a bit nicer than a regular slash (though older browsers might not recognize it at all).

i use many of the html 4.0 character entity references for greek characters such as
     &theta; = θ
     &pi; = π
     &omega; = ω
also available are some math symbols like
     &radic; = √
     &int; = ∫
     &sum; = ∑
compatibility? modern browsers support these, but it depends on the font. some font's have these symbols and others don't. i've seen a case where different sizes of the same font varied in whether they have these symbols.

i use &minus; instead of a dash. compare how they look: −1, -1. if you are using a mono-spaced font (like courier) then you can get away with using a dash instead.

i frequently use the non-breaking space &nbsp; to fix up spacing problems, in math and elsewhere.

using a css cascading stylesheet can help standardize the look of math across many pages and different browsers. for example, i define the styles for <sub> and <sup> with
     sup { vertical-align: 0.8ex; font-size:95%; }
     sub { vertical-align: -0.6ex; font-size:95%; }
this helps ensure that css compliant browsers show subscripts and superscripts as i want them to.

i define several css classes to control things like margins, centering, line breaks, default font, etc. for example, here is my class for inline math display:
     span.inline_eqn { white-space: nowrap; }
then in the html i would write something like
...the function <span class="inline_eqn">f(x) = x + 2</span> is a simple function...
which ensures that the inline equation is not split onto two lines when the browser renders the page.

i specify a bigger line height for display equations (equations that are on their own line) with { line-height: 2em } to help make the spacing between equations more even. this helps because then equations with or without subscripts or superscripts have about the same line height.

fractions with <table>

fractions can be made with html tables. i use the css border-top style to create the fraction line. here's an example:
x =   x2 + x + 1
2 cos(x)
here is the equation with the table borders turned on, so you can see where the cells are:
x =   x2 + x + 1
2 cos(x)
this is the html and css that produces the fraction.
<style>
    td.upper_line { border-top:solid 1px black; }
    table.fraction { text-align: center; vertical-align: middle;
        margin-top:0.5em; margin-bottom:0.5em; line-height: 2em; }
</style>

<table class="fraction" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td rowspan="2" nowrap="nowrap">
            <i>x</i> =  
        </td><td nowrap="nowrap">
            <i>x</i><sup>2</sup> + <i>x</i> + 1
        </td>
    </tr><tr>
        <td class="upper_line">
            2 cos(<i>x</i>)
        </td>
    </tr>
</table>
note that the part of the equation that is not in the fraction is put into a separate table cell that spans the rows of the table. i find that proper indenting helps to make the html more readable while authoring. i don't use macros for these tables because macros would obscure the indenting of the table.

at present, tables cannot be used for inline display – some browsers show them on a separate line. for example, here is a small table fraction
l
θ'
  that should appear within the text, not on a separate line. compatibility is not quite enough: on my systems, internet explorer shows it correctly; netscape navigator shows it inline, but not vertically centered; safari and opera put it on a separate line.

using tables for layout is generally frowned-upon by advanced web gurus. but, until mathml is more widely supported, i think using tables is a good short-term solution.

square roots with <table>

square roots can also be done with the html <table> command. it's very similar to the fractions-with-tables described above. here's an example:
r =  x2 + y2
depending on your browser and default fonts, the radical and the horizontal line may not join up exactly. on my pc this looks best with internet explorer, fair with opera, and poor with firefox. (firefox seems to be doing more kerning of the characters which causes some overlap problems).

to see where the table cells are, here is the same equation with borders turned on:
r =  x2 + y2
the html to produce this is:
<table style="margin-top:0.5em; margin-bottom:0.5em; text-align: center;"
    align="center" cellpadding="0" cellspacing="0" >
    <tr>
        <td nowrap="nowrap" align="right">
            <i>r</i> = <span style="font-size: 150%;">&radic;</span>
        </td><td style="border-top:solid 1px black;" nowrap="nowrap">
            &nbsp;<i>x</i><sup>2</sup> + <i>y</i><sup>2</sup>
        </td>
    </tr>
</table>
things to notice: for consistency and flexibility you may want to use css style classes instead of the style directive inside the <table> commands (this is shown in the fraction example above).

larger radicals can be had by increasing the font size (though each browser interprets this a bit differently); here is an example that is combined with a fraction:
r =  x2 + y2
5
a different approach is to use a bitmap image for the radical symbol. with css absolute positioning you can make a transparent png or gif bitmap and position it over the text. but the drawbacks to this approach are: if the user chooses a different font size, then the radical symbol won't scale correctly along with the text under the radical; and print quality is not as good when mixing bitmaps and text.

numbering equations

i use <table> for numbering equations. here's an example:
a2 + b2 = c2 (1)
here is the table with borders to show where the cells are:
a2 + b2 = c2 (1)
the html to produce this is
<style>
    table.num_eqn { width:99%;  text-align: center; vertical-align: middle;
        margin-top:0.5em; margin-bottom:0.5em; line-height: 2em; }
    td.eqn_number { text-align:right; width:2em; }
</style>

<table class="num_eqn" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            <i>a</i><sup>2</sup> + <i>b</i><sup>2</sup> = <i>c</i><sup>2</sup>
        </td>
        <td class="eqn_number">
            (1)
        </td>
    </tr>
</table>
note that the <style> tag goes in the <head> section of your html document. also i use a macro to generate most of the html code for the numbered equation table, so it's a lot easier to type.

long equations and line-breaking

with long equations, it is annoying when the browser puts a line-break in the middle of a math expression. try resizing your browser window and you'll see this here: br θ' cos2 θm x'' cos θm r θ'' cos2θ + m r θ' 2sin θ cos θm g sin θbr θ' sin2 θ = m r θ'' sin2θ + m r θ' 2sin θ cos θ the solution is to surround the sub-expressions with the tags
     <span style="white-space:nowrap;"> ... </span>
here is the same equation with the nowrap styling: br θ' cos2 θm x'' cos θm r θ'' cos2θ + m r θ' 2sin θ cos θm g sin θbr θ' sin2 θ = m r θ'' sin2θ + m r θ' 2sin θ cos θ

gladtex for windows

gladtex is a perl script that converts latex expressions to bitmap images. (note that i have created an enhanced version of gladtex for windows, more on this below). the latex is embedded in your html document within <eq> tags. when the document is processed by gladtex, bitmap images are produced and <img> tags are inserted into your html document. for example,
     <eq>\sum_{k=1}^\infty \frac{1}{k^2} = \frac{\pi^2}{6}</eq>
is replaced by
     <img src="eqns/web_math000.png" width="88" height="48"
     alt="\sum_{k=1}^\infty \frac{1}{k^2} = \frac{\pi^2}{6}">

which displays this equation image \sum_{k=1}^\infty \frac{1}{k^2} = \frac{\pi^2}{6} (actually, the source document should have a .htex extension, and gladtex then produces a document with the .html extension.)

here is an example of an inline equation: \int x \; dx = x^2 /2 which is specified with the text
     <eq inline=1>\int x \; dx = x^2 /2</eq>
note the inline=1 property. gladtex takes special measures to get the equation to line up with the other text on the line.

i've enhanced gladtex in several ways: to use gladtex, you need to install several other tools: perl, latex, ghostscript, and grep. see the page describing my enhanced version of gladtex for more information.

reviews of math display packages

html math as described above is not a package, but for comparison here is a review:

authoring: more complicated than other packages that use latex input. helpful to use a macro package to reduce the amount of html you have to write.

advantages: fast display of lots of math. no special installation required for users. free and standards based. adjusts to user font size preferences.

disadvantages: authoring can be more tedious. only simple math can be represented, though you can combine it with gladtex as needed for more complex equations.

compatibility: very good. uses built-in html and css standards. but be sure to test under various browsers and operating systems.


mathml is the new standard for displaying math on the web. in the long term this will probably be the best solution.

authoring: mathml is too verbose to write directly, so you need to find an authoring package that will produce it. several packages are available, however you then need to integrate the resulting mathml into your html file.

advantages: it is the standard. can represent any imaginable equation. display and print quality should be very good. it downloads quickly because it arrives as text within the web page, instead of separate image files.

disadvantages: not yet widely supported by browsers. requires a player to be installed by the user.

compatibility: not yet widely supported. there is a free player available for ie under windows. directly supported by amaya and mozilla/netscape browsers.


gladtex is a perl script that converts latex expressions to bitmap images. the latex is embedded in your html document within <eq> tags. when the document is processed by gladtex, bitmap images are produced and <img> tags are inserted into your html document. note that i have created a version that works on windows and that has some additional useful features.

advantages: easy authoring with latex embedded directly in your html document.

disadvantages: with many equations on a page, image files can make the page slow to display. image files require some additional file and directory organization. print quality is not the best. images do not scale if the reader changes font size.

compatibility: 100% compatible with all browsers, all systems.

license: open source.


jsmath produces some of the best looking math i've seen, and is very easy to use. you put a tex equation inside an html <span> or <div> tag, and then some javascript will turn it into html. the javascript is sent along with the page to the user's machine where it runs when they look at the page. the javascript is based on an actual tex engine, so the result is quite beautiful.

advantages: easy authoring using standard tex inside html tags. free, open source.

disadvantages: users should install tex fonts for best results. jsmath works without the tex fonts, but the result doesn't look nearly as good and the speed of display is slow. even with the tex fonts it can take a while to render lots of math, so best for pages without much math.

compatibility: seems good, but relies on complex javascript code and specific fonts.


tth is a tex to html translator. it is a highly developed package and seems to be well regarded.

authoring: you create a latex document which is then converted to html by tth (a c program).

advantages: easy authoring in latex.

disadvantages: the resulting math is sometimes a bit odd looking. the html is not really under author control.

compatibility: reliance on the symbol font means this does not work on macintosh systems.

license: free only for non-commercial uses. not open source.


pdf is a great format for display-only on the web. you can easily make a pdf version of a latex paper using pdflatex.

advantages: looks great. prints great.

disadvantages: not html, therefore not well integrated into a web site. appears in a special player window. user must have installed a pdf viewer.

compatibility: pdf viewer is widely available.

other math display packages

textogif is a latex-to-image convertor similar to gladtex, but requires you to write each equation as a separate .tex file.

htex is a latex-to-image convertor similar to gladtex, but seems to not be actively supported currently.

hyperlatex "is a way to create documents that will look good both as printed text and on the web. it is not a general-purpose converter to take latex into html. rather it is a set of macro definitions that allow a user to write one document for two media and to have the output look good in both. hyperlatex is a set of emacs macros, which can be used from within emacs, or compiled and used independently."
if you are not scared off by emacs, hyperlatex may be a good solution.

latex2html is a well-known converter from latex to html. i would rather have more control over the html, so i did not evaluate this seriously.

tex4ht is a general latex-to-html converter similar to tth or latex2html.

mathematica is a commercial math package. it provides some solutions for exporting to html or mathml.

other links

here are some useful links about displaying math on the web:
michael kors outletjordan 11 legend bluecoach outlet onlinejordan 11 legend bluemichael kors outletlouis vuitton outletjordan 11 legend bluejordan 6 black infraredmichael kors outletlouis vuitton outletmichael kors outletmichael kors outletlegend blue 11sbeats by dre cheapcoach outlet onlinelegend blue 11slouis vuitton outletjordan retro 11michael kors ukjordan 6 black infrared