Help:Displaying a formula: Difference between revisions

Content deleted Content added
No edit summary
Tag: 2017 source edit
 
(19 intermediate revisions by the same user not shown)
Line 1:
{{TOCright}}{{Wikipedia how-to}}{{Commons Import}}
{{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 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.
[[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==
Line 34 ⟶ 38:
* 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.
Line 40 ⟶ 44:
=== 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].
Line 51 ⟶ 55:
==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"
Line 153 ⟶ 157:
 
Fractions can also be made with the TeX, but they do not match the Mediawiki typeface.
 
== Functions ==
 
Functions are rendered in EMEA Support Wiki's by TeX using Mathoid. The text here is from the following sources:
* [[mediawikiwiki:Help:Displaying a formula |Help:Displaying a formula (Meta-Wiki)]]
* [[wikisource:Help:Fractions_and_functions |Help:Fractions_and_functions (Wiki-Source)]]
* [[wikipedia:Help:Displaying_a_formula |Help:Displaying_a_formula (Wikipedia) ]]
* [https://web.archive.org/web/20160305074303/https://www.math.upenn.edu/tex-stuff/cookbook.pdf {{TeX}} Cookbook], [[Media:Tex-cookbook.pdf | Local Copy]]
 
==General==
Line 200 ⟶ 196:
<math>\text {þ}</math>
 
For producing special characters without math tags, see [[mmediawikiwiki:Help:Special characters | Special characters]].
 
Comparison:
Line 1,048 ⟶ 1,044:
 
== Alphabets and typefaces == <!--T:65-->
[[wwikipedia:Texvc|Texvc]] cannot render arbitrary [[wwikipedia:Unicode|Unicode]] characters. Those it can handle can be entered by the expressions below.
For others, such as [[wwikipedia:Cyrillic|Cyrillic]], they can be entered as Unicode or HTML entities in running text, but cannot be used in displayed formulas.
 
<!--T:66-->
Line 1,310 ⟶ 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.
*<code>{\color{Blue}x^2}+{\color{YellowOrange}2x}-{\color{OliveGreen}1}</code>
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.
*:<math>{\color{Blue}x^2}+{\color{YellowOrange}2x}-{\color{OliveGreen}1}</math>
 
{| 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
*<code>x_{1,2}=\frac{-b\pm\sqrt{\color{Red}b^2-4ac}}{2a}</code>
*:<math>x_{1,2}=\frac{-b\pm\sqrt{\color{Red}b^2-4ac}}{2a}</math>
 
{| class="wikitable"
 
It is also possible to change the background color (since [[rev:59550|r59550]]), as in the following example:
{| class=wikitable
|-
! How it looks rendered
! Background
! Syntax
! Wikicode
! Feature
! Rendering (in PNG)
|-
| <math>{\color{Blue}x^2}+{\color{Orange}2x}-{\color{LimeGreen}1}</math>
! rowspan=2 | White
| <syntaxhighlight lang="latex" inline>{\color{Blue}x^2}+{\color{Orange}2x}-{\color{LimeGreen}1}</syntaxhighlight>
| <code>e^{i \pi} + 1 = 0</code>
| works with both texvc and MathJax
| <math>e^{i \pi} + 1 = 0\,</math>
|-
| <math>\color{Blue}x^2\color{Black}+\color{Orange}2x\color{Black}-\color{LimeGreen}1</math>
| <code>'''\definecolor{orange}{RGB}{255,165,0}\pagecolor{orange}'''e^{i \pi} + 1 = 0</code>
| {{nowrap|1=<syntaxhighlight lang="latex" inline>\color{Blue}x^2\color{Black}+\color{Orange}2x\color{Black}-\color{LimeGreen}1</syntaxhighlight>}}
| <math>\definecolor{orange}{RGB}{255,165,0}\pagecolor{orange}e^{i \pi} + 1 = 0\,</math></span>
| works with both texvc and MathJax
|-
| <math>\color{Blue}{x^2}+\color{Orange}{2x}-\color{LimeGreen}{1}</math>
! rowspan=2 | Orange
| <syntaxhighlight lang="latex" inline>\color{Blue}{x^2}+\color{Orange}{2x}-\color{LimeGreen}{1}</syntaxhighlight>
| <code>e^{i \pi} + 1 = 0</code>
| only works with MathJax
| style="background-color:orange;" | <math>e^{i \pi} + 1 = 0\,</math>
|-}
 
| <code>'''\definecolor{orange}{RGB}{255,165,0}\pagecolor{orange}'''e^{i \pi} + 1 = 0</code>
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).
| style="background-color:orange;" | <math>\definecolor{orange}{RGB}{255,165,0}\pagecolor{orange}e^{i \pi} + 1 = 0\,</math>
 
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:
<!--T:73-->
<syntaxhighlight lang="latex">\definecolor{myorange}{rgb}{1,0.65,0.4}\color{myorange}e^{i \pi}\color{Black} + 1 = 0</syntaxhighlight>
See here for [http://oregonstate.edu/%7Epeterseb/tex/samples/docs/color-package-demo.pdf all named colors] supported by LaTeX.
 
:<math>\definecolor{myorange}{rgb}{1,0.65,0.4}\color{myorange}e^{i \pi}\color{Black} + 1 = 0</math>
<!--T:74-->
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 [[Wikipedia:Manual of Style#Color coding]].
 
== Formatting issues == <!--T:75-->
Line 1,419 ⟶ 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.
 
==Commutative diagrams==
===1 step method ===
<math>
\begin{array}{lcl}
& X & \overset{f}\rightarrow & Z & \\
& g \downarrow && \downarrow g'\\
& Y & \underset{f'}\rightarrow & W & \\
\end{array}
</math>
<syntaxhighlight lang="tex"><math>
\begin{array}{lcl}
& X & \overset{f}\rightarrow & Z & \\
& g \downarrow && \downarrow g'\\
& Y & \underset{f'}\rightarrow & W & \\
\end{array}
</math></syntaxhighlight>
 
===3 step method ===
[[File:First isomorphism theorem (plain).svg|thumb|A sample commutative diagram, created in the manner described]]
To make a [[commutative diagram]], there are three steps:
 
# write the diagram in [[TeX]]
# [[#Convert to SVG|convert to SVG]]
# [[commons:Commons:First steps/Upload form|upload the file]] to [[commons:|Wikimedia Commons]]
 
====Diagrams in TeX====
 
[http://www.tug.org/applications/Xy-pic/ Xy-pic]{{efn|Use the barr option for commutative diagrams, e.g., <code>\usepackage[cmtip,all,barr]{xy}</code>.}} ([http://tex.loria.fr/graph-pack/doc-xypic/xyguide-html/xyguide-html.html 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|Commons: Category:Xy-pic diagrams]].
 
Simpler packages include:
 
*[[American Mathematical Society|AMS's]] [http://www.dante.de/CTAN//help/Catalogue/entries/amscd.html amscd]
*Paul Taylor's [http://www.ctan.org/tex-archive/macros/generic/diagrams/taylor/ diagrams]
*François Borceux [http://www.ctan.org/tex-archive/help/Catalogue/entries/borceux.html Diagrams]
 
The following is a template for Xy-pic:
<syntaxhighlight lang=latex>
\documentclass[border=10pt]{standalone} % Crop to size, remove page numbers, leave margin
\usepackage[all]{xy} % Loading the XY-Pic package
\begin{document}
\SelectTips{eu}{} % Euler (shorter) arrowheads (tips)
$$
\xymatrix{
%%% Diagram goes here %%%
}
$$
\end{document}
</syntaxhighlight>
 
Using postscript drivers may in some cases give smoother curves and will handle fonts differently:
<syntaxhighlight lang=latex>\usepackage[all, ps, dvips]{xy}</syntaxhighlight>
 
====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:
 
<syntaxhighlight lang=bash>
pdflatex file.tex
pdf2svg file.pdf file.svg
</syntaxhighlight>
 
The [http://pdfcrop.sourceforge.net pdfcrop] and [http://www.cityinthesky.co.uk/opensource/pdf2svg pdf2svg] utilities are needed for this procedure. You can alternatively use [http://www.pdftron.com/pdf2svg/ 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):
<syntaxhighlight lang=bash>
latex file.tex
dvipdfm file.dvi
</syntaxhighlight>
 
In general, you will not be able to get anywhere with diagrams without TeX and Ghostscript, and the <syntaxhighlight lang="latex" inline>inkscape</syntaxhighlight> program is a useful tool for creating or modifying your diagrams by hand. There is also a utility <syntaxhighlight lang="latex" inline>pstoedit</syntaxhighlight> 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, [[User:Ryan Reich|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====
{{See also|commons:Commons:First steps/Upload form}}
{{See also|Help:Contents/Images and media}}
 
As the diagram is your own work, upload it to [[commons:|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#Support in applications|SVG application]] and viewing at default size (100% scaling), otherwise adjust the <code>-y</code> option to <code>dvips</code>.
; Name: Make sure the file has a [[WP:Naming conventions|meaningful name]].
; Upload: [[commons:Special:Userlogin|Login to Wikimedia Commons]], then <span class="plainlinks">[http://commons.wikimedia.org/w/index.php?title=Special:Upload&uselang=ownwork upload the file]</span>; for the '''Summary''', give a brief description.
 
Now go to the [[Help:Image page|image page]] and add a [[commons:Commons:First steps/Quality and description#Good file descriptions|description]], including the '''source code''', using this template:
 
<pre>
{{Information
|description =
{{en|1= '''Description [[:en:Link to WP page|topic]]'''}}
|source = {{own}}, created as per:
[[:en:Help:Displaying a formula#Commutative diagrams]];
source code below.
|date = '''The Creation Date, like 1999-12-31'''
|author = '''[[User:YourUserName|Your Real Name]]'''
|permission = {{self|PD-self '''(or [[commons:Licensing#Well-known licenses|other license]])'''
|author = '''[[User:YourUserName|Your Real Name]]'''}}
}}
 
====TeX source====
 
<syntaxhighlight lang="latex">
% TeX source here
</syntaxhighlight>
[[Category:Commutative diagrams]]
[[Category:Xy-pic diagrams]]
[[Category:Images with LaTeX source code]]
</pre>
 
; Source code
:*Include the source code in the [[Help:Image page|image page]], in the {{mono|Source}} section of the <code><nowiki>{{</nowiki>[[commons:Template:Information|Information]]}}</code> template, so that the diagram can be edited in future.
:*Include the complete <code>.tex</code> 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 <code><nowiki>{{</nowiki>[[commons:Template:cot|cot]]}}</code> or <code><nowiki>{{</nowiki>[[commons:Template:cob|cob]]}}</code> 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 <code>[[commons:Template:PD-self|PD-self]]</code>; some use <code>[[commons:Template:PD-self|PD-ineligible]]</code>, especially for simple diagrams, or other licenses. Please ''do not'' use the [http://www.gnu.org/copyleft/fdl.html 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 <code>1=</code> is necessary if you use nest templates within the description, and harmless otherwise.)
; Category
: Include <code><nowiki>[[Category:Commutative diagrams]]</nowiki></code>, 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 <code><nowiki>[[File:Diagram.svg]]</nowiki></code>
 
====Examples====
 
A sample conforming diagram is [[commons:File:PSU-PU.svg]].
 
==Chemistry==
Line 1,686 ⟶ 1,598:
<math chem><-></math></nowiki>|<math>\longleftrightarrow</math><br/><math chem><-></math>
}}
 
== Examples ==
 
=== Chemistry ===
<nowiki><math chem>C6H5-CHO</math></nowiki>
<math chem>C6H5-CHO</math>
 
<nowiki><math chem>{SO4^{2-}} + Ba^2+ -> BaSO4 v</math></nowiki>
<math chem>{SO4^{2-}} + Ba^2+ -> BaSO4 v</math>
 
<nowiki><math chem>H2O</math></nowiki>
<math chem>H2O</math>
 
<nowiki><math chem>Sb2O3</math></nowiki>
<math chem>Sb2O3</math>
 
<nowiki><math chem>H+</math></nowiki>
<math chem>H+</math>
 
<nowiki><math chem>CrO4^2-</math></nowiki>
<math chem>CrO4^2-</math>
 
<nowiki><math chem>AgCl2-</math></nowiki>
<math chem>AgCl2-</math>
 
<nowiki><math chem>[AgCl2]-</math></nowiki>
<math chem>[AgCl2]-</math>
 
<nowiki><math chem>Y^{99}+</math></nowiki>
<math chem>Y^{99}+</math>
 
<nowiki><math chem>Y^{99+}</math></nowiki>
<math chem>Y^{99+}</math>
 
<nowiki><math chem>H2_{(aq)}</math></nowiki>
<math chem>H2_{(aq)}</math>
 
<nowiki><math chem>NO3-</math></nowiki>
<math chem>NO3-</math>
 
<nowiki><math chem>(NH4)2S</math></nowiki>
<math chem>(NH4)2S</math>
 
==Examples of implemented TeX formulas==
Line 1,877 ⟶ 1,747:
\end{align}</math></syntaxhighlight>
}}
 
== References ==
{{Reflist}}