Help:Displaying a formula

From CheLabWiki

Jump to: navigation, search


CheLabWiki uses a subset of TeX markup, including some extensions from LaTeX and AMSLaTeX, for mathematical formulae. It generates either PNG images or simple HTML markup, depending on user preferences and the complexity of the expression. In the future, as more browsers are smarter, it will be able to generate enhanced HTML or even MathML in many cases. (See blahtex for information about current work on adding MathML support.)

Contents


Syntax

Math markup goes inside <math> ... </math>. The toolbar has a button for inserting these. Similar to HTML, extra spaces and newlines in TeX are ignored. The TeX code has to be put literally: MediaWiki templates, predefined templates, and parameters cannot be used within math tags: pairs of double braces are ignored and "#" gives an error message. However, math tags work in the then and else part of #if, etc.

Rendering

The PNG images are black on white (not transparent). These colors, as well as font sizes and types, are independent of browser settings or CSS. Font sizes and types will often deviate from what HTML renders. Vertical alignment with the surrounding text can also be a problem. Solutions to most of these shortcomings have been proposed, but not yet implemented; however, on CheLabWiki, the sizing of displayed equations has been adjusted to be roughly consistent with the surrounding text. The css selector of the images is img.tex.

We emphasize that the appearance of equations on CheLabWiki will depend on your browser; the best rendering is provided by Firefox, which is available for both Windows and Macs and is a free download from Mozilla.

You have some control over whether your browser attempts to render equations in HTML, or as PNG images, or perhaps MathML. See the page under the "Math" tab on your User Preferences page.

