Help:Displaying a formula



There are three methods for displaying formulas in Wikipedia: raw HTML, HTML with math templates (abbreviated here as math), and a subset of LaTeX implemented with the HTML markup  (referred to as LaTeX in this article). Each method has some advantages and some disadvantages, which have evolved over time with improvements of MediaWiki. The manual of style MOS:MATH has not always evolved accordingly. So the how-to recommendations that appear below may differ from those of the manual of style. In this case, they express a consensus resulting of the practice of most experienced members of WikiProject Mathematics and many discussions at Wikipedia talk:WikiProject Mathematics.

For example, the famous Einstein formula can be entered in raw HTML as, which is rendered as  (the template nowrap is here only for avoiding a line break inside the formula). With math, it can be entered as, which is rendered as $E = mc$. With LaTeX, it is entered as, and rendered as E=mc^2.

Use of raw HTML
Variable names and many symbols look very different with raw HTML and the other display methods. This may be confusing in the common case where several methods are used in the same article. Moreover, mathematicians who are used to reading and writing texts written with LaTeX often find the raw HTML rendering awful.

So, raw HTML should normally not be used for new content. However, raw HTML is still present in many mathematical articles. It is generally a good practice to convert it to math format, but coherency must be respected; that is, such a conversion must be done in a whole article, or at least in a whole section. Moreover, such a conversion must be identified as such in the edit summary, and making other changes in the same edit should be avoided. This is for helping other users to identify changes that are possibly controversial (the "diff" of a conversion may be very large, and may hide other changes).

Converting raw HTML to math is rather simple: when the formula is enclosed with nowrap, it suffices to change "nowrap" into "math". However, if the formula contains an equal sign, one has to add  just before the formula for avoiding confusion with the template syntax; for example,. Also, vertical bars, if any, must either be replaced with or avoided by using.

LaTeX vs.
These two ways of writing mathematical formulas each have their advantages and disadvantages. They are both accepted by the manual of style MOS:MATH. The rendering of variable names is very similar. Having a variable name displayed in the same paragraph with and   is generally not a problem.

The disadvantages of LaTeX are the following: On some browser configurations, LaTeX inline formulas appear with a slight vertical misalignment, or with a font size that may be slightly different from that of the surrounding text. This is not a problem with a block displayed formula, and also typically not with inline formulas that exceed the normal line height marginally (for example formulas with subscripts and superscripts). The use of LaTeX in a piped link or in a section heading does not appear in blue in the linked text or the table of content. Moreover, links to section headings containing LaTeX formulas do not always work as expected. Finally, having many LaTeX formulas may significantly increase the processing time of a page. LaTeX formulas should be avoided in image captions or footnotes, because when the image is clicked for a larger display or a footnote is selected on a mobile device, LaTeX in the caption or footnote will not render.

Disadvantages of math include that not all formulas can be displayed, and while it may be possible to display a complicated formula with math, it may be poorly rendered. Except for the most common symbols such as letters, numerals, and basic punctuation, rendering of Unicode mathematical symbols can be inconsistent in size or alignment where fallback fonts do not match, and some readers may not have any font which includes certain uncommon symbols. Spaces within a formula must be directly managed (for example by including explicit hair or thin spaces). Variable names must be italicized explicitly, and superscripts and subscripts must use an explicit tag or template. Except for short formulas, the source of a formula typically has more markup overhead and can be difficult to read.

The common practice of most members of WikiProject mathematics is the following:
 * Use of mvar and math for isolated variables and math for simple inline formulas; or alternately the use of LaTeX for these purposes (optionally using the tmath template), especially on articles with many complex formulas or where rendering seems inconsistent
 * Use of mvar and math for formulas in image captions, even if the rendering is mediocre
 * Use of LaTeX for separately displayed formulas and more complicated inline formulas
 * Use of LaTeX for formulas involving symbols that are not regularly rendered in Unicode (see MOS:BBB)
 * Avoid formulas in section headings, and when this is necessary, use raw HTML (see Finite field for an example)

The choice between math and LaTeX depends on the editor. Converting a page from one format to another must be done with stronger reasons than editor preference.

Display format of LaTeX
By default SVG images with non-visible MathML are generated. The text-only form of the LaTeX can be set via user preferences at My Preferences – Appearance – Math.

The hidden MathML can be used by screen readers and other assistive technology. To display the MathML in Firefox:
 * Install the Native MathML extension
 * Or copy its CSS rules to your Wikipedia user stylesheet.

In either case, you must have fonts that support MathML (see developer.mozilla.org) installed on your system. For copy-paste support in Firefox, you can also install MathML Copy.

