Help:Formulas

MediaWiki uses a subset of AMS-LaTeX markup, a superset of LaTeX markup which is in turn a superset of TeX markup, 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 become smarter, it will be able to generate enhanced HTML or even MathML in many cases.

Syntax
Math markup goes inside. The edit toolbar has a button for this.

Similar to HTML, in TeX extra spaces and newlines 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 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.

The alt text of the PNG images, which is displayed to visually impaired and other readers who cannot see the images, defaults to the wikitext that produced the image, excluding the  and. You can override this by explicitly specifying an  attribute for the   element. For example,  generates an image \sqrt{\pi} whose alt text is "Square root of pi".

Apart from function and operator names, as is customary in mathematics, variables and letters are in italics; digits are not. For other text, (like variable labels) to avoid being rendered in italics like variables, use  or. For example,  gives $$\text{abc}$$. This does not work for special characters; they are ignored unless the whole $$ expression is rendered in HTML:



gives:



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.

The codes on the left produce the symbols on the right, but the latter can also be put directly in the wikitext, except for &lsquo;=&rsquo;.

Both HTML and TeX have advantages in some situations.

Pros of HTML

 * 1) Formulas in HTML behave more like regular text. In-line HTML formulas always align properly with the rest of the HTML text and, to some degree, can be cut-and-pasted. The formula's background and font size match the rest of HTML contents and the appearance respects CSS and browser settings while the typeface is conveniently altered to help you identify formulas. The display of a formula entered using mathematical templates can be conveniently altered by modifying the templates involved; this modification will affect all relevant formulas without any manual intervention. Formulas typeset with HTML code will be accessible to client-side script links (a.k.a. scriptlets).
 * 2) Pages using HTML code for formulas will load faster.
 * 3) The HTML code, if entered diligently, will contain all semantic information to transform the equation back to TeX or any other code as needed. It can even contain differences TeX does not normally catch, e.g.   for the imaginary unit and   for an arbitrary index variable.

Pros of TeX

 * 1) TeX is semantically more precise than HTML.
 * 2) In TeX, " " means "mathematical variable x", whereas in HTML " " is generic and somewhat ambiguous.
 * 3) On the other hand, if you encode the same formula as " ", you get the same visual result $&amp;alpha;$ and no information is lost. This requires diligence and more typing that could make the formula harder to understand as you type it. However, since there are far more readers than editors, this effort is worth considering.
 * One consequence of this is that TeX code 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 is true that the current situation is not ideal, but that is not a good reason to drop information/contents. Another consequence of this is that TeX can be converted to MathML for browsers which support it, thus keeping its semantics and allowing the rendering to be better suited for the reader's graphic device.
 * 1) TeX is the preferred text formatting language of most professional mathematicians, scientists, and engineers. It is easier to persuade them to contribute if they can write in TeX. TeX has been specifically designed for typesetting formulas, so input is easier and more natural if you are accustomed to it, and output is more aesthetically pleasing if you focus on a single formula rather than on the whole containing page. Once a formula is done correctly in TeX, it will render reliably, whereas the success of HTML formulas is somewhat dependent on browsers or versions of browsers. Another aspect of this dependency is fonts: the serif font used for rendering formulas is browser-dependent and it may be missing some important glyphs. While browsers are generally able to substitute a matching glyph from a different font family, this may not work for combined glyphs (compare &lsquo;  &rsquo; and &lsquo; a&#773; &rsquo;).
 * 2) TeX formulas, by default, render larger and are usually more readable than HTML formula and are not dependent on client-side browser resources, such as fonts, and so the results are more reliably WYSIWYG.
 * 3) While TeX does not assist you in finding HTML codes or Unicode values (which you can obtain by viewing the HTML source in your browser), cutting and pasting from a TeX PNG in Wikipedia into simple text will return the LaTeX source.

In some cases it may be the best choice to use neither TeX nor the html-substitutes, but instead the simple ASCII symbols of a standard keyboard (see below, for an example).

Functions, symbols, special characters
{|class="wikitable" !colspan="2"|

Accents/diacritics
!colspan="2"|

Standard functions
!colspan="2"|

Bounds
!colspan="2"|

Projections
!colspan="2"|

Differentials and derivatives
!colspan="2"|

Letter-like symbols or constants
!colspan="2"|

Modular arithmetic
!colspan="2"|

Radicals
!colspan="2"|

Operators
!colspan="2"|

Sets
!colspan="2"|

Relations
!colspan="2"|

Geometric
!colspan="2"|

Logic
!colspan="2"|

Arrows
!colspan="2"|

Special
!colspan="2"|

Unsorted (new stuff)

 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }

