Help:Formula

MediaWiki and TeX
MediaWiki 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.

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.

MediaWiki templates, variables and parameters cannot be used within math tags, see m:Template talk:Demo of attempt to use parameters within TeX.

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).

The use of HTML in stead 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 appearence respects CSS and browser settings.

Pros of TeX

 * 1) TeX is semantically superior to HTML. In TeX, " " means "mathematical variable $$x$$", whereas in HTML " " could mean anything. Information has irremediably been lost.
 * 2) TeX has been specifically designed for typesetting formulae, so input is easier and more natural, and output is more aesthetically pleasing. Also anybody who has written mathematics at a professional level is already familiar with TeX.
 * 3) One consequence of point 1 is that TeX can be transformed in 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.
 * 4) 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.

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$$ should look good.

If you need to align it otherwise, use  $$...$$  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:

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:



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_{1,2}=\frac{-b\pm\sqrt{b^2-4ac}}{2a}$$ $$x_{1,2}=\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)$$

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\\x&0<x\le 1\end{cases}$$ f(x) = \begin{cases}1 & -1 \le x < 0\\ \frac{1}{2} & x = 0\\x&0<x\le 1\end{cases}

Prefixed subscript
$${}_pF_q(a_1,...,a_p;c_1,...,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,...,a_p;c_1,...,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.