Help:Displaying a formula

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

To have math rendered, you have to set  in LocalSettings.php.

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. See for more information.

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. $$sin a$$ It should be pointed out that solutions to most of these shortcomings have been proposed by Maynard Handley, but have not been implemented 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 $$\text{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 codes on the left produce the symbols on the right, but the latter can also be put directly in the wikitext.

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, " " means "mathematical variable $$x$$", whereas in HTML " " 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 drop information/contents. It's more a reason to help improve the situation.
 * 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 rendered vectorially.
 * 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 persuade them to contribute if they can write in TeX.

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 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 en:Wikipedia: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, 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:



Commutative diagrams
To make a commutative diagram, there are three steps:
 * Write the diagram in TeX
 * Convert to SVG
 * Upload the file to Wikimedia Commons

Diagrams in TeX
Xy-pic (online manual) is the most power and general-purpose diagram package in TeX.

Simpler packages include:
 * AMS's amscd
 * Paul Taylor's diagrams
 * François Borceux Diagrams

The following is a template for Xy-pic, together with a hack to increase the margins in dvips, so that the diagram is not truncated by over-eager cropping (suggested in TUGboat: TUGboat, Volume 17 1996, No. 3): \documentclass{amsart} \usepackage[all, ps, dvips]{xy} % Loading the XY-Pic package % Using postscript driver for smoother curves \usepackage{color}             % For invisible frame \begin{document} \thispagestyle{empty} % No page numbers \SelectTips{eu}{}    % Euler arrowheads (tips) \setlength{\fboxsep}{0pt} % Frame box margin {\color{white}\framebox} % end math, end frame \end{document}

Convert to SVG
Once one has the TeX code, one can produce an SVG file via the following, which assume the TeX file is called comm.tex: latex comm.tex dvips -E -y 2500 -o comm.eps comm.dvi eps2eps -dNOCACHE comm.eps comm2.eps pstoedit -f sk comm2.eps comm.sk inkscape -z -f comm.sk -l comm.svg These produce a DVI file, convert it to EPS (rescaling by 2.5x), convert fonts to outlines, and convert to SVG via Sketch.

This assumes several pieces of software:
 * a working TeX distribution, such as TeX Live
 * Ghostscript
 * pstoedit
 * Inkscape

Upload the file
As the diagram is your own work, upload it to Wikimedia Commons, so that all projects (notably, all languages) can use it without having to copy it to their language's Wiki. (If you've previously uploaded a file to somewhere other than Commons, transwiki it to Commons.)

Now go to the image page and add a description, including the source code, using this template:
 * Check size: Before uploading, check that the default size of the image is neither too large nor too small by opening in an SVG application and viewing at default size (100% scaling), otherwise adjust the -y option to dvips.
 * Name: Make sure the file has a meaningful name.
 * Upload: Login to Wikimedia Commons, then upload the file ; for the Summary, give a brief description.

Category:Commutative diagrams


 * Source code:
 * Include the source code in the image page, in the Source section of the Information template, so that the diagram can be edited in future.
 * Include the complete .tex file, not just the fragment, so future editors do not need to reconstruct a compilable file.
 * (Don't include it in the Summary section, which is just supposed to be a summary.)
 * License: The most common license for commutative diagrams is PD-self; some use PD-ineligible, especially for simple diagrams, or other licenses. Please do not use the GFDL, as it requires the entire text of the GFDL to be attached to any document that uses the diagram.
 * Description: If possible, link to a Wikipedia page relevant to the diagram.
 * Category: Include  </tt>, so that it appears in commons:Category:Commutative diagrams. There are also subcategories, which you may choose to use.
 * Include image: Now include the image on the original page via  [[Image:Diagram.svg]] </tt>

Examples
A sample conforming diagram is.

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

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

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.