Math in HTML and CSS
Math in HTML and CSS
1 of 23
http://www.cs.tut.fi/~jkorpela/math/
Content:
What? No math?
Alternative formats
What about simple linearized notation?
Formulas as images
Images in running text
Using normal characters
Using special characters
Line breaks as problems
Grouping digits and using thinner spaces
Using fonts
Parentheses
Fractions
Underlines, overlines
Arrays and tabulations
Subscripts, superscripts, and exponents
Equation numbers
Towards two-dimensionality
What? No math?
You may have wondered why web sites, such as Wikipedia,
use images for presenting mathematical formulas, even in
simple cases like = 0. Partly the reason is simplicity
and uniformity: since some formulas need to be presented
as images, it may sound simpler to use images for all imageseven in cases where simple HTML markup would
do ine:
= <i></i>
or
∇² = <i>φ</i>
Images provide a methodologically uniform approach, but the result is not typographically uniform at all: characters in an image are typically rather different from the same characters in text.
Although HTML markup exists in the sample case, mathematical
formulas are usually much more complicated. HTML lacks
markup for mathematical expressions as structures, and there is
no simple way to produce anything essentially two-dimensional
beyond superscripting or subscripting.
Given the fact that HTML was originally developed at CERN, the European Laboratory for Particle Physics, it may
sound odd that HTML has rather little markup for mathematical expressions and other special notations used in
science. Its not a surprise that you cant do math in HTML; after all, its a markup language, not a programming
language. But how are we expected to write physics reports or even modern biology papers without mathematical notations?
There was once an HTML 3.0 draft, with a section titled HTML Math, suggesting relatively simple markup for some basic mathematics. But its all history; the draft expired in 1995. (There was also an earlier idea about HTML+, which would have had a different,
more natural-looking math syntax.)
The modern trend is to regard things like mathematical markup as special applications, for which specialized
languages are used; see W3Cs Math Home Page for information about such trends, including the MathML language. If you ask me, the language hopelessly mixes structure and appearance. In any case, support to MathML in
web browsers still isnt wide enough to justify its use on web pages in general.
However, this does not mean that it would be impossible to write mathematical documents in HTML. There are
dif iculties and dif icult decisions to be made. For relatively simple mathematical notations, HTML can be used
rather well, especially if you use handle the toughest parts using images with adequate alt texts. See, for example,
22/12/2015 13:05
2 of 23
http://www.cs.tut.fi/~jkorpela/math/
Stan Browns articles on math. Note that theres the possibility of making an article available both in HTML format and in some other format, and this might be relatively easy if suitable tools can be found.
The approach is simple, from an authors point of view: you just insert a ixed script element in your document,
and you put an inline formula between \( and \), a display formula between $$ pairs. In the formula, you use for
example TeX (speci ically, AMSTeX) notations, which is rather simple to learn for basic mathematical constructs.
Such an approach makes the document accessible to virtually all who have the necessary mathematical prerequisites, if you limit the character repertoire to ISO Latin 1. But naturally its a rather simplistic method. You might
still consider using it as an alternative format, made available along with some more advanced but less accessible
format(s), such as an image.
Formulas as images
Genera ng images from formulas
You can use some software, e.g. something TeX based (see also AMS TeX resources) or a mathematics program
22/12/2015 13:05
3 of 23
http://www.cs.tut.fi/~jkorpela/math/
with graphic output format option, e.g. Mathematica, to produce visual representations of formulas as images.
Often such software gives the result in PostScript format, but for Web use a GIF format is usually better, since it is
much more widely supported in browsers by default. Various tools exist for converting from PostScript to GIF
format. You could also consider using the Latex2HTML converter which can, among many other things, generate
images in GIF format. (Theres another converter, TtH, which uses all kinds of tricks to convert mathematical expressions to HTML without using images. It applies very questionable methods like the Symbol font kludge. You
might still get some useful ideas from its output and apply them more reasonably.)
22/12/2015 13:05
4 of 23
http://www.cs.tut.fi/~jkorpela/math/
The purpose of the alt attribute is make the text comprehensible in a browsing situation where the image is not
displayed. For some notes on the importance and practical use of alt texts, see my Guidelines on alt texts in img elements. In the simplest case, you would use a simple linearized notation as alt text.
A particular problem arises when such alt texts contain notational features that are not self-evident and are not otherwise used in
the document. For example, suppose that expressions contain exponentiation, which is indicated using the usual two-dimensional
formatting (an exponent appears raised, perhaps in smaller font) in image presentations, but a circum lex character ^ is used in alt
texts. How can we inform people who need the alt texts, without disturbing people who see the images only? Theres a simple trick:
use a dummy image, such as single-pixel transparent GIF, and put the explanations into its alt attribute, e.g.
<img src="transp.gif" alt=
"In this document, the circum lex character ^ is used
to indicate exponentiation, i.e. raising to a power.">
And you would put this element somewhere near the start of the document, before the irst occurrence of an alt text that uses the
convention explained.
If the image has been generated using TeX, you might also provide a link to the TeX version, since some people
who cannot see the image for some reason might be able to understand TeX markup, or utilize it indirectly. Perhaps the image itself could be made into a link. In fact the MathWorld and Treasure Troves of Science pages often
include the TeX notation directly as the alt text; this is useful but not as useful as giving plain text presentations
there and providing links to TeX versions. In the document HTML Techniques for Web Content Accessibility Guidelines 1.0, section Markup and style sheets rather than images: The example of math recommends that if an HTML
document has been generated from a TeX document, the original TeX (or LaTeX) document too be made available
on the Web, since there are possibilities for auditory rendition of such versions.
That document also recommends that if a formula is constructed from several images, a single alt attribute
should specify a textual alternative to the entire formula. Apparently, you would put it into one of the images and
use alt="" for the other images.
es
Within the normal set of characters, namely the ISO Latin 1 character repertoire, which you can use pretty
safely in HTML authoring, some characters need special attention as regards to use in mathematical notations.
(As regards to typing them into your HTML documents, see section Typing characters in my character code tutorial.)
On Macintosh platforms, several browsers have had serious problems with some ISO Latin 1 characters, and
these characters include the superscripts 1, 2, and 3 and the vulgar fractions as well as the multiplication
sign . It seems that these problems have inally lost signi icance: any reasonably modern browser on Mac renders these characters correctly.
Among the basic arithmetic operators, the plus sign + poses no problems. As a minus sign, the hyphen-minus
character (-) is commonly used, but using the minus sign character would be more logical. It is usually visually
better (longer). Nowadays, it seldom causes character problems. It avoids some line breaking problems that
22/12/2015 13:05
5 of 23
http://www.cs.tut.fi/~jkorpela/math/
browsers have with hyphen-minus: we usually do not want e.g. a unary minus sign to have separated from its operand by a line break. (Browsers often break after hyphen-minus, but not minus sign.) For multiplication, there is
seldom any reason to use the asterisk *, e.g. a*b (its a programming language idiosyncracy), since you can use
the multiplication sign, e.g. a b or perhaps the middle dot, e.g. a b. When referring to vector operations, you
could use for a cross product and for a dot product. However, in principle, by the ISO 80000-2 standard, the
correct multiplication dot is not the middle dot () but the dot operator (, U+22C5). For fractions and division,
the solidus (slash) character / is commonly used, but you might also consider using the division sign in some
cases.
In addition to the above-mentioned use as a multiplication symbol, the asterisk * has several uses as an operator
symbol of some kind. Generally such uses are surrogate notations for various star-like symbols with more speci ic semantics. Since the asterisk is displayed in relatively small size and in a superscript position in most fonts,
it does not look good when it should be binary operator. So if you use it that way, in lack of better characters, you
might consider using markup that suggests that * be displayed in a monospace font; the simplest method is to
use tt markup, e.g. <tt>*</tt>. On the other hand, for use e.g. as a star denoting an adjoint matrix, superscript
style is better, and normally achieved (if achievable in a browsing situation) by not using any font markup for it.
When using special characters, there is, in principle, a rich repertoire of various asterisk- and star-like characters available in Unicode. There is a large number of characters with asterisk in their name, including asterisk
operator. Note that there are several Unicode characters with star in their name, including some that are classi ied as mathematical (general category: Sm, i.e. Symbol, Math), such as star operator.
Other operators include the less than and greater than signs (< and >). Since these characters are essential as
tag delimiters in HTML, they should be escaped using the notations < and > when they are to be included
into the documents textual content, as in a<b. Similarly the ampersand (&), which might be used e.g. in logic as
an and operator (connective), should be written as &.
There is nothing special about the equals sign (=); it can be typed as such. But the inequality symbol and the not
equal to, less than or equal to, and greater than or equal to symbols are problematic; they do not belong to
ISO Latin 1. If you have decided to use special characters (i.e. characters outside ISO Latin 1) despite their problems, then you would use the numeric references ≠ and ≤ and ≥ (appearance on your
browser: ). Otherwise you would have to use some surrogates like =/, <= and >=. Some authors use =< and
=> instead of <= and >=, but theres anyway the risk that one of the constructs be mistaken as an implication arrow or another arrow symbols. The not equals sign is the most dif icult, since it has no widely recognized surrogate. Note that /= is a divide and assign operator in some programming languages! In some contexts, you might
use the character pair != to denote inequality, especially if you expect it to be familiar to readers from their experience with programming languages like C, Perl, and JavaScript. For a general audience, however, the != symbol
could be a mystery; there is hardly anything natural in using the exclamation mark to denote negation!
When expressing physical quantities, note the following rules of the SI system of units (see Guide for the Use of
the International System of Units (SI) for more information):
Unit symbols like m or s are written in Roman (upright) style, not in italics. In HTML, this normally means
that you dont use any speci ic markup for them. But if the symbol appears inside text that is affected by
markup or CSS rules that make the browser use an italic font, such as inside an em element (which is typically
presented in italics), then you should use e.g. something like the following:
in HTML:
<em>The measured time was only 42 <span class="unit">s</span>.</em>
and in CSS:
.unit { font-style: normal; }
Units formed from other units by multiplication can be written using the dot operator or a space, e.g.
N m or N m; or without any symbol between the operands (Nm), if there is no ambiguity.
For multiplication of numbers and quantities, the multiplication sign is preferred when a decimal point is
used in numbers. When a comma is used as the decimal marker, then either the multiplication sign or the dot
operator can be used for multiplication. These rules are given in international standards, which give preference to the use of a comma as a decimal marker, but it is debatable whether this is wise e.g. in documents in
English; see the document ISO 31-0: a standard that may confuse by Stewart Clark. Examples:
4.2 × 10<sup>15</sup> and, in another usage, 4,2 c5; 10<sup>15</sup> (renderings: 4.2 1015
and 4.2 1015 but note the problems with superscripts).
Temperatures in Celsius degrees are written using the degree sign, with a space between it and preceding
22/12/2015 13:05
6 of 23
http://www.cs.tut.fi/~jkorpela/math/
The problems and solutions are discussed in Guide to using special characters in HTML. The document is
newer than the presentation in this document and covers some topics not discussed here.
Support to mathematical characters in fonts varies greatly. Some typical examples:
Unicode
U+00B1 plus-minus sign
U+2264 less-than or equal to
U+2230 volume integral
U+1D44E mathematical italic small a
Status in fonts
Practically universal support
Very good support
Limited, but works for most users
rather limited support; use <i>a</i> instead
In practice, all or most of the mathematical symbols you need are covered by the Lucida Sans Unicode font,
which is normally present in Windows systems. It is not of particularly good design, so you might suggest Arial
Unicode MS (shipped with Microsoft Of ice) as a preferred alternative. Moreover, you should include a list of
fonts that are commonly available on other platforms (Linux etc.).
For pages that make essential use of mathematical characters that are not covered by fonts normally in use on
22/12/2015 13:05
7 of 23
http://www.cs.tut.fi/~jkorpela/math/
Windows platforms, it might be a good idea to include a short note about fonts. Perhaps with a few selected characters and with an explanation of what they should look like and a remark: There are free fonts that contain
these characters, such as DejaVu Serif, Quivira, and Symbola.
Note that by HTML speci ications, you are not limited to using those numeric character references for which
there is a named entity in HTML 4.0. You can principle use any &#number; notation where number is the Unicode
code position to which a character has been assigned.
As a practical point, note that Microsoft has de ined WGL4 (or Pan-European) list of characters, containing
some mathematical symbols too. If you use them only, you have relatively good odds of inding a large number of
users equipped with fonts that can display the characters. See Using Special Characters from Windows Glyph List 4
(WGL4) in HTML by Alan Wood.
See also the document How to ind an &#number; notation for a character, and note that you will probably be interested especially on the following Unicode blocks:
Mathematical Operators, e.g.
Letterlike Symbols, e.g.
Arrows, e.g.
If you only need Greek letters in addition to Ascii characters, then there is the possibility of using an encoding which lets you enter
Greek letters directly. You could use ISO 8859-7, which is relatively widely supported. See section Simpler ways for simpler needs: simple 8-bit encodings in the above-mentioned document. But note this would give you just Greek letters, not e.g. not equals sign, nabla
operator, aleph, etc. ad . Moreover, you would risk losing most of the upper half of ISO Latin 1, due to browser bugs.
22/12/2015 13:05
8 of 23
http://www.cs.tut.fi/~jkorpela/math/
head) to a grotesque version where the arrowhead dominates (e.g., in Calibri). Moreover, some common fonts
like Verdana lack it, so in text with Verdana as the primary font, the arrows will appear in whatever font the
browser chooses to use as a replacement font. Therefore, it may be a good idea to suggest a speci ic, widely available font where the appearance is acceptable. You can write e.g.
<span class="arrow">→</span>
in HTML and
.arrow { font-family: "Times New Roman"; }
in CSS. In Times New Roman, the arrow () corresponds to the arrow symbol traditionally used in mathematics,
and it can be used in conjunction with many different fonts.
If the vertical position of the arrow is unsatisfactory, as it may be when used between capital letters of the Arial
font (A B), you can tune the rendering (A B), by adding e.g. the CSS rule
.arrow { position: relative; bottom: 0.07em; }.
The division slash character (U+2215), being de ined as a mathematical operator, is in principle more adequate
than the common slash character. It belongs to rather many fonts. But the problem is that the appearance in generally unsuitable. Its glyph usually touches the adjacent characters or even strikes through them. Here is an expression using irst the common slash, then the division slash:
a/b
ab
22/12/2015 13:05
9 of 23
http://www.cs.tut.fi/~jkorpela/math/
22/12/2015 13:05
10 of 23
http://www.cs.tut.fi/~jkorpela/math/
It is safest to use spaces, since the other alternatives could be misinterpreted. For example, in English 1,005
would mean one thousand and ive and 1.005 would mean one and ive thousandths; in French, and in several
other languages, its just the reverse! We need to make some decision concerning the decimal separator, but for
integers we can avoid the problem by using spaces: 1 005 is unambiguous. It is true that e.g. in English texts it
does not conform to normal English practice, but here we are discussing mathematical texts, where the practice
is recommended by standards,
This raises two problems: First, the line breaking problem that we just discussed; but we saw that there are reasonable solutions to it. Second, you might regard spacing between digit groups as typographically excessive, if
normal spacing (as between words) is used.
Spacing in expressions
The excessive spacing problem also arises in other contexts in mathematical expressions. It is often regarded as
good style to use some spacing e.g. around mathematical operators, but not as big spacing as we get in typical
browsing situations if we just use normal spaces.
In high-quality typesetting, e.g. when using TEX, spacing is controlled carefully, using advanced tools and techniques. We cannot expect to achieve the same using HTML and CSS, but we can aim at reasonable quality.
An expression like a + b is usually best written in HTML so that there are spaces around the operator +. This
gives more lexibility, since we can then use word-spacing in a simple way. If the spaces were omitted, letterspacing (which actually affects the spacing between all characters, not just letters) could be used in simple cases
like this, but things would get much more complicated when variables consist of several characters (e.g., contain
subscripts).
The following example shows the rendering of an expression with several operators, under different style sheets:
unstyled with spaces
a + (b c)
22/12/2015 13:05
11 of 23
http://www.cs.tut.fi/~jkorpela/math/
word-spacing:-0.07em a + (b c)
word-spacing:-0.2em
a + (b c)
unstyled without spaces a+(bc)
Thus, a word-spacing value like -0.07em creates an appearance that more or less resembles typical typesetting of
mathematical expressions. A value of -0.2em tends to reduce spacing so that it almost corresponds to the rendering we would get if no space characters were used. The effects naturally depend on the font in use, but these observations apply to typical fonts like Times New Roman and Arial.
Using fonts
Say No to font kludges
In the old days, one of the most common ways of trying to include Greek letters and mathematical symbols into
Web pages was to use font face="Symbol", such as writing <font face="Symbol">c</font> to get the Greek letter
gamma (). You may still ind web pages that propagate such usage as if were clever and useful. I will not explain
here why that approach is fundamentally wrong; I refer to the excellent presentations Using FONT FACE to extend
repertoire? by Alan J. Flavell and <FONT FACE> considered harmful at the Alis Babel site. Brie ly, the trick appears
to work in many situations, but thats because of browser bugs. The invariable meaning of <font face="Symbol">c</font> is the letter c, so any correctly behaving browser will display it using some physical presentation
(glyph) for that character.
22/12/2015 13:05
12 of 23
http://www.cs.tut.fi/~jkorpela/math/
not to the margin.) If you need the spacing just for an individual expression, you could use markup like <i style=
"padding-right: 0.2em">f</i>(0) for an expression like f (0). Or you might even write a general style sheet rule
that sets a right margin for all inline elements that are commonly rendered in italics. You might explicitly set
right margin to zero, since it is imaginable that some browsers deal with the problem by using some default right
margin, and you don't want a cumulated effect. Example:
i, em, cite, dfn, var { padding-right: 0.15em; margin-right: 0; }
Font demo
serif sans-serif
x x
U U
aa
a a
o o o o
This usually rules out Arial, the most commonly used font on web pages. In most browsers, the default font is
Times New Roman, which is a good serif font for printed matter but problematic on screen due to the much
smaller resolution. Suitable serif fonts that work both on screen and on paper include Cambria, Georgia, Palatino
Linotype, and Bookman Old Style. Just remember to list down a few of them in your font-family, in order of your
preference, since none of them them is universally installed on computers.
Hint: after writing a CSS rule like
body { font-family: Cambria, Georgia, Palatino Linotype, Bookman Old Style, serif; }
use Firefox with Web Developer Extension to view your page, then select Edit CSS in its CSS menu. Modify the
style sheet by removing the irst font in the list, look at the page, remove the next font, etc. This way you can
quickly test the page on all the fonts you suggest, without editing the page itself.
Changing fonts
In good typography, we avoid mixing fonts in text. We can use normal, italic, and bold versions of a font, but not
fonts of different design in the same paragraph or other block of text. However, in mathematical texts, it is often
more or less necessary to mix fonts, taking letters and other common characters from one font and mathematical
symbols from another.
The main reason is that most fonts have a limited character repertoire, as described in section Using special characters. When you need to pick up special characters, you cannot be too picky. In particular, a large number of
mathematical symbols can be found in commonly available sans-serif fonts like Lucida Sans Unicode and Arial
Unicode MS but not in common serif fonts.
For example, the nabla operator (U+2207) is present in several fonts, but not in any serif font commonly available on Windows systems. Thus, to write the expression f you should use markup like
<font class=nabla></font><i>f</i>
together with a CSS rule like the following:
.nabla { font-family: Arial Unicode MS, DejaVu Serif, Linux Libertine, Lucida Sans Unicode; }
Parentheses
While it has been conventional to some extent to use different characters for nested parentheses, using ( ), [ ], { },
and then angle brackets, such practice is not endorsed by standards. Quoting ISO 80000-2:
It is recommended to use only parentheses for grouping, since brackets and braces often have a speci ic
meaning in particular ields. Parentheses can be nested without ambiguity.
Thus, angle brackets should only be used in specialized meanings, such as L inner product of functions, or
maybe for an arithmetic mean if the primary notation (line over) is not applicable. Instead of usage like [(a +
b)/c], normal parentheses should be used: ((a + b)/c).
If angle brackets are used in math, then they should, according to the standard, be MATHEMATICAL LEFT ANGLE
BRACKET U+27E8 and MATHEMATICAL RIGHT ANGLE BRACKET U+27E9. The HTML entities ⟨ and ⟩
22/12/2015 13:05
13 of 23
http://www.cs.tut.fi/~jkorpela/math/
denote other characters, U+2329 and U+232A. While they work more often than the correct characters, working here means just getting displayed in some odd font.
It is not appropriate to use the less than sign (<) and the greater than sign (>) as angle brackets. In mathematical
texts, their usage should be limited to the relational meanings (though the relation could of course be other than
the common ordering relation). It should never be a matter of glyph preference which character you use, though
in this imperfect world, violations of this principle are sometimes understandable and foregiveable.
The Unicode standard says that the use of U+2329 and U+0232A as mathematical brackets is strongly discouraged, because of their canonical equivalence to CJK angle brackets. This canonical equivalence is likely to result
in unintended spacing problems if these characters are used in mathematical formulae. In practice, when you
use these characters, they will most probably be picked up from a font designed for Chinese-Japanese-Korean
(CJK) ideographs, therefore designed to it into a largish square, typically causing typographic mismatch. On the
other hand, font support to the correct mathematical angle brackets is still rather limited, so avoid them unless
the contents absolutely needs them.
Theres a special of oddity with the entity references for angle brackets. Even though HTML speci ications clearly
de ine ⟨ and ⟩ as 〈 and 〉, i.e. as denoting U+2329 and U+232A, most browsers treat
them as denoting U+27E8 and U+27E9. Strangely enough, in this issue, IE seems to be the only browser that
works by the speci ications. HTML5 drafts have silently changed the meanings of ⟨ and ⟩ to correspond to the behavior of most browsers. Conclusion: Avoid entity references. By using character references, you
will at least know which character will be used, even though you still have all the font problems.
Frac ons
Simple linearized nota on
For fractions like 6/7, the common linearized notation is usually the best, especially within running text. It is not
typographically good, but it is robust, and people are accustomed to such simple presentations of fractions on
web pages.
Two-dimensional style
In two-dimensional display formulas, even fractions can be shown using a horizontal line, with a number above
and below it, but inside text, its hardly feasiblethe numbers would need to be such a small size that legibility
would be poor. The following line illustrates an attempt at such presentation, with various compromises and relatively complicated HTML and CSS, and therefore with some fragility:
Is 13 or better?
Frac on characters
If the only fractions in your document are vulgar fractions , you might consider using the ISO Latin 1 characters for them, e.g. as entity references ½, ¼, ¾. But if you need other fractions too, this is
not a good idea, since it would be odd if different fractions had essentially different appearances.
In Unicode there are a few more fraction characters (namely for 1/3, 2/3, 1/5, 2/5, 3/5, 4/5, 1/6, 5/6, 1/8, 3/8,
5/8, 7/8), in the Number Forms block, but it would still be a limited repertoire. Moreover, although they are covered by many fonts, the font support is far from universal. The newest fraction characters, namely those for 1/7,
1/9, 1/10, and 0/3, have very limited font support (only a few fonts, none of which is shipped with any operating
system or popular software).
Construc ng a frac on
You might use a linear notation with sup markup for the numerator and the sub markup for the denominator.
The main problem is then that an expression like 5/8 tends to cause uneven line spacing, due to the poor quality
of implementation of superscript and subscript style in most browsers. It is therefore better to use CSS to reduce
font size and change vertical position.
You might also consider using the fraction slash (U+2044) character which should, according to the Unicode
standard, solve the problem for numeric fractions in an elegant way. That would mean something like
5&x#2044;8 in HTML. The fraction slash character is often more slanted than the normal slash (solidus) charac-
22/12/2015 13:05
14 of 23
http://www.cs.tut.fi/~jkorpela/math/
ter. This is intended to correspond to special rendering where the numbers around it are in reduced size and vertically positioned in a manner that re lects a traditional way of writing fractions. But browsers do not currently
do such things, and this may result in unsatisfactory rendering: the fraction slash appears between normally
styled numbers ( ), possibly touching them, depending on font (e.g., in Arial, looks bad. Although this could
be alleviated by setting letter-spacing, its more natural to try to imitate the traditional fraction appearance, using CSS.
Using MathJax
It is easy to create fractions using MathJax, with the \frac command. However, it creates a fraction with numerator and denominator stacked, with horizontal line between them. Such a presentation is usually OK in display
formulas, but less so in text. About tuning, see the Q/A pages LaTeX force slash fraction notation and How do I
typeset arbitrary fractions like the standard symbol for .5 = ?
MathML
In MathML, a fraction can be described as a special case of a fractional expression, using the mfrac element. The
code is verbose, but a more serious problem is that not all browsers support MathML, especially when embedded in an HTML document. On non-supporting browsers, the code degrades to a rendering like 5 8 instead of
5/8.
Summary
To demonstrate what the different approaches yield on your current browser with its current settings, here is a
table of different presentations for 5/8:
A fraction presented using different techniques
Approach
Appearance
Linear
notation
5/8
5/8 text
Special
character
⅝
text
<sup>5</sup>/<sub>8</sub>
5/ text
8
<span class=frac>5/8</span>
MathJax
\(\frac{5}{8}\)
MathML
text
58 text
text
text
text
22/12/2015 13:05
15 of 23
http://www.cs.tut.fi/~jkorpela/math/
Underlines, overlines
Underlining
To underline something, you could use the u element in HTML. However, underlining is widely taken as indicating a link on the Web. Links want to be links, and you should avoid doing anything that might make something
look like a link if it isnt. But if underlining a symbol is part of an established tradition in some ield, go ahead and
use the u element. It would be less logical to use the CSS declaration text-decoration: underline, since here underlining is not just a suggestion on rendering style but an essential feature of content.
As an alternative, you could use the combining low line character (U+0332) after the symbol to be underlined.
However, this character appears in a few fonts only and does not necessarily produce any better rendering. A
simple test (with an underlined character and a symbol with combining low line): x, x .
Overlining
It is common to use overlining in mathematics, e.g. to indicate an average. Somewhat illogically, HTML has no
markup for overline. In a style sheet you could suggest overlining, using the declaration text-decoration: overline.
However, as the property name says, its assumed to be decoration, not part of the content proper, and in any
case style sheets are for suggestions only; you should expect style sheets to get ignored fairly often. Presentationally, note that the overline appears rather high above the symbol.
Overlining something like x might be adequate if the context or explicit explanations make it clear what is meant
even if the overline does not appear. For casual overlining a single symbol, you could use an embedded style
sheet as follows:
<b style="text-decoration:overline"><i>x</i></b>.
If overlining is essential, consider using the combining overline character (U+0305) after the symbol to be overlined. There are risks with fonts, but in most browsing situations, this method works. The rendering varies but is
generally much better than in the CSS methods, as the overline is close to the letter. A simple test, irst with a
CSS-overlined letter, then a letter with combining overline: x, x .
Radicals
For radicals (expressions of roots), it is customary in typeset mathematics to use a vinculum it more evident
what belongs under the root. The vinculum is a horizontal line that joins with the radical sign, and the joining is
dif icult to arrange without using specialized software that draws math expressions.
You might consider the following options:
Use fractonal powers instead, e.g. (a + b)1/2. This is even the recommended way according to the ISO
80000-2 standard.
Use just the radical sign with no vinculum, using parentheses if needed, e.g. (a + b). This is recommended
in The Oxford Style Manual (clause 12.6.2).
You might suggest overlining to make to produce a sort of vinculum:
22/12/2015 13:05
16 of 23
http://www.cs.tut.fi/~jkorpela/math/
(a + b)
This uses simple markup where the expression in parentheses is enclosed between <span
style="text-decoration:overline"> and </span>.
In this context, the relatively high vertical placement of the overline does not disturb. It is even desirable, and
perhaps an even higher placement would be desirable. Things may get distorted on many browsers if there are
exponents written using sup under the root.
In our example, the parentheses are redundant when overlining is applied. I have experimented with tricks which would put the
parentheses inside span elements with style sheets suggesting the suppression (display:none) of them in presentation.
Instead of text-decoration: overline, you might set a top border for the radicand, e.g. .radic { border-top: solid
1px }. This seems to produce reasonable presentation even when there are exponents and subscripts (sup and
sub elements) in the radicand, as the following example illustrates:
d = (x2 x1) + (y2 y1)
What about roots other than the square root? There are Unicode characters for the cubic root and fourth root
symbol, though they are less widely supported than the square root symbol. For a general root, you might put the
radical index right before the radical symbol, in superscript style. Besides, you could use CSS to suggest reduced
spacing between those characters. This would mean HTML markup like the following:
<span class="radic"><sup><var>n</var></sup>√</span><span class="radicand"><var>x</var>
</span>
with CSS like the following:
.radic {letter-spacing:-0.15em; }
.radicand {text-decoration:overline; }
This looks like the following in your current browsing situation: nx.
A =
x-y -a 42
c a*b c
In the example above, the table representing a matrix has been embedded into an outer table so that we have
been able to associate a symbol with the table. Similar techniques can be used e.g. when you wish to present the
sum of tables; you would write an outer one-row table which contains the matrices in its cells and a plus sign in a
cell of its own between them. The following example illustrates this:
1 5
3 7
2 2
2 2
3 7
5 9
22/12/2015 13:05
17 of 23
http://www.cs.tut.fi/~jkorpela/math/
a2, ex
a2, ex
a
sup markup
a, e
For subscripts, the situation is rather similar. However, there is a more limited set of subscript characters than
superscript characters.
22/12/2015 13:05
18 of 23
http://www.cs.tut.fi/~jkorpela/math/
However, especially when superscripting is used to express exponentiation, superscripting is essential, and there
need not be any contextual hints. It really makes a difference if 109, which is intended to mean 10 to the power 9,
actually gets displayed as 109. The same applies to using superscripts e.g. in denoting the transpose of A by AT
(i.e., A immediately followed by T in superscript style).
Superscripts are often used for footnote references in print media. But in mathematical texts, such practice is
best avoided in order to remove any risk of confusing such usage with exponentiation or other mathematical superscripting. Besides, such footnote references dont work well on Web pages in general, as explained in the document Footnotes (or endnotes) on Web pages.
i=0 xi
That means summation of xi (with i as subscript) from i=0 to in inity. In browsing situations where the in inity
symbol is correctly displayed, the main problem on most browsers is that the in inity symbol does not appear
above i=0 but to the right of it (in superscript style though). In the worst case, the reader might misunderstand
the upper limit as an exponent of the lower limit! So it is perhaps better not to use a superscript at all but put the
limits into a subscript, e.g. as i=0,, (which makes use of the horizontal ellipsis character; midline horizontal
ellipsis would be better, but its less widely supported) giving the following appearance: i=0,, xi.
The in inity symbol might appear in fairly small size. In general, special symbols easily become unreadable when font size is reduced, so you might consider setting the font size larger than normal. In the above example with HTML markup for a formula and the
formula itself side by side, the font size for the formula has been set to 125%.
The presentation of a summation expression could be tuned in different ways, some of which will be discussed in
the sequel. But generally they lead to rather complicated constructs, and the complexity may cause problems on
different browsers, current and future. However, some simple superscript positioning problems can be addressed
relatively easily. Let us take the example of just positioning a simple one-character superscript above a one-character subscript.
In chemistry, sometimes both subscripts and superscripts are used, for example in formulas for ions. Consider
the formula
NO<sub>3</sub><sup></sup>
where letter O has both subscript 3 and superscript . The latter is the minus sign, and the Ascii hyphen is a
particularly poor surrogate here due to its shortness; such problems were discussed in the section on special
characters. It seems that the stylistically preferred notation for ions has the superscript in the same horizontal
position as the subscript. See, for example, the Ions page in Eric Weissteins World of Chemistry, which uses images to create such appearance.
The markup mentioned above by default creates an appearance where the superscript is on the right of the subscript (O3 ). Changing the order of the superscript and subscript would not help much. But we can try to affect
the horizontal placement by using a negative margin. Since, in general, the notation for an ion always has a superscript and may or may not have a subscript, it seems practical to put the sup element irst and move the subscript to the left. This would mean markup like
<span class="ions">NO<sup>−</sup><sub>3</sub></span>
(or maybe with div instead of span) and a style sheet like the following:
.ions { line-height: 1.8; }
.ions sub { margin-left: -1ex; }
and it would result in the following on your browser: O3.
Creating good appearance for variables with both a subscript and a superscript is rather challenging: ine-tuning
is needed, and the rendering will still greatly depend on the font used. Beware that widths of characters vary by
font, so a horizontal shift created by margin-left or some other method might be adequate for some fonts and
22/12/2015 13:05
19 of 23
http://www.cs.tut.fi/~jkorpela/math/
poor for others. Moreover, it cannot as such be used in conjunction with the method of making line spacing even
that will be described later in this document.
The margin-left property effectively shifts the subscript to the left. The line-height property is useful for defeating some IE bugs and fairly natural, since you are using quite some height here, more than we can expect to be
available by default. Note that if an element in class ions contains a lone superscript, you would need to take extra measures, since the CSS code above postulates that a sup element appears immediately after a sub element.
From the HTML perspective, the basic problem in situations like this in that the sub and sup elements have been
de ined in a rather presentation-oriented manner rather than structurally. When you write <sub>i</sub>, you're
saying that i is a subscript but not what it is associated with. In a case like ai2, i is a subscript for a whereas
2 is a superscript (exponent) for the expression consisting of a with subscript i. There is no way to express
this structural relationship in HTML. Using extra parentheses, like (ai)2, deviates from common mathematical
notations and looks somewhat clumsy, but it makes the meaning unambiguous and clear.
Even though it might seem suitable to set the font size to achiever similar sizing across browsers, it has just the
opposite effect. If you dont set it, browsers generally apply a size reduction (by about 80%) fairly consistently.
But if you set font-size on sub or sup, IE will interpret it differently from other browsers.
If you really need to set the font size of subscripts or superscripts, you have a few options, like the following:
Use span markup (with class attributes) instead of sub and sup, and set vertical-align and font-size in CSS.
Use sub and sup but use JavaScript to convert them to span elements. Demo page: sup size ix.
Nested superscripts
Most browsers render superscripts properly, or at least tolerably well. But they often fail to handle nested superscripts (or subscripts) well. And that means that exponentiation in an exponentiation may get lost in graphic presentation, too. For example, some old versions of Internet Explorer render a<sup>b<sup>c</sup></sup> the
same way as a<sup>bc</sup>. However, modern browsers, including reasonably new versions of IE, honor
c
nested superscripts in rendering, Here is test for your browser: ab (the letter c should be a superscript of b).
The preceding paragraph may illustrate the problem that nested superscripts easily cause problems by (almost)
hitting the preceding line. small font. One approach (perhaps observable in this paragraph) is to set the
line-height property (in CSS) to a relatively high value like 1.6. Moreover, it might be a good idea to set
line-height globally (for the body element) to a value like 1.3, since this helps with some of the smaller problems
of uneven line spacing and may improve readability in general.
22/12/2015 13:05
20 of 23
http://www.cs.tut.fi/~jkorpela/math/
Equa on numbers
In mathematics, it is common to number equations and put the number on the right of an equation, in parentheses or brackets, as follows:
(a + b)2 = a2 + 2ab + b2
(42)
(43)
Several approaches have been proposed to achieve such layout using just CSS and no presentational markup.
However, the CSS methods (whether based on loating or on positioning) seem to suffer from various problems
on current browsers. Some methods work well if the equation its on one line but lead to confusion when it is divided over two or more lines. Since we wish to create pages that adapt to varying rendering widths ( luid design), a simple table is the practical solution:
<table class="eq" summary="Equation and its number." width="100%">
<tr>
<td>the equation</td>
<th align="right" valign="bottom">(number)</th>
</tr>
</table>
If you regard such a table as a deprecated layout table, consider its markup, speci ically the summary attribute
and the use of th (table header cell) element for the equation number, which logically acts as a header for the row
(the equation).
You can get rid of the presentational attributes width, align and valign by using corresponding CSS properties. If
you like prevent the equation number from appearing in bold, you should either replace (somewhat illogically)
the th element by a td element, or use the font-weight property in CSS. As a whole, this could mean the following
style sheet:
.eq { width: 100%; }
.eq th { text-align: right;
22/12/2015 13:05
21 of 23
http://www.cs.tut.fi/~jkorpela/math/
vertical-align: bottom;
font-weight: normal; }
The following equation has been formatted with such a style sheet (and its HTML markup is pure):
(p + q)(r + s) = (p + q)r + (p + q)s = pr + qr + ps + qs
(44)
Towards two-dimensionality
For simplicity, let us irst assume that we wish to present the expression x divided by a b in the conventional
two-dimensional format. In this trivial case, the linearization x/(a b) would do just ine, but in more complicated cases, two-dimensionality would greatly improve the clarity. Using an image is one possibility, and if the
linearized version isnt too complicated and you include it as the textual alternative, it might work ine. But lets
see some other possibilities.
f(x)
------- dx
1 + x
a
In any case, the visual quality of this method cannot be very impressive. Moreover, it creates accessibility problems, since its gibberish unless seen in the exact preformatted way.
22/12/2015 13:05
22 of 23
http://www.cs.tut.fi/~jkorpela/math/
x
a b
Reasonable appearance might be achieved that way, with perhaps tolerably graceful degradation in text-only media: a simple character cell browser like Lynx would basically display it as
x
divided by
a - b
which might be understandable. But designing and writing suitable table markup would be rather awkward for
nontrivial expressions.
It would be simpler and more natural to have a table with two rows only, using a bottom border for the irst cell.
The border would create a suitable horizontal line. However, in text-only presentation and in nonvisual presentation, the data would appear as
x
a - b
which can be dif icult to interpret. This could be partly addressed by using a summary attribute for the table, e.g.
<table summary="This is a fractional expression, with the numerator in the irst cell, the denominator in the second">.
Lets see what we could do with the integral above. Using a bit contrived table markup, we might get something
like the following:
b
f (x)
dx
1 + x
In any simple text presentation, it would look rather awful, though. You might consider providing a separate link
to an alternative presentation, for such reasons.
For a rather common case of a de inition that is most naturally presented in two lines, this approach might work
tolerably, for simple de initions:
ij =
1, if i = j
0, if i j
Returning to simple examples, let us consider how we might present (a b)/x two-dimensionally using style
sheets, speci ically the display property in CSS1 (cf. to ideas about removing redundant parentheses above). We
would start from the simple linear notation (a-b)/x. We would put the parentheses and the slash each inside a
span element containing that character only, and we would also use span for the numerator and denominator.
Then, using class names assigned to the span elements, we would suggest in CSS the suppression of the display
of those characters, presenting both the numerator and the denominator as a block, and underlining the numerator. This means markup like the following:
<span class="nom"><span class="lin">(</span><i>a</i> −
<i>b</i><span class="lin">)</span></span><span
class="lin">/</span>
<span class="den"><i>x</i></span>
with the following style sheet:
22/12/2015 13:05
23 of 23
http://www.cs.tut.fi/~jkorpela/math/
x
And it degrades to (a b)/x in non-CSS browsing situations.
We might still add
.den { line-height: 0.65; }
to reduce the spacing between the line and the denominator, so that the expression would look more natural.
The value 0.65 is a compromise. On many browsers, it doesnt improve things much, and a smaller value like 0.5
would be better, in a case where the denominator has just letters with no ascenders. But there is the risk that on
some browsers, a small value chops off the top of the text in the denominator. It gives the following appearance
on your browser:
a b
x
If the denominator is wider than the numerator, you would like to overline the denominator instead of underlining the numerator.
Conclusion
We could consider other approaches too, such as using positioning with style sheets instead of tables. But it
probably suf ices to conclude with the following note:
In HTML, a rich set of mathematical symbols and some other basic notations can be used, but currently
with accessibility problems that users need to solve. Two-dimensional presentation of expressions via
HTML markup is trickery and handcraft, now and in the foreseeable future. Thus, quite often it is better
to use JavaScript-based tools or images for such purposes.
Date of creation: 2000-03-08. Last update: 2013-02-03.
This page belongs to section Mathematics & physics and IT of the free information site IT and communication by Jukka Yucca
Korpela.
22/12/2015 13:05