Use of HTML templates
TeX markup is not the only way to render mathematical formulas. For simple inline formulas, the template math and its associated templates are often preferred. The following comparison table shows that similar results can be achieved with the two methods. See also Help:Special characters.

Here is a summary of the mathematical templates:

Care should be taken when writing sets within math, as braces, equal signs, and vertical bars can conflict with template syntax. The mset template is available for braces, as shown in the example above. Likewise, abs encloses its parameter inside vertical bars to help with the pipe character conflicting with template syntax. For a single vertical bar, use, and for an equal sign, use.

HTML entities
Though Unicode characters are generally preferred, sometimes HTML entities are needed to avoid problems with wiki syntax or confusion with other characters:

In the table below, the codes on the left produce the symbols on the right, but these symbols can also be entered directly in the wikitext either by typing them if they are available on the keyboard, by copy-pasting them, or by using menus below the edit windows. (When editing any Wikipedia page in a desktop web browser, use the "Insert" pulldown menu immediately below the article text, or the "Special characters" menu immediately above the article text.) Normally, lowercase Greek letters should be entered in italics, that is, enclosed between two single quotes.

Spacing
To avoid line-wrapping in the middle of a formula, use math. If necessary, a non-breaking space can be inserted with "&amp;nbsp;". When an inline formula is long enough, it can be helpful to allow it to break across lines. Whether using LaTeX or templates, split the formula at each acceptable breakpoint into separate  tags or math templates with any binary relations or operators and intermediate whitespace included at the trailing rather than leading end of a part.

Typically whitespace should be a regular space or none at all. In rare circumstances, such as where one character overlaps another due to one being in italics, a thin space can be added with thin space.

More
For more on Wikipedia howtos and math style guidelines:
 * MOS:FRAC for how to input fractions in various circumstances
 * MOS:RADICAL for how to square and other roots in various circumstances
 * Per MOS:ELLIPSIS, "..." (three ASCII periods) is used instead of "…" (single character)
 * Mathematical operators and symbols in Unicode
 * Manual of Style (mathematics)
 * Manual of Style (mathematics)

For more on special characters:
 * Unicode character name index can be used to find the Unicode number of a character.
 * W3C list of MathML characters indexed by code or name
 * List of XML and HTML character entity references – includes all named entities
 * Glossary of mathematical symbols

LaTeX basics
Math markup goes inside. Chemistry markup goes inside  or. can be used in place of  to avoid line wrapping of adjacent text (with caveats). All these tags use TeX.

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.

The now deprecated tag  was considered too ambiguous, and it has been replaced by.

LaTeX commands
LaTeX commands are case-sensitive, and take one of the following two formats:
 * They start with a backslash  and then have a name consisting of letters only. Command names are terminated by a space, a number or any other "non-letter" character.
 * They consist of a backslash  and exactly one non-letter.

Some commands need an argument, which has to be given between curly braces  after the command name. Some commands support optional parameters, which are added after the command name in square brackets. The general syntax is:

\commandname[option1,option2,...]{argument1}{argument2}...

Special characters
The following symbols are reserved characters that either have a special meaning under LaTeX or are unavailable in all the fonts. If you enter them directly in your text, they will normally not render, but rather do things you did not intend.


 * 1) $ % ^ & _ { } ~ \

These characters can be entered by prefixing the escape character backslash  or using special sequences:

\# \$ \% ^\wedge \& \_ \{ \} \sim \backslash

yielding:

The backslash character  can not be entered by adding another backslash in front of it ; this sequence is used for line breaking. For introducing a backslash in math mode, you can use  instead which gives \backslash.

The command  produces a tilde which is placed over the next letter. For example,  gives \tilde{a}. To produce just a tilde character ~, use  which gives \tilde{}, placing a ~ over an empty box. Alternatively  produces \sim, a large centred ~ which may be more appropriate in some situations.

The command  produces a hat over the next character, for example   produces \hat{o}. For a stretchable version use  giving. The wedge  is normally used as a mathematical operator \wedge. The sequence  produces ^\wedge the best equivalent to the ASCII caret ^ character.

Spaces
"Whitespace" characters, such as blank or tab, are treated uniformly as "space" by LaTeX. Several consecutive whitespace characters are treated as one "space". See below for commands that produces spaces of different size.

LaTeX environments
Environments in LaTeX have a role that is quite similar to commands, but they usually have effect on a wider part of formula. Their syntax is: Environments supported by Wikipedia include matrix, align, etc. See below.

