The goal of this post is to outline the absolute basics of how to typeset mathematics using MathJax, without any prior knowledge assumed.
Introduction: when asking and answering questions, it makes your writing much clearer if you typeset the mathematical notation using the markup language MathJax. Compare how the quadratic formula reads with and without it:
$$
\underbrace{x=\frac{-b\pm \sqrt{b^2-4ac}}{2a}}_{\text{With MathJax}} \quad \underbrace{\text{x=-b+-sqrt(b^2-4ac)/2a}}_{\text{Without MathJax}}
$$
This tutorial serves as a brief introduction to how MathJax works, and how you can use it. A more comprehensive overview of the subject can be found here.
Table of contents:
- Getting started
- Understanding MathJax syntax
- Numbers and arithmetic
- Functions
- Chaining syntax together
- Miscellaneous commands
1. Getting started: in the body of your post, you can typeset formulas by putting two dollar signs $$
before the formula, and after the formula:
Be careful not to leave an empty line between the formula and the dollar signs. In the above example, the formula is displayed on a new line. This is known as a 'block formula' or 'displayed formula'. It is possible to write out multiple lines of equations. Each time you want to go onto a new line, you have to indicate this by typing two back-slashes \\
:
2x + 7 = 15 \\
2x = 8 \\
x = 4
Notice that the final line does not require two back-slashes. If you enclose the above code using dollar signs, then it renders as
$$
2x + 7 = 15 \\
2x = 8 \\
x = 4
$$
It is also possible to display the formula on the same line as the text:
This is known as an 'inline formula'. Rather than enclosing the formula with two dollar signs, you should only use a single $
at the start and end of the formula. You can also use inline formulas in the title of your post (but not block formulas).
2. Understanding MathJax syntax: since MathJax is a markup language, there is a syntax that you have to follow. Simply enclosing x=-b+-sqrt(b^2-4ac)/2a
with dollar signs does not do the trick:
$$
x=-b+-sqrt(b^2-4ac)/2a
$$
Instead, there are a set of commands that have to followed in order to typeset the equations nicely. The rest of this post will focus on the most common ones; a more comprehensive list can be found in the main tutorial.
In general, if you want to see how a formula was rendered, then right-click on it, and press 'Show Math As', then 'TeX Commands':
If you're wondering how to type a specific symbol, then Detexify is the tool for you. It allows you to draw a symbol, and finds the closest matching command for it. (These commands are not guaranteed to work in MathJax, but they are likely to.)
3. Numbers and arithmetic: to write out equations such as $4+7=11$, you simply have to enclose them using with dollar signs (no special commands are needed). Easy-peasy! 4+7=11
renders as $4+7=11$.
For multiplication and division, there are a number of options:
- $5 \times 7 = 35$ is rendered using
5 \times 7 = 35
. If you want to indicate multiplication using a dot, as in $5 \cdot 7 = 35$, you can write 5 \cdot 7 = 35
.
- $12 \div 6 = 2$ is rendered using
12 \div 6 = 2
. It is more common to indicate division using fractions: $\frac{3x+7}{2}$ is produced by \frac{3x+7}{2}
. Notice how the \frac
command has to be accompanied by two sets of curly braces. The numerator goes in the first set of curly braces, and the denominator goes in the second set of curly braces. Finally, you can also indicate division simply by using a forward slash: 1/3
produces $1/3$.
For exponents, you have to use a superscript ^
. For instance, $2^4=16$ is rendered using 2^4=16
. If the exponent has more than one character, then you have to enclose it using curly braces. For instance, $2^{10}=1024$ is rendered using 2^{10}=1024
, not 2^10=1024
.
Square roots are typeset using the command \sqrt
: $\sqrt{64}$ is produced by \sqrt{64}
. Confusingly, cube roots can also be typeset using the \sqrt
command: $\sqrt[3]{64}$ is produced by \sqrt[3]{64}
. $n$-th roots can be handled similarly.
Inequalities such as $5>4$ can be written as 5>4
. If you need to use a 'greater than or equal to' sign, then use \geq
: $5 \geq 4$ is produced by 5 \geq 4$
. The 'less than or equal to' sign is \leq
. The 'not equal to' sign is \neq
.
4. Functions: to write out a function such as $\sin(x)$, you have to use a backlash: \sin(x)
. You can also omit the parentheses by using curly braces instead: \sin{x}
renders as $\sin{x}$. Alternatively, you can leave a space between the function and the argument: \sin x
also renders as $\sin x$. Other functions such as $\ln x$ and $\arctan x$ can be typeset in much the same way. Again, if you are unsure about what the command for a function is, then right-click on it, and press 'Show Math As', then 'TeX Commands'. You can often guess the MathJax command for a function: it is no surprise that \exp
renders as $\exp$.
To indicate the base of a logarithm, use an underscore _
and curly braces where needed. \log_{10}x
renders as $\log_{10}x$. In general, the subscript of an expression can be typeset with _
, and the superscript with ^
. For instance, \sin^2 x
produces $\sin^2{x}$ and x_1
produces $x_1$. You can even use both a _
and ^
in the same expression: x_1^2
produces $x_1^2$, although this can be written more cleanly using parentheses: (x_1)^2
produces $(x_1)^2$.
Finally, if you want to define a function yourself, then something like f(x)=2x+7
renders as $f(x)=2x+7$.
5. Chaining syntax together: more complicated-looking formulas in MathJax are often built from simpler ones. The formula
$$
\frac{1}{x}\sqrt{\frac{1+x}{1-x}}\ln(\frac{2x^2+2x+1}{2x^2-2x+1})
$$
looks unpleasant to typeset, but is actually simply composed of three simple expressions:
\frac{1}{x}
\sqrt{\frac{1+x}{1-x}}
\ln (\frac{2x^2+2x+1}{2x^2-2x+1})
Notice that the MathJax editor has no problem with putting a fraction inside of a square root, for example. One aesthetic issue with the above formula is the odd sizing of the parentheses in
$$
\ln(\frac{2x^2+2x+1}{2x^2-2x+1})
$$
This can be remedied by replacing (
with \left(
and )
with \right)
. If we change the code to
\ln\left(\frac{2x^2+2x+1}{2x^2-2x+1}\right)
it displays as
$$
\ln\left(\frac{2x^2+2x+1}{2x^2-2x+1}\right)
$$
which is more pleasant to look at, though not strictly necessary. If you want to include text in formulas, then use \text
. For instance,
\text{speed} = \frac{\text{distance}}{\text{time}}
renders as
$$
\text{speed} = \frac{\text{distance}}{\text{time}}
$$
6. Miscellaneous commands:
- Greek letters: simply type the name of the Greek letter.
\pi
renders as $\pi$. If you want uppercase, then begin the command with a capital letter: \Gamma
renders as $\Gamma$.
- For set membership, the commands
\mathbb
and \in
come in handy: $x \in \mathbb{R}$ is rendered by x \in \mathbb{R}
. The complete list of fonts can be found in the main tutorial.
- For logical implication,
\implies
and \iff
are useful:
$$
x = 5 \implies x^2 = 25 \\
x^2 = 25 \iff x = 5 \text{ or } x = -5
$$
is rendered by
x = 5 \implies x^2 = 25 \\
x^2 = 25 \iff x = 5 \text{ or } x = -5