For a little more semantics on these symbols, see the brief TeX Cookbook.

Fractions, matrices, multilines
or

Small fractions

Large (normal) fractions

Large (nested) fractions

Binomial coefficients

Small binomial coefficients

Large (normal) binomial coefficients

Matrices \begin{matrix} x & y \\ z & v \end{matrix}

\begin{vmatrix} x & y \\ z & v \end{vmatrix}

\begin{Vmatrix} x & y \\ z & v \end{Vmatrix}

\begin{bmatrix} 0 & \cdots & 0 \\ \vdots & \ddots & \vdots \\ 0 & \cdots & 0 \end{bmatrix}

\begin{Bmatrix} x & y \\ z & v \end{Bmatrix}

\begin{pmatrix} x & y \\ z & v \end{pmatrix}

\bigl( \begin{smallmatrix} a&b\\ c&d \end{smallmatrix} \bigr)

Case distinctions f(n) = \begin{cases} n/2, & \text{if }n\text{ is even} \\ 3n+1, & \text{if }n\text{ is odd} \end{cases}

Multiline equations \begin{align} f(x) & = (a+b)^2 \\ & = a^2+2ab+b^2 \\ \end{align}

\begin{alignat}{2} f(x) & = (a-b)^2 \\ & = a^2-2ab+b^2 \\ \end{alignat} Multiline equations (must define number of colums used ({lcr}) (should not be used unless needed) \begin{array}{lcl} z & = & a \\ f(x,y,z) & = & x + y + z \end{array}

Multiline equations (more) \begin{array}{lcr} z & = & a \\ f(x,y,z) & = & x + y + z \end{array}

Breaking up a long expression so that it wraps when necessary, at the expense of destroying correct spacing f(x) \,\!

f(x) \,\!

Simultaneous equations \begin{cases} 3x + 5y + z \\ 7x - 2y + 4z \\ -6x + 3y + 2z \end{cases}

Arrays \begin{array}{|c|c||c|} a & b & S \\ \hline 0&0&1\\ 0&1&1\\ 1&0&1\\ 1&1&0\\ \end{array}

Parenthesizing big expressions, brackets, bars
You can use various delimiters with \left and \right:

Alphabets and typefaces
Texvc cannot render arbitrary Unicode characters. Those it can handle can be entered by the expressions below. For others, such as Cyrillic, they can be entered as Unicode or HTML entities in running text, but cannot be used in displayed formulas.

Color
Equations can use color:





Note that color should not be used as the only way to identify something, because it will become meaningless on black-and-white media or for color-blind people.

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

Automatic spacing may be broken in very long expressions (because they produce an overfull hbox in TeX):
 * $$0+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+\cdots$$
 * $$0+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+\cdots$$

This can be remedied by putting a pair of braces { } around the whole expression:
 * $${0+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+\cdots}$$
 * $${0+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+\cdots}$$

Alignment with normal text flow
Due to the default CSS

an inline expression like $$\int_{-N}^{N} e^x\, dx$$ should look good.

If you need to align it otherwise, use  and play with the   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  at the end of a formula to force PNG even in "HTML if possible" mode, unlike. If it is understandable to have  at the end of a formula for some reason, it may also be used at the beginning or the combinations   and   (a negative and positive space which cancel out) may appear anywhere within the math expression to force PNG.

Forcing PNG can be useful to keep the rendering of formulas in a proof consistent, for example, or to fix formulas 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:

in the middle of the expression.)

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 formulas 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:



Commutative diagram
$$ \begin{array}{lcl} & X & \overset{f}\longrightarrow & Z & \\ &g \downarrow &&\downarrow g'\\ &Y & \underset{f'}\longrightarrow& W & \\ \end{array} $$ $$ \begin{array}{lcl} & X & \overset{f}\longrightarrow & Z & \\ &g \downarrow &&\downarrow g'\\ &Y & \underset{f'}\longrightarrow& W & \\ \end{array} $$

Quadratic polynomial
$$ax^2 + bx + c = 0$$ $$ax^2 + bx + c = 0$$

Quadratic polynomial (force PNG rendering)
$$ax^2 + bx + c = 0\,\!$$ $$ax^2 + bx + c = 0\,\!$$

Quadratic formula
$$x={-b\pm\sqrt{b^2-4ac} \over 2a}$$ $$x={-b\pm\sqrt{b^2-4ac} \over 2a}$$