Rendering
The 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; a work-around is described in the "Alignment with normal text flow" section below. The CSS selector of the images is.

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   (formerly  ). You can also define new function names using. For example,  gives \text{abc}. provides spacing before and after the operator name when appropriate, as when  is rendered as  (with space to the left and right of "sn") and   as  (with space to the left and not to the right). LaTeX's starred version,  is not supported, but a workaround is to add   instead. For example,  renders as

LaTeX does not have full support for Unicode characters, and not all characters render. Most Latin characters with accents render correctly. However some do not, in particular those that include multiple diacritics (e.g. with Latin letters used in Vietnamese) or that cannot be precomposed into a single character (such as the uppercase Latin letter W with ring), or that use other diacritics (like the ogonek or the double grave accent, used in Central European languages like Polish, or the horn attached above some vowels in Vietnamese), or other modified letter forms (used in IPA notations, or African languages, or in medieval texts), some digram ligatures (like Ĳ in Dutch), or Latin letters borrowed from Greek, or small capitals, as well as superscripts and subscript letters. For example,  and   (used in Icelandic) will give errors.

The normal way of entering quotation marks in text mode (two back ticks for the left and two apostrophes for the right), such as  will not work correctly. As a workaround, you can use the Unicode left and right quotation mark characters, which are available from the "Symbols" dropdown panel beneath the editor:.

Force-rerendering of formulas
MediaWiki stores rendered formulas in a cache so that the images of those formulas do not need to be created each time the page is opened by a user. To force the rerendering of all formulas of a page, you must open it with the getter variables. Imagine for example there is a wrong rendered formula in the article Integral. To force the re-rendering of this formula you need to open the URL

Afterwards you need to bypass your browser cache so that the new created images of the formulas are actually downloaded.

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

Accents and diacritics
!colspan="2"|

Standard numerical 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"|
 * a \bmod b
 * a \bmod b
 * a \bmod b
 * a \bmod b
 * a \bmod b
 * a \bmod b

Radicals
!colspan="2"|

Operators
!colspan="2"|

Sets
!colspan="2"|

Relations
!colspan="2"|

Geometric
!colspan="2"|

Logic
don't use  which is now deprecated don't use  which is now deprecated

!colspan="2"|

Arrows
!colspan="2"|

Special
!colspan="2"|

Unsorted (new stuff)

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

For a little more semantics on these symbols, see this brief TeX Cookbook or here TeX Cookbook.

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

Display attribute
The tag can take a   attribute with possible values of   and.

Inline
If the value of the display attribute is inline, the contents will be rendered in inline mode: there will be no new paragraph for the equation and the operators will be rendered to consume only a small amount of vertical space.

The sum converges to 2.

The next line-width is not disturbed by large operators.

The code for the math example reads:

The quotation marks around  are optional and   is also valid.

Technical implementation
Technically the command \textstyle will be added to the user input before the TeX command is passed to the renderer. The result will be displayed without further formatting by outputting the image or MathMLelement to the page.

Block
In block-style the equation is rendered in its own paragraph and the operators are rendered consuming less horizontal space. The equation is indented.

The sum converges to 2.

It was entered as

Technical implementation
Technically the command \displaystyle will be added to the user input (if the user input does not already contain the string \displaystyle or \align) before the TeX command is passed to the renderer. The result will be displayed in a new paragraph. Therefore, the style of the MathImage is altered i.e. the style attribute "display:block;margin:auto" is added. For MathML it is ensured that display=inline is replaced by display block which produces a new paragraph

Not specified
If nothing is specified the equation is rendered in the same display style as "block", but without using a new paragraph. If the equation does appear on a line by itself, it is not automatically indented.

The sum converges to 2.

The next line-width is disturbed by large operators.

Or:

The sum

converges to 2.

In both cases, the math is coded as:

Equation numbering
The templates NumBlk and EquationRef can be used to number equations. The template EquationNote can be used to refer to a numbered equation from surrounding text. For example, the following syntax:



produces the following result (note the equation number in the right margin):

Later on, the text can refer to this equation by its number using syntax like this:



The result looks like this:


 * As seen in equation ($&alpha;$), example text...

The equation number produced by EquationNote is a link that the user can click to go immediately to the cited equation.

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 with the  command. For example,

The  command colors all symbols to its right. However, if the  command is enclosed in a pair of braces (e.g.  ) then no symbols outside of those braces are affected.

There are several alternate notations styles

Some color names are predeclared according to the following table, you can use them directly for the rendering of formulas (or for declaring the intended color of the page background).

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

Latex does not have a command for setting the background color. The most effective way of setting a background color is by setting a CSS styling rule for a table cell:

Rendered as:

Custom colors can be defined using:



Spacing
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):



This can be remedied by putting a pair of braces { } around the whole expression:



When relational symbols such as \uparrow are employed as ordinary symbols, for example in bra–ket notation, additional spacing may have to be avoided:

Empty horizontal or vertical spacing
The  commands create empty horizontal and/or vertical space the same height and/or width of the argument.

Alignment with normal text flow
Because of the default CSS

an inline expression like 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.

If you rely on this workaround, if and when the rendering on the server gets fixed in a future release, this extra manual offset will suddenly make every affected formula align incorrectly. So use it sparingly, if at all.

Unimplemented elements and workarounds
The current Mathoid–MathJax backend has the following elements unimplemented (see also MathJax's own description of differences):

Special integral operators
The following integral operators that are not supported by the default font of MathJax 2.7 are available

they have poor horizontal spacing, generate cropped SVG images unless used with other tall characters, and appear different from the usual integral symbol :



cf. T348587. However, they render fine when MathML is used.

and
Elements which are not yet implemented are, namely a two-fold integral   (\iint) with a circular curve through the centre of the two integrals, and similarly  , a circular curve through three integrals. In contrast,  (\oint) exists for the single dimension (integration over a curved line within a plane or any space with higher dimension).

These elements appear in many contexts:  denotes a surface integral over the closed 2d boundary of a 3d region (which occurs in much of 3d vector calculus and physical applications – like Maxwell's equations), likewise   denotes integration over the closed 3d boundary (surface volume) of a 4d region, and they would be strong candidates for the next TeX version. As such there are a lot of workarounds in the present version.


 * {| class="toccolours collapsible collapsed" width="80%" style="text-align:left"

! and   using currently implemented symbols
 * looks like:
 * looks like:


 * , which uses  along with   and   (overdrawn after backspacing):


 * , which uses  twice (with some backward kerning) along with   (also overdrawn after backpacing) to produce a more consistent circle:

(should also be preferably more tightly kerned) looks more or less like:


 * which uses three \int symbols (with more backward kerning) with \subset and \supset (overdrawn after backspacing):


 * , which uses three  symbols (with more backward kerning) along with   (also overdrawn after backspacing):


 * }

However, since no standardisation exists as yet, any workaround like this (which uses many  symbols for backspacing) should be avoided, if possible. See below for a possibility using PNG image enforcement.

Note that  (the double integral) and   (the triple integral) are still not kerned as they should preferably be, and are currently rendered as if they were successive   symbols; this is not a major problem for reading the formulas, even if the integral symbols before the last one do not have bounds, so it's best to avoid backspacing "hacks" as they may be inconsistent with a possible future better implementation of integrals symbols (with more precisely computed kerning positions).

and as PNG images
These symbols are available as PNG images which are also integrated into two templates, oiint and oiiint, which take care of the formatting around the symbols.

The templates have three parameters:


 * preintegral: the text or formula immediately before the integral
 * intsubscpt: the subscript below the integral
 * integrand: the text or formula immediately after the integral

Examples

 * Stokes' theorem:




 * Ampère's law + correction:



$&alpha;$
 * Continuity of 4-momentum flux (in general relativity):

Oriented and   as PNG images
Some variants of  and   have arrows on them to indicate the sense of integration, such as a line integral around a closed curve in the clockwise sense, and higher dimensional analogues. These are not implemented in TeX on Wikipedia either, although the template intorient is available - see link for details.

Arc notation
is not yet implemented to display the arc notation. However, there exists a workaround: use, which gives

For longer arcs, use Overarc:

3.$2 1$

Triple dot
is not implemented. For a workaround use, which gives

.

Starred operatorname
The starred version of  is not currently supported. A workaround for is

Strikethrough
Strikethrough like  or   is not implemented, nor is overlapping like. This means struck characters like ƛ are difficult to type, except the hardcoded. A workaround suffix for a normal strikethrough is, and for elevated strikethrough is  , which give



Formatting in \text
Formatting in \text is not supported. In other words, you can't use:

but have to use:

More specifically, in Mathoid's MathJax, no processing is done to the contents of \text at all. The texvcjs component blocks the use of macros, but another way this behavior leaks through is in the processing of quotation marks, where the Unicode version must be used instead of `:



It is currently impossible to get straight (typewriter) quotes in MathJax.

Automatic line-breaking
The current image-based implementation precludes automatic line-breaking of inline formulae after binary operators and "=" as seen in TeX. The only workarounds are to not write long formulae inline, or to split the formula into separate parts at each acceptable break point.

Readers wishing to enable automatic line-breaking can try to have the browser render the MathML itself or to use an alternate in-browser renderer.

Unicode characters
Non-ASCII Unicode characters like $\pi$ work in MathML, but not in Mathoid (server-side MathJax in SVG/PNG mode, after validation by texvcjs). They should be avoided for maximum compatibility.

MathJax itself supports Unicode characters in, but the texvcjs validator insists on blocking them (replicating old texvc behavior). A formal feature request and discussion is required to fix this.

Deprecated syntax
The texvc processor accepted some non-standard control sequences. These are now deprecated for Wikipedia use because the MathJax-based renderers do not support them. This is part of an effort to update the math engine. See mw:Extension:Math/Roadmap for details. A bot User:Texvc2LaTeXBot will replace this syntax on the English Wikipedia.

Chemistry
There are three ways to render chemical sum formulas as used in chemical equations:
 * p (p is a deprecated alias for it)
 * p
 * and

p is short for (where  is a chemical sum formula)

Technically, math is a  tag with the extension   enabled, according to the MathJax documentation.

Manual of Style/Chemistry advises avoiding the chem and math markup methods when possible.

Note, that the commands  and   are disabled, because they are marked as deprecated in the mhchem LaTeX package documentation.

If the formula reaches a certain "complexity", spaces might be ignored ( might be rendered as if it were   with a positive charge). In that case, write  (and not   as was previously suggested). This will allow auto-cleaning of formulas once the bug is fixed and/or a newer version of   is available.

Please note that there are still major issues with mhchem support in MediaWiki. Some issues can be solved by enabling the extension using math and formatting individual items with. For example,

States
Subscripting states is not an IUPAC recommendation.

Further examples using ordinary LaTeX tags
To align the equations or color them, use math chem and \ce.

1 step method
$$ \begin{array}{lcl} & X & \overset{f}\rightarrow & Z & \\ & g \downarrow && \downarrow g'\\ & Y & \underset{f'}\rightarrow & W & \\ \end{array} $$

3 step method
To make a commutative diagram, there are three steps:


 * 1) write the diagram in TeX
 * 2) convert to SVG
 * 3) upload the file to Wikimedia Commons

Diagrams in TeX
Xy-pic (online manual) is the most powerful and general-purpose diagram package in TeX. Diagrams created using it can be found at Commons: Category:Xy-pic diagrams.

Simpler packages include:


 * AMS's amscd
 * Paul Taylor's diagrams
 * François Borceux Diagrams

The following is a template for Xy-pic:

Using postscript drivers may in some cases give smoother curves and will handle fonts differently:

Convert to SVG
Once you have produced your diagram in LaTeX (or TeX), you can convert it to an SVG file using the following sequence of commands:

The pdfcrop and pdf2svg utilities are needed for this procedure. You can alternatively use pdf2svg from PDFTron for the last step.

If you do not have pdfTeX (which is unlikely) you can use the following commands to replace the first step (TeX → PDF):

In general, you will not be able to get anywhere with diagrams without TeX and Ghostscript, and the  program is a useful tool for creating or modifying your diagrams by hand. There is also a utility  which supports direct conversion from Postscript files to many vector graphics formats, but it requires a non-free plugin to convert to SVG, and regardless of the format, this editor has not been successful in using it to convert diagrams with diagonal arrows from TeX-created files.

These programs are:


 * 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, to Commons.)


 * 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  option to.
 * 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.

Now go to the image page and add a description, including the source code, using this template:


 * Source code
 * Include the source code in the image page, in the Source section of the  template, so that the diagram can be edited in future.
 * Include the complete  file, not just the fragment, so future editors do not need to reconstruct a compilable file.
 * You may optionally make the source code section collapsible, using the  or   templates.
 * (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 ; some use , 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. (The  is necessary if you use nest templates within the description, and harmless otherwise.)


 * Category
 * Include, 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

Examples
A sample conforming diagram is.

Semantics and links
While links from formulas using LaTeX macros such as \href or \url or are currently not supported, one can link individual math expressions to Wikidata items to explain the meaning of individual terms of mathematical expressions. For example,

links to a special page that displays additional information on that formula. To change the information shown on the special page, navigate to the Wikidata item linked at the bottom of the special page. Use the has part property to link parts of the equation to other Wikidata items with their respective Wikipedia articles. This is not limited to individual identifiers, but can also be used to link more complex terms.

A condensed version of that special page might be shown in the future as a popup: T239357.

Multiple equations
The altered newline code  below adds a vertical space between the two lines of length equal to $$0.6$$ times the height of a single ' ' character.