Help:Math

MediaWiki Math is a feature of MediaWiki that allows for the direct rendition of tagged mathematical symbols in an article. With this feature, an editor can make mathematical formulas look far more impressive with the TeX formatting that it provides.

MediaWiki Math is now available on CreationWiki. The recent transfer to a dedicated server enabled the installation of the support software that MediaWiki math requires.

An example
To render a simple expression, type:

$$x + y = z$$

With no further function calls, MediaWiki Math will render this in stylized italics, thus:

$$x + y = z$$

This is significantly different from normal wikified italics. Thus:

x + y = z

yields

x + y = z

To make the above text somewhat larger, type:

This yields:

$$x + y = z\,$$

To render a more sophisticated expression, one must use LaTeX function calls. For example, to render the square root of 1 minus the square of the constant e (or a variable named e), type:

$$\sqrt{1-e^2}$$

This will display thus:

$$\sqrt{1-e^2}$$

Syntax
The math syntax has two parts--the tags and the code. The tags are $$ to begin and $$ to end. The code can be a simple expression containing variables and operators (usually arithmetic and/or relational operators that appear as typed characters) or a complex expression requiring escaped function calls to guide the rendering of arcane symbols that can never appear on any keyboard.

TeX code must appear literally. Template substitution does not render properly, and some symbols, notably the # sign, result in execute-time errors. However, literal math-tagged code will render properly as the "then" or "else" part of an #if statement. In fact, an editor can use math-tagged code within a template, but may not invoke a template between math tags.

Rendering
If rendition is successful, the math executable will produce a PNG-type image, store it in a special output directory, and display it in the article at the location of the tags. Otherwise, the text and/or code between the tags will display as is, as it would for any "broken image link."

Rendition normally consists of black text on a white background, unless the editor calls a function for a different foreground color. (Background colors are not subject to change in the math routine; they depend on the skin.)

If an editor uses no function calls, then the routine renders letters in stylized italics and digits in normal typeface.

Function, symbol, and special character dictionary
These tables come from the MetaWikipedia, and more particularly from the designers of the MediaWiki Math system.

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

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

Large (normal) Fractions $$\dfrac{2}{4} = 0.5 \qquad \dfrac{2}{c + \dfrac{2}{d + \dfrac{2}{4}}} = a$$

Large (nested) 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}$$

Arrays \begin{array}{|c|c||c|} a & b & S \\ \hline 0&0&1\\ 0&1&1\\ 1&0&1\\ 1&1&0\\ \end{array} $$ \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 (the executable, see below) 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:


 * $${\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 colors supported by LaTeX.

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

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

Alignment with normal text flow
Due to the default css

img.tex { vertical-align: middle; }

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 (see some of the examples in the above tables), 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.

An editor might have to do this 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:



Commutative diagrams
Commutative diagrams are not directly supported in CreationWiki, nor does MediaWiki have any good way to render them on the server. MediaWiki recommends that an editor write such a diagram in TeX, convert it to an .svg image, and then upload it. Please upload any outside-generated images into the Media Pool.

Math Support Explained
"Math support" on any wiki is all a matter of rendering certain mathematical symbols, not as text characters, but as special images. To do this, MediaWiki uses a special executable called texvc, which a developer must typically build using the Objective CAML language (a development product of the French Institut national de recherche en informatique et en automatique (INRIA), or in English, The National Institute for Research in Computer Science and Control (the official English name). In addition, MediaWiki Math requires the following auxiliary executables on the server:


 * LaTeX
 * dvips
 * gs, part of Ghostscript
 * convert, part of ImageMagick. (Note: not all MediaWiki sites use ImageMagick. Servers that have GL built-in can still handle images without it. But a MediaWiki site cannot render math without it.)

Reasons for using TeX
TeX is a widely recognized format for representing mathematical formulas. It is semantically richer than HTML and thus avoids misunderstanding. Furthermore, the rendition of TeX is constant across browsers, precisely because the server generates an image on-the-fly and does not rely on the browser to render the code.