The alt attribute of the PNG images (the text that is displayed if your browser can't display images; Internet Explorer shows it up in the hover box) is the wikitext that produced them, excluding the <math> and </math>.

Apart from function and operator names, as is customary in mathematics for variables, letters are in italics; digits are not. For other text, (like variable labels) to avoid being rendered in italics like variables, use \mbox or \mathrm. For example, <math>\mbox{abc}</math> gives abc.

TeX vs HTML

Before introducing TeX markup for producing special characters, it should be noted that, as this comparison table shows, sometimes similar results can be achieved in HTML (see HTML Special Characters).

TeX Syntax (forcing PNG) TeX Rendering HTML Syntax HTML Rendering
<math>\alpha\,</math> \alpha\, &alpha; α
<math>\sqrt{2}</math> \sqrt{2} &radic;2 √2
<math>\sqrt{1-e^2}</math> \sqrt{1-e^2} &radic;(1&minus;''e''&sup2;) √(1−e²)

The use of HTML instead of TeX is still under discussion. The arguments either way can be summarised as follows.

Pros of HTML

  1. In-line HTML formulae always align properly with the rest of the HTML text.
  2. The formula's background, font size and face match the rest of HTML contents and the appearance respects CSS and browser settings.
  3. Pages using HTML will load faster.

Pros of TeX

  1. TeX is semantically superior to HTML. In TeX, "<math>x</math>" means "mathematical variable x", whereas in HTML "x" could mean anything. Information has been irrevocably lost.
  2. TeX has been specifically designed for typesetting formulae, so input is easier and more natural, and output is more aesthetically pleasing.
  3. One consequence of point 1 is that TeX can be transformed into HTML, but not vice-versa. This means that on the server side we can always transform a formula, based on its complexity and location within the text, user preferences, type of browser, etc. Therefore, where possible, all the benefits of HTML can be retained, together with the benefits of TeX. It's true that the current situation is not ideal, but that's not a good reason to omit information.
  4. Another consequence of point 1 is that TeX can be converted to MathML for browsers which support it, thus keeping its semantics and allowing it to be renderred vectorally.
  5. When writing in TeX, editors need not worry about whether this or that version of this or that browser supports this or that HTML entity. The burden of these decisions is put on the server. This doesn't hold for HTML formulae, which can easily end up being rendered wrongly or differently from the editor's intentions on a different browser.
  6. TeX is the preferred text formatting language of most professional mathematicians, scientists, and engineers. It is easier to convince them to contribute if they can write in TeX.

Functions, symbols, special characters

Subscripts, superscripts, integrals

Fractions, matrices, multilines

Alphabets and typefaces

Delimiters: parentheses, brackets, bars, etc.

Spacing

Note that TeX handles most spacing automatically, but you may sometimes want manual control.

Feature Syntax How it looks rendered
double quad space a \qquad b a \qquad b
quad space a \quad b a \quad b
text space a\ b a\ b
text space without PNG conversion a \mbox{ } b a b
large space a\;b a\;b
medium space a\>b [not supported]
small space a\,b a\,b
no space ab ab\,
small negative space a\!b a\!b

Align with normal text flow

Due to the default css

img.tex { vertical-align: middle; }

an inline expression like \ \int_{-N}^{N} e^x\, dx \ might look good or it might need some adjustment.

If you need to align it otherwise, use <font style="vertical-align:-100%;"><math>...</math></font> and play with the vertical-align argument until you get it right; however, how it looks may depend on the browser and the browser settings.

Also note that if you rely on this workaround, if/when the rendering on the server gets fixed in future releases, as a result of this extra manual offset your formulae will suddenly be aligned incorrectly. So use it sparingly, if at all.

Forced PNG rendering

To force the formula to render as PNG, add \, (small space) at the end of the formula (where it is not rendered). This will force PNG if the user is in "HTML if simple" mode, but not for "HTML if possible" mode (math rendering settings in preferences).

You can also use \,\! (small space and negative space, which cancel out) anywhere inside the math tags. This does force PNG even in "HTML if possible" mode, unlike \,.

This could be useful to keep the rendering of formulae in a proof consistent, for example, or to fix formulae that render incorrectly in HTML (at one time, a^{2+2} rendered with an extra underscore), or to demonstrate how something is rendered when it would normally show up as HTML (as in the examples above).

For instance:

Syntax How it looks rendered
a^{c+2} ac + 2
a^{c+2} \, a^{c+2} \,
a^{\,\!c+2} a^{\,\!c+2}
a^{b^{c+2}} a^{b^{c+2}} (WRONG with option "HTML if possible or else PNG"!)
a^{b^{c+2}} \, a^{b^{c+2}} \, (WRONG with option "HTML if possible or else PNG"!)
a^{b^{c+2}}\approx 5 a^{b^{c+2}}\approx 5 (due to "\approx" correctly displayed, no code "\,\!" needed)
a^{b^{\,\!c+2}} a^{b^{\,\!c+2}}
\int_{-N}^{N} e^x\, dx \int_{-N}^{N} e^x\, dx


This has been tested with most of the formulae on this page, and seems to work perfectly.

You might want to include a comment in the HTML so people don't "correct" the formula by removing it:

<!-- The \,\! is to keep the formula rendered as PNG instead of HTML. Please don't remove it.-->

Color

Equations can use color:

  • {\color{Blue}x^2}+{\color{Brown}2x}-{\color{OliveGreen}1}
    {\color{Blue}x^2}+{\color{Brown}2x}-{\color{OliveGreen}1}
  • x_{1,2}=\frac{-b\pm\sqrt{\color{Red}b^2-4ac}}{2a}
    x_{1,2} \ = \ \frac{-b\pm\sqrt{\color{Red}b^2-4ac}}{2a}

See here for all named colours supported by LaTeX.

Note that color should not be used as the only way to identify something because color blind people may not be able to distinguish between the two colors.

Examples

See also

External links

  • A PDF Short Introduction to LaTeX — skip to page 59 for the math section. See page 72 for a complete reference list of symbols included in LaTeX and AMS-LaTeX.
  • MathML — A product of the W3C Math working group — is a low-level specification for describing mathematics as a basis for machine to machine communication.
Personal tools