Tall parentheses and fractions
$$2 = \left( \frac{\left(3-x\right) \times 2}{3-x} \right)$$ $$2 = \left( \frac{\left(3-x\right) \times 2}{3-x} \right)$$

$$S_{\text{new}} = S_{\text{old}} - \frac{ \left( 5-T \right) ^2} {2}$$ $$S_{\text{new}} = S_{\text{old}} - \frac{ \left( 5-T \right) ^2} {2}$$

Integrals
$$\int_a^x \!\!\!\int_a^s f(y)\,dy\,ds = \int_a^x f(y)(x-y)\,dy$$ $$\int_a^x \!\!\!\int_a^s f(y)\,dy\,ds = \int_a^x f(y)(x-y)\,dy$$

Summation
$$\sum_{i=0}^{n-1} i$$ $$\sum_{i=0}^{n-1} i$$

$$\sum_{m=1}^\infty\sum_{n=1}^\infty\frac{m^2\,n}{3^m\left(m\,3^n+n\,3^m\right)}$$ $$\sum_{m=1}^\infty\sum_{n=1}^\infty\frac{m^2\,n} {3^m\left(m\,3^n+n\,3^m\right)}$$

Differential equation
$$u'' + p(x)u' + q(x)u=f(x),\quad x>a$$ $$u'' + p(x)u' + q(x)u=f(x),\quad x>a$$

Complex numbers
$$|\bar{z}| = |z|, |(\bar{z})^n| = |z|^n, \arg(z^n) = n \arg(z)$$ $$|\bar{z}| = |z|, |(\bar{z})^n| = |z|^n, \arg(z^n) = n \arg(z)$$

Limits
$$\lim_{z\rightarrow z_0} f(z)=f(z_0)$$ $$\lim_{z\rightarrow z_0} f(z)=f(z_0)$$

Integral equation
$$\phi_n(\kappa) = \frac{1}{4\pi^2\kappa^2} \int_0^\infty \frac{\sin(\kappa R)}{\kappa R} \frac{\partial}{\partial R} \left[R^2\frac{\partial D_n(R)}{\partial R}\right]\,dR$$ $$\phi_n(\kappa) = \frac{1}{4\pi^2\kappa^2} \int_0^\infty \frac{\sin(\kappa R)}{\kappa R} \frac{\partial}{\partial R} \left[R^2\frac{\partial D_n(R)}{\partial R}\right]\,dR$$

Example
$$\phi_n(\kappa) = 0.033C_n^2\kappa^{-11/3},\quad \frac{1}{L_0}\ll\kappa\ll\frac{1}{l_0}$$ $$\phi_n(\kappa) = 0.033C_n^2\kappa^{-11/3},\quad \frac{1}{L_0}\ll\kappa\ll\frac{1}{l_0}$$

Continuation and cases
$$f(x) = \begin{cases}1 & -1 \le x < 0 \\ \frac{1}{2} & x = 0 \\ 1 - x^2 & \text{otherwise}\end{cases}$$ $$ f(x) = \begin{cases} 1 & -1 \le x < 0 \\ \frac{1}{2} & x = 0 \\ 1 - x^2 & \text{otherwise} \end{cases} $$

Prefixed subscript
$${}_pF_q(a_1,\dots,a_p;c_1,\dots,c_q;z) = \sum_{n=0}^\infty \frac{(a_1)_n\cdots(a_p)_n}{(c_1)_n\cdots(c_q)_n}\frac{z^n}{n!}$$ $${}_pF_q(a_1,\dots,a_p;c_1,\dots,c_q;z) = \sum_{n=0}^\infty \frac{(a_1)_n\cdots(a_p)_n}{(c_1)_n\cdots(c_q)_n} \frac{z^n}{n!}$$

Fraction and small fraction
$$\frac{a}{b}\ \tfrac{a}{b}$$ $$\frac{a}{b}\ \tfrac{a}{b}$$

Repeating fraction
$$0.10\overline{00101010}$$ $$0.10\overline{00101010}$$

Area of a quadrilateral
$$S=dD\,\sin\alpha\!$$ $$S=dD\,\sin\alpha\!$$

Volume of a sphere-stand
$$V=\tfrac16\pi h\left[3\left(r_1^2+r_2^2\right)+h^2\right]$$ $$V=\tfrac16\pi h\left[3\left(r_1^2+r_2^2\right)+h^2\right]$$