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.
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.50 for the first one, and $2.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.
Here is a complete sample page containing TeX mathematics (see the MathJax Web Demos Repository for more).
<!DOCTYPE html>
<html>
<head>
<title>MathJax TeX Test Page</title>
<script defer src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-chtml.js">
</script>
</head>
<body>
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}.$$
</body>
</html>
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.
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.
For mathematics written in MathML notation, you mark your mathematics using standard tags, where represents displayed mathematics and or just 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 tags.
Here is a complete sample page containing MathML mathematics:
<!DOCTYPE html>
<html>
<head>
<title>MathJax TeX Test Page</title>
<script defer src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-chtml.js">
</script>
</head>
<body>
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}.$$
</body>
</html>
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 in an HTML document.
MathJax v2.0 introduced a new input format, AsciiMath notation, by incorporating ASCIIMathML as one of its input processors. 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:
<!DOCTYPE html>
<html>
<head>
<title>MathJax AsciiMath Test Page</title>
<script>
MathJax = {
loader: {load: ["input/asciimath", "output/chtml", "ui/menu"]},
output: {font: "mathjax-newcm"}
}
</script>
<script defer src="https://cdn.jsdelivr.net/npm/mathjax@4/startup.js">
</script>
<body>
<p>When `a != 0`, there are two solutions to `ax^2 + bx + c = 0` and
they are</p>
<p style="text-align:center">
`x = (-b +- sqrt(b^2-4ac))/(2a) .`
</p>
</body>
</html>
This renders as shown below:
If your are using javascript to process mathematics, and need to put a TeX or LaTeX expression in a string literal, you need to be aware that javascript uses the backslash (\) as a special character in strings. Since TeX uses the backslash to indicate a macro name, you often need backslashes in your javascript strings. In order to achieve this, you must double all the backslashes that you want to have as part of your javascript string. For example,
const math = '\\frac{1}{\\sqrt{x^2 + 1}}';
This can be particularly confusing when you are using the LaTeX macro \\, as both backslashes must be doubled as \\\\. So you would do:
const array = '\\begin{array}{cc} a & b \\\\ c & d \\end{array}';
to produce an array with two rows.
It is also possible to use the String.raw constructor to create strings with backslashes that don't need to be doubled. For example,
const math = String.raw`\frac{1}{\sqrt{x^2 + 1}}`;
is equivalent to the first declaration above.