Help:Displaying a formula: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 2:
{{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 [[VisualEditor]]. The visual editor shows a button that allows to choose one of three offered modes to display a formula.]]
 
There are three methods for displaying formulas in Wikipedia: raw [[HTML]], HTML with [[template:math templates|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 [[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 [[WP:CONSENSUS|consensus]] resulting of the practice of most experienced members of [[WP:WikiProject Mathematics|WikiProject Mathematics]] and many discussions at [[Wikipedia talk:WikiProject Mathematics]].
 
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 manual of style [[MOS:MATH]]. 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 [[WP: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 [[WP: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 [[MOS:BBB]])
* Avoid formulas in section headings, and when this is a problem, use raw HTML (see [[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 [[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 [[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==
{{Shortcut|WP:MATHSYMBOL}}
{{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 [[Help:Special characters]].
 
{| class="wikitable"
|-
! TeX syntax
! TeX rendering
|rowspan=7|&nbsp;
! HTML syntax
! HTML rendering
|-
| <syntaxhighlight lang="latex" inline><math>\alpha</math></syntaxhighlight>
| <math>\alpha</math>
| <syntaxhighlight lang="html5" inline>{{math|''&alpha;''}}</syntaxhighlight> &emsp; or &emsp; <syntaxhighlight lang="html5" inline>{{mvar|&alpha;}}</syntaxhighlight>
| {{math|''&alpha;''}} &emsp; or &emsp; {{mvar|&alpha;}}
|-
| <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'', ''&pi;''}}}}</syntaxhighlight>
| {{math|{{mset|1, ''e'', ''&pi;''}}}}
|-
| <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}} &le; 2}}</syntaxhighlight>
| {{math|{{abs|''z'' + 1}} &le; 2}}
|}
 
== Fractions ==