Writing Mathematics for MathJax — MathJax 4.0 documentation (original) (raw)
Putting mathematics in a web page
To put mathematics in your web page, you can use TeX and LaTeX notation, MathML notation,AsciiMath notation, or a combination of all three within the same page; the MathJax configuration tells MathJax which you want to use, and how you plan to indicate the mathematics when you are using TeX/LaTeX or AsciiMath notation. These three formats are described in more detail below.
TeX and LaTeX input
Mathematics that is written in TeX or LaTeX format is indicated using_math delimiters_ that surround the mathematics, telling MathJax what part of your page represents mathematics and what is normal text. There are two types of equations: ones that occur within a paragraph (in-line mathematics), and larger equations that appear separated from the rest of the text on lines by themselves (displayed mathematics).
The default math delimiters are $$...$$ and \[...\] for displayed mathematics, and \(...\) for in-line mathematics. Note in particular that TeX’s $...$ in-line delimiters are not used by default. That is because dollar signs appear too often in non-mathematical settings, which could cause some text to be treated as mathematics unexpectedly. For example, with single-dollar delimiters, “… the cost is 2.50forthefirstone,and2.50 for the first one, and 2.50forthefirstone,and2.00 for each additional one …” would cause the phrase “2.50 for the first one, and” to be typeset as mathematics, since it falls between dollar signs, and those dollar signs would be removed, as they would be treated as math delimiters. See the section on TeX and LaTeX Math Delimiters for more information on using dollar signs as delimiters.
Here is a complete sample page containing TeX mathematics (see theMathJax Web Demos Repositoryfor more).
MathJax TeX Test Page When \(a \ne 0\), there are two solutions to \(ax^2 + bx + c = 0\) and they are x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$This renders as shown below:
Since the TeX notation is part of the text of an HTML page, there are some caveats that you must keep in mind when you enter your mathematics. In particular, you need to be careful about the use of less-than signs and ampersands, since those are what the browser uses to indicate the start of a tag in HTML or a symbol entity name. Putting a space on both sides of these characters should be sufficient, but see TeX and LaTeX support for more details.
If you are using MathJax within a blog, wiki, or other content management system, the markup language used by that system may interfere with the TeX notation used by MathJax. For example, if your blog uses Markdown notation for authoring your pages, the underscores used by TeX to indicate subscripts may be confused with the use of underscores by Markdown to indicate italics, and the two uses may prevent your mathematics from being displayed. See TeX and LaTeX support for some suggestions about how to deal with the problem.
There are a number of extensions for the TeX input processor that are loaded by combined components that include the TeX input format (e.g.,tex-chtml.js), and others that are loaded automatically when needed. See TeX and LaTeX Extensions for details on TeX extensions that are available.
MathML input
For mathematics written in MathML notation, you mark your mathematics using standard <math> tags, where <math display="block"> represents displayed mathematics and <math display="inline"> or just <math> represents in-line mathematics.
MathML notation will work with MathJax in HTML files, not just XHTML files, even in older browsers, and the web page need not be served with any special MIME-type. Note, however, that in HTML (as opposed to XHTML), you should not include a namespace prefix for your <math>tags; for example, you should not use <m:math> except in an XHTML file where you have tied the m namespace to the MathML DTD by adding thexmlns:m="http://www.w3.org/1998/Math/MathML" attribute to your file’s<html> tag.
In order to make your MathML work in the widest range of situations, it is recommended that you include thexmlns="http://www.w3.org/1998/Math/MathML" attribute on all<math> tags in your document, although this is not strictly required, and this is preferred to the use of a namespace prefix likem: above, since those are deprecated in HTML5.
Here is a complete sample page containing MathML mathematics (see theMathJax Web Demos Repositoryfor more).
MathJax MathML Test PageWhen a≠0 , there are two solutions to ax2 + bx + c = 0 and they are x = − b ± b2 − 4ac 2a .
This renders as shown below:
When entering MathML notation in an HTML page (rather than an XHTML page), you should not use self-closing tags, as these are not part of HTML, but should use explicit open and close tags for all your math elements. For example, you should use
rather than <mspace width="5pt" /> in an HTML document. If you use the self-closing form, some browsers will not build the math tree properly, and MathJax will receive a damaged math structure, which will not be rendered as the original notation would have been. Typically, this will cause parts of your expression to not be displayed. MathJax does try to unravel the broken DOM tree, but this process is imperfect. Unfortunately, there is little MathJax can do about that, since the browser has incorrectly interpreted the tags long before MathJax has a chance to work with them.
See the MathML page for more on MathJax’s MathML support.
AsciiMath input
MathJax v2.0 introduced a new input format, AsciiMath notation, by incorporating ASCIIMathML as one of its input processors. This has not been fully ported to MathJax version 3 and above, but there is a version of it that uses the legacy v2 code to patch it into MathJax v3 and v4. None of the combined components currently include it, so you would need to specify it explicitly in your MathJax configuration in order to use it. See theAsciiMath page for more details.
By default, you mark mathematical expressions written in AsciiMath by surrounding them in “back-ticks”, i.e., `...`.
Here is a complete sample page containing AsciiMath notation:
MathJax AsciiMath Test PageWhen `a != 0`, there are two solutions to `ax^2 + bx + c = 0` and they are
`x = (-b +- sqrt(b^2-4ac))/(2a) .`
This renders as shown below:
Here we are loading the input and output components separately rather than using a combined configuration file. Since the output/chtmlcomponent does not have a font configured with it, we need to specify the font explicitly in the output section of the MathJax configuration. It is also possible to load a combined component liketex-chtml.js rather than startup.js and includeinput/asciimath in the load array of the loader block of the configuration, in which case the mathjax-newcm font will already be included, and won’t need to be specified separately.
See the AsciiMath support page for more on MathJax’s AsciiMath support and how to configure it.