Help:Displaying a formula: Difference between revisions
Content deleted Content added
Caleb Cooper (talk | contribs) |
Caleb Cooper (talk | contribs) No edit summary Tag: 2017 source edit |
||
| (34 intermediate revisions by the same user not shown) | |||
Line 1:
{{TOCright}}{{Wikipedia how-to}}{{Commons Import}}
[[File:Editing a formula using VisualEditor.png|thumb|This screenshot shows the formula {{math|1=''E'' = ''mc''{{smallsup|2}}}} being edited using [[wikipedia:VisualEditor|VisualEditor]]. The visual editor shows a button that allows to choose one of three offered modes to display a formula.]]
Math is rendered in EMEA Support Wiki's by TeX using Mathoid. There are three methods for displaying formulas in mediawiki: raw HTML, HTML with math templates (abbreviated here as {{tl|math}}), and a subset of LaTeX implemented with the HTML markup <syntaxhighlight lang="html5" inline><math></math></syntaxhighlight> (referred to as ''LaTeX'' in this article). Each method has some advantages and some disadvantages, which have evolved over the time with improvements of MediaWiki. The manual of style for math has not always evolved accordingly. So the how-to recommendations that appear below may differ from those of the manual of style.<ref>[[mediawikiwiki:Extension:Math/Help:Formula|Extension:Math/Help:Formula (Mediawiki-Wiki)]]</ref>
<ref>[[metawikimedia:Help:Displaying a formula |Help:Displaying a formula (Wiki-Source)]]</ref>
<ref>[[wikipedia:Help:Displaying_a_formula |Help:Displaying_a_formula (Wikipedia) ]]</ref>
<ref>[https://web.archive.org/web/20160305074303/https://www.math.upenn.edu/tex-stuff/cookbook.pdf {{TeX}} Cookbook]{{space}}[[Media:Tex-cookbook.pdf | local copy]]</ref>
For example, the famous Einstein formula can be entered in raw HTML as <syntaxhighlight lang="html5" inline>{{nowrap|''E'' {{=}} ''mc''<sup>2</sup>}}</syntaxhighlight>, which is rendered as {{nowrap|''E'' {{=}} ''mc''<sup>2</sup>}} (the template {{tl|nowrap}} is here only for avoiding a line break inside the formula). With {{tl|math}}, it can be entered as <syntaxhighlight lang="html5" inline>{{math|''E'' {{=}} ''mc''{{sup|2}}}}</syntaxhighlight>, which is rendered as {{math|''E'' {{=}} ''mc''{{sup|2}}}}. With LaTeX, it is entered as <syntaxhighlight lang="html5" inline><math>E=mc^2</math></syntaxhighlight>, and rendered as <math>E=mc^2</math>.
==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 {{tl|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 it should be avoided making other changes in the same edit. 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 {{tl|math}} is rather simple: when the formula is enclosed with {{tl|nowrap}}, it suffices to change "nowrap" into "math". However, if the formula contains an equal sign, one has to add 1= just before the formula for avoiding confusion with the template syntax; for example, <syntaxhighlight lang="html5" inline>{{math|1=''E'' = ''mc''{{sup|2}}}}</syntaxhighlight>. Also, vertical bars, if any, must either be replaced with {{tlx|!}} or avoided by using {{tlx|abs}}.
==LaTeX vs. <nowiki>{{math}}</nowiki>==
{{anchor|LaTeX vs. math template}}
These two ways of writing mathematical formulas each have their advantages and disadvantages. They are both accepted by the [[wikipedia:MOS:MATH|manual of style]]. The rendering of variable names is very similar. So having a variable name displayed in the same paragraph with {{tlx|math}} and <syntaxhighlight lang="html5" inline><math></syntaxhighlight> 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 is slightly different from that of the surrounding text. This is not a problem with a block displayed formula. This is generally also not a real problem with inline formulas that exceed the normal line height (for example formulas with subscripts and superscripts).
Also, the use of LaTeX in a [[wikipedia:piped link|piped link]] or in a section heading should appear in blue in the linked text or the table of content, but they do not. Moreover, links to section headings containing LaTeX formulas do not work always as expected.
Finally, too many LaTeX formulas may significantly increase the processing time of a page. LaTeX formulas should be avoided in image captions, because when the image is clicked for a larger display, LaTeX in the caption will not render.
The disadvantages of {{tl|math}} are the following:
not all formulas can be displayed. While it is possible to render a complicated formula with {{tl|math}}, it is often poorly rendered. Except for the most common ones, the rendering of non-alphanumeric Unicode symbols is often very poor and may depend on the browser configuration (misalignment, wrong size, ...). The spaces inside formulas are not managed automatically, and thus need some expertise for being rendered correctly. Except for short formulas, many more characters have to be typed for entering a formula, and the source is more difficult to read.
Therefore, the common practice of most members of [[wikipedia:WPM|WikiProject mathematics]] is the following:
* Use of {{tl|mvar}} and {{tl|math}} for isolated variables and very simple inline formulas
* Use of {{tl|mvar}} and {{tl|math}} for formulas in image captions, even if the rendering is mediocre
* Use of LaTeX for displayed formulas and more complicated inline formulas
* Use of LaTeX for formulas involving symbols that are not regularly rendered in Unicode (see [[wikipedia:MOS:BBB]])
* Avoid formulas in section headings, and when this is a problem, use raw HTML (see [[wikipedia:Finite field|Finite field]] for an example)
The choice between {{tl|math}} and LaTeX depends on the editor. So converting from a format to another one must be done with stronger reasons than editor preference.
=== Display format of LaTeX ===
{{see also|mw:Extension:Math}}
By default SVG images with non-visible [[wikipedia:MathML|MathML]] are generated. The text-only form of the LaTeX can be set via [[Help:Preferences|user preferences]] at [[Special:Preferences#mw-prefsection-rendering|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 [https://addons.mozilla.org/en-US/firefox/addon/native-mathml/ Native MathML] extension
* Or copy [https://github.com/fred-wang/webextension-native-mathml/blob/master/content-scripts/mediawiki.css its CSS rules] to your Wikipedia [[wikipedia:Help:User style|user stylesheet]].
In either case, you must have fonts that support MathML ([https://developer.mozilla.org/en-US/docs/Mozilla/MathML_Project/Fonts see developer.mozilla.org]) installed on your system. For copy-paste support in Firefox, you can also install [https://addons.mozilla.org/en-US/firefox/addon/mathml-copy/ MathML Copy].
==Use of HTML templates==
{{See also|Wikipedia:Rendering math}}
TeX markup is not the only way to render mathematical formulas. For simple inline formulas, the template {{tl|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 [[wikipedia:Help:Special characters]].
{| class="wikitable"
|-
! TeX syntax
! TeX rendering
|rowspan=7|
! HTML syntax
! HTML rendering
|-
| <syntaxhighlight lang="latex" inline><math>\alpha</math></syntaxhighlight>
| <math>\alpha</math>
| <syntaxhighlight lang="html5" inline>{{math|''α''}}</syntaxhighlight>   or   <syntaxhighlight lang="html5" inline>{{mvar|α}}</syntaxhighlight>
| {{math|''α''}}   or   {{mvar|α}}
|-
| <syntaxhighlight lang="latex" inline> <math>f(x) = x^2</math></syntaxhighlight>
| <math> f(x) = x^2</math>
| <syntaxhighlight lang="html5" inline>{{math|''f''(''x'') {{=}} ''x''<sup>2</sup>}}</syntaxhighlight>
| {{math|''f''(''x'') {{=}} ''x''<sup>2</sup>}}
|-
| <syntaxhighlight lang="latex" inline> <math>\{1,e,\pi\}</math></syntaxhighlight>
| <math>\{1,e,\pi\}</math>
| <syntaxhighlight lang="html5" inline>{{math|{{mset|1, ''e'', ''π''}}}}</syntaxhighlight>
| {{math|{{mset|1, ''e'', ''π''}}}}
|-
| <syntaxhighlight lang="latex" inline> <math>|z + 1| \leq 2</math></syntaxhighlight>
| <math>|z + 1| \leq 2</math>
| <syntaxhighlight lang="html5" inline>{{math|{{abs|''z'' + 1}} ≤ 2}}</syntaxhighlight>
| {{math|{{abs|''z'' + 1}} ≤ 2}}
|}
===HTML entities===
Though Unicode characters are generally preferred, sometimes HTML entities are needed to avoid problems with wiki syntax or confusion with other characters:
{{aligned table|cols=10|style=text-align:center;|row1class=unicode|row2class=mw-code
| < | > | − | • |′ | ″ | ⋅ | ·| – | —
| &lt; | &gt; | &minus; | &bull; | &prime; | &Prime; | &sdot; | &middot; | &ndash; | &mdash; }}
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 (<syntaxhighlight lang="html5" inline>''</syntaxhighlight>).
{| class="wikitable" border=1 <!--set border for MSIE browsers-->
|-
! HTML syntax
! Rendering
|- style="vertical-align:middle;"
|
<pre>&alpha; &beta; &gamma; &delta; &epsilon; &zeta;
&eta; &theta; &iota; &kappa; &lambda; &mu; &nu;
&xi; &omicron; &pi; &rho; &sigma; &sigmaf;
&tau; &upsilon; &phi; &chi; &psi; &omega;</pre>
|
α β γ δ ε ζ<br>η θ ι κ λ μ ν<br>ξ ο π ρ σ ς<br>τ υ φ χ ψ ω<br>
|- style="vertical-align:middle;"
|
<pre>&Gamma; &Delta; &Theta; &Lambda; &Xi; &Pi;
&Sigma; &Phi; &Psi; &Omega;</pre>
|
Γ Δ Θ Λ Ξ Π<br>Σ Φ Ψ Ω
|- style="vertical-align:middle;"
| <pre>&int; &sum; &prod; &minus; &plusmn; &infin;
&asymp; &prop; = &equiv; &ne; &le; &ge;
&times; &middot; &sdot; &divide; &part; &prime; &Prime;
&nabla; &permil; &deg; &there4; &empty;</pre>
|
∫ ∑ ∏ − ± ∞<br/>≈ ∝ = ≡ ≠ ≤ ≥<br/>× · ⋅ ÷ ∂ ′ ″<br/>∇ ‰ ° ∴ ∅
|- style="vertical-align:middle;"
| <pre>&isin; &notin; &cap; &cup; &sub; &sup; &sube; &supe;
&not; &and; &or; &exist; &forall;
&rArr; &hArr; &rarr; &harr; &uarr; &darr;
&alefsym; - &ndash; &mdash;</pre>
|
∈ ∉ ∩ ∪ ⊂ ⊃ ⊆ ⊇<br>¬ ∧ ∨ ∃ ∀<br/>⇒ ⇔ → ↔ ↑ ↓<br/>ℵ - – —
|}
===Superscripts and subscripts===
{{aligned table|cols=3|style=text-align:center;|row1class=unicode|row2class=mw-code|x<sup>2</sup> | x<sub>3</sub> | x{{su|p=2|b=1}} |<nowiki>x<sup>2</sup></nowiki>|<nowiki>x<sub>3</sub></nowiki>|<nowiki>x{{su|p=2|b=1}}</nowiki>}}
===Spacing===
To avoid line-wrapping in the middle of a formula, use {{tl|math}}. If necessary, a non-breaking space ( ) can be inserted with "&nbsp;".
Typically [[wikipedia:whitespace character|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 {{tl|thin space}}.
== Fractions ==
Fractions sometimes occur in regular text, and need proper presentation. Only a limited number are covered in the UTF-8 repertoire: they are ¼ ½ ¾ ⅓ ⅔ ⅕ ⅖ ⅗ ⅘ ⅙ ⅚ ⅛ ⅜ ⅝ ⅞
A number of these fractions have been added to the characters bar at the bottom of the edit page. The remainder have been left off since they do not render in the font used for Wikisource. They will appear here if your computer has Lucida Sans Unicode. They are overlarge compared with the regular font size.
Other fractions can be done with keyboard figures (like 11/16, 27/32, et cetera), but they are intrusive, being overlarge. More elegant fractions can be made by HTML coding, such as using the superscript/subscript markup.
Thus, <span style="vertical-align:super;font-size:x-small">11</span>/<span style="vertical-align:sub;font-size:x-small">16</span> will be displayed by <code><nowiki><span style="vertical-align:super;font-size:x-small">11</span>/<span style="vertical-align:sub;font-size:x-small">16</span></nowiki></code>
[[:Template:frac]] will produce {{frac|11|16}};
<nowiki>{{fs70|{{frac|11|16}}}}</nowiki> fits inline: {{fs70|{{frac|11|16}}}}
[[:Template:sfrac]] will produce vertical fractions fitting in with the Mediawiki typeface. Ex. {{sfrac|4|3|4}}, {{sfrac|67|83726}};
<nowiki>{{fs70|{{sfrac|4|3}}}}</nowiki> fits inline: {{fs70|{{sfrac|4|3}}}}
Fractions can also be made with the TeX, but they do not match the Mediawiki typeface.
==General==
Line 59 ⟶ 165:
===Size===
There are some possibilities, to change the size of the formulas. For example, fractions can be made smaller using <code>\tfrac</code> instead of <code>\frac</code>.
In general, formulas can be made even smaller if <code>\scriptstyle</code> is employed:
=== Special characters ===
{{markupv
|m=<math>\text {abcdefghijklmnopqrstuvwxyzàáâãäåæçčďèéěêëìíîïňñòóôõöřšť÷øùúůûüýÿž}</math>
<math>\text {abcdefghijklmnopqrstuvwxyzàáâãäåæçčèéêëìíîïñòóôõö÷øùúûüýÿ}\,</math>
|r=<nowiki><math>\text {abcdefghijklmnopqrstuvwxyzàáâãäåæçčďèéěêëìíîïňñòóôõöřšť÷øùúůûüýÿž}</math></nowiki>
<nowiki><math>\text {abcdefghijklmnopqrstuvwxyzàáâãäåæçčèéêëìíîïñòóôõö÷øùúûüýÿ}\,</math></nowiki>
}}
Or, using <code>\mbox</code> instead of <code>\text</code>, pretty much the same:
{{markupv
|m=<math>\mbox {abcdefghijklmnopqrstuvwxyzàáâãäåæçčďèéěêëìíîïňñòóôõöřšť÷øùúůûüýÿž}</math>
<math>\mbox {abcdefghijklmnopqrstuvwxyzàáâãäåæçčèéêëìíîïñòóôõö÷øùúûüýÿ}\,</math>
|r=<nowiki><math>\mbox {abcdefghijklmnopqrstuvwxyzàáâãäåæçčďèéěêëìíîïňñòóôõöřšť÷øùúůûüýÿž}</math></nowiki>
<nowiki><math>\mbox {abcdefghijklmnopqrstuvwxyzàáâãäåæçčèéêëìíîïñòóôõö÷øùúûüýÿ}\,</math></nowiki>
}}
Line 96 ⟶ 196:
<math>\text {þ}</math>
For producing special characters without math tags, see [[
Comparison:
Line 120 ⟶ 220:
<code><nowiki><math> \hat{} \quad \tilde{} \quad \backslash </math></nowiki></code> gives <math> \hat{} \quad \tilde{} \quad \backslash </math>
==Functions, symbols, special characters==
Line 1,060 ⟶ 1,044:
== Alphabets and typefaces == <!--T:65-->
[[
For others, such as [[
<!--T:66-->
Line 1,322 ⟶ 1,306:
|}
== Color ==
Equations can use color with the <code>\color</code> command. For example,
{| class="wikitable"
|-
! How it looks rendered
! Syntax
! Feature
|-
| <math>{\color{Blue}x^2}+{\color{Orange}2x}-{\color{LimeGreen}1}</math>
| <syntaxhighlight lang="latex" inline>{\color{Blue}x^2}+{\color{Orange}2x}-{\color{LimeGreen}1}</syntaxhighlight>
|
|-
| <math>x=\frac{{\color{Blue}-b}\pm\sqrt{\color{Red}b^2-4ac}}{\color{Green}2a}</math>
| {{nowrap|1=<syntaxhighlight lang="latex" inline>x=\frac{{\color{Blue}-b}\pm\sqrt{\color{Red}b^2-4ac}}{\color{Green}2a}</syntaxhighlight>}}
|
|}
The <code>\color</code> command colors all symbols to its right.
However, if the <code>\color</code> command is enclosed in a pair of braces (e.g. <code>{\color{Red}...}</code>) then no symbols outside of those braces are affected.
{| class="wikitable"
|-
! How it looks rendered
! Syntax
! Feature
|-
| <math>x\color{red}\neq y=z</math>
| <syntaxhighlight lang="latex" inline>x\color{red}\neq y=z</syntaxhighlight>
|
Colors <span style="color:red;">red</span> everything to the right of <code><nowiki>\color{red}</nowiki></code>. To only color the <math>\neq</math> symbol red, place braces around <code><nowiki>\color{red}\neq</nowiki></code> or insert <code><nowiki>\color{black}</nowiki></code> to the right of <code><nowiki>\neq</nowiki></code>.
|-
| <math>x{\color{red}\neq} y=z</math>
| <syntaxhighlight lang="latex" inline>x{\color{red}\neq} y=z</syntaxhighlight>
|
|-
| <math>x\color{red}\neq\color{black} y=z</math>
| <syntaxhighlight lang="latex" inline>x\color{red}\neq\color{black} y=z</syntaxhighlight>
|
|-
| <math>\frac{-b\color{Green}\pm\sqrt{b^2\color{Blue}-4{\color{Red}a}c}}{2a}=x</math>
| {{nowrap|1=<syntaxhighlight lang="latex" inline>\frac{-b\color{Green}\pm\sqrt{b^2\color{Blue}-4{\color{Red}a}c}}{2a}=x</syntaxhighlight>}}
| The outermost braces in <code>{\color{Red}a}c</code> limit the affect of <code>\color{Red}</code> to the symbol <code>a</code>. Similarly, <code>\color{Blue}</code> does not affect any symbols outside of the <code>\sqrt{}</code> that encloses it, and <code>\color{Green}</code> does not affect any symbols outside of the numerator.
|}
There are several alternate notations styles
{| class="wikitable"
|-
! How it looks rendered
! Syntax
! Feature
|-
| <math>{\color{Blue}x^2}+{\color{Orange}2x}-{\color{LimeGreen}1}</math>
| <syntaxhighlight lang="latex" inline>{\color{Blue}x^2}+{\color{Orange}2x}-{\color{LimeGreen}1}</syntaxhighlight>
| works with both texvc and MathJax
|-
| <math>\color{Blue}x^2\color{Black}+\color{Orange}2x\color{Black}-\color{LimeGreen}1</math>
| {{nowrap|1=<syntaxhighlight lang="latex" inline>\color{Blue}x^2\color{Black}+\color{Orange}2x\color{Black}-\color{LimeGreen}1</syntaxhighlight>}}
| works with both texvc and MathJax
|-
| <math>\color{Blue}{x^2}+\color{Orange}{2x}-\color{LimeGreen}{1}</math>
| <syntaxhighlight lang="latex" inline>\color{Blue}{x^2}+\color{Orange}{2x}-\color{LimeGreen}{1}</syntaxhighlight>
| only works with MathJax
|}
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.
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:
<pre style="display: inline-block;">
{| class="wikitable" align="center"
| style="background-color: gray;" | <math>x^2</math>
| style="background-color: Goldenrod;" | <math>y^3</math>
|}
</pre>
Rendered as:
{| class="wikitable" align="center"
| style="background-color: gray;" | <math>x^2</math>
| style="background-color: Goldenrod;" | <math>y^3</math>
|}
Custom colors can be defined using:
<syntaxhighlight lang="latex">\definecolor{myorange}{rgb}{1,0.65,0.4}\color{myorange}e^{i \pi}\color{Black} + 1 = 0</syntaxhighlight>
:<math>\definecolor{myorange}{rgb}{1,0.65,0.4}\color{myorange}e^{i \pi}\color{Black} + 1 = 0</math>
== Formatting issues == <!--T:75-->
Line 1,433 ⟶ 1,464:
<!--T:82-->
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.
==Chemistry==
Line 1,700 ⟶ 1,598:
<math chem><-></math></nowiki>|<math>\longleftrightarrow</math><br/><math chem><-></math>
}}
==Examples of implemented TeX formulas==
Line 1,892 ⟶ 1,748:
}}
==
{{Reflist}}
| |||