User:Hadseys/Formatting/Formulae

MediaWiki uses a subset of TeX markup, including some extensions from LaTeX and AMS-LaTeX, 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.)

More precisely, MediaWiki filters the markup through Texvc, which in turn passes the commands to TeX for the actual rendering. Thus, only a limited part of the full TeX language is supported; see below for details.

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

Similarly 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. See.

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. The css selector of the images is img.tex.

It should be pointed out that most of these shortcomings have been addressed by Maynard Handley, but have not been released yet.

The  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   and.

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  or. For example,   gives $$\mbox{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 Help:Special characters).

$$\frac{p_{1}}{p_{2}} \ = \ \int_{B_{1}}\!\!\!f\ \Bigg / \int_{B_{2}}\!\!\!f \ \ \nsim\ \ \frac{B_{1}}{B_{2}} $$

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

Pros of HTML

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

Pros of TeX

 * TeX is semantically superior to HTML. In TeX, " " means "mathematical variable $$x$$", whereas in HTML " " could mean anything. Information has been irrevocably lost. This has multiple benefits:
 * 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 drop information/contents. It's more a reason to help improve the situation.
 * TeX can be converted to MathML for browsers which support it, thus keeping its semantics and allowing it to be rendered as a vector.
 * TeX has been specifically designed for typesetting formulae, so input is easier and more natural, and output is more aesthetically pleasing.
 * When writing in TeX, editors need not worry about browser support, since it is rendered into an image by the server. HTML formulae, on the other hand, can end up being rendered inconsistent of editor's intentions (or not at all), by some browsers or older versions of a browser.

Fractions, matrices, multilines
$$\frac{2}{4}=0.5$$

Small Fractions $$\tfrac{2}{4} = 0.5$$

Large (normal) Fractions $$\dfrac{2}{4} = 0.5$$

Large (nestled) Fractions $$\cfrac{2}{c + \cfrac{2}{d + \cfrac{2}{4}}} = a$$

Binomial coefficients $$\binom{n}{k}$$

Small Binomial coefficients $$\tbinom{n}{k}$$

Large (normal) Binomial coefficients $$\dbinom{n}{k}$$

Matrices \begin{matrix} x & y \\ z & v \end{matrix} $$\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{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} 0 & \cdots & 0 \\ \vdots & \ddots & \vdots \\ 0 & \cdots & 0\end{bmatrix} $$

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

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

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

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

Multiline equations \begin{align} f(x) & = (a+b)^2 \\ & = a^2+2ab+b^2 \\ \end{align} $$ \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} $$ \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}   $$\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} $$\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 $$f(x) \,\!$$ $$= \sum_{n=0}^\infty a_n x^n $$ $$= a_0+a_1x+a_2x^2+\cdots$$ $$f(x) \,\!$$$$= \sum_{n=0}^\infty a_n x^n $$$$= a_0 +a_1x+a_2x^2+\cdots$$

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

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

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

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 = 2 \sinh N$$ 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  (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:

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:



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. See Manual of Style.

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=\frac{-b\pm\sqrt{b^2-4ac}}{2a}$$ $$x=\frac{-b\pm\sqrt{b^2-4ac}}{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_{new} = S_{old} + \frac{ \left( 5-T \right) ^2} {2}$$ $$S_{new} = S_{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_{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 & 0 < x \le 1\end{cases}$$ $$ f(x) = \begin{cases} 1 & -1 \le x < 0 \\ \frac{1}{2} & x = 0 \\ 1 - x^2 & 0 < x\le 1 \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\cdot\cdot\cdot(a_p)_n}{(c_1)_n\cdot\cdot\cdot(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\cdot\cdot\cdot(a_p)_n}{(c_1)_n\cdot\cdot\cdot(c_q)_n} \frac{z^n}{n!}$$

Bug reports
Discussions, bug reports and feature requests should go to the Wikitech-l mailing list. These can also be filed on Mediazilla under MediaWiki extensions.