# MathJax basic tutorial and quick reference

1. To see how any formula was written in any question or answer, including this one, right-click on the expression it and choose "Show Math As > TeX Commands". (When you do this, the '$' will not display. Make sure you add these. See the next point.) 2. For inline formulas, enclose the formula in $...$. For displayed formulas, use $$...$$. These render differently. For example, type $\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$ to show $$\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$$ (which is inline mode) or type $$\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$$ to show $$\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$$ (which is display mode). 3. For Greek letters, use \alpha, \beta, …, \omega: $$\alpha, \beta, … \omega$$. For uppercase, use \Gamma, \Delta, …, \Omega: $$\Gamma, \Delta, …, \Omega$$. 4. For superscripts and subscripts, use ^ and _. For example, x_i^2: $$x_i^2$$, \log_2 x: $$\log_2 x$$. 5. Groups. Superscripts, subscripts, and other operations apply only to the next “group”. A “group” is either a single symbol, or any formula surrounded by curly braces {}. If you do 10^10, you will get a surprise: $$10^10$$. But 10^{10} gives what you probably wanted: $$10^{10}$$. Use curly braces to delimit a formula to which a superscript or subscript applies: x^5^6 is an error; {x^y}^z is $${x^y}^z$$, and x^{y^z} is $$x^{y^z}$$. Observe the difference between x_i^2 $$x_i^2$$ and x_{i^2} $$x_{i^2}$$. 6. Parentheses Ordinary symbols ()[] make parentheses and brackets $$(2+3)[4+4]$$. Use \{ and \} for curly braces $$\{\}$$. These do not scale with the formula in between, so if you write (\frac{\sqrt x}{y^3}) the parentheses will be too small: $$(\frac{\sqrt x}{y^3})$$. Using \left(\right) will make the sizes adjust automatically to the formula they enclose: \left(\frac{\sqrt x}{y^3}\right) is $$\left(\frac{\sqrt x}{y^3}\right)$$. \left and\right apply to all the following sorts of parentheses: ( and ) $$(x)$$, [ and ] $$[x]$$, \{ and \} $$\{ x \}$$, | $$|x|$$, \vert $$\vert x \vert$$, \Vert $$\Vert x \Vert$$, \langle and \rangle $$\langle x \rangle$$, \lceil and \rceil $$\lceil x \rceil$$, and \lfloor and \rfloor $$\lfloor x \rfloor$$. \middle can be used to add additional dividers. There are also invisible parentheses, denoted by .: \left.\frac12\right\rbrace is $$\left.\frac12\right\rbrace$$. If manual size adjustments are required: \Biggl(\biggl(\Bigl(\bigl((x)\bigr)\Bigr)\biggr)\Biggr) gives $$\Biggl(\biggl(\Bigl(\bigl((x)\bigr)\Bigr)\biggr)\Biggr)$$. 7. Sums and integrals \sum and \int; the subscript is the lower limit and the superscript is the upper limit, so for example \sum_1^n $$\sum_1^n$$. Don't forget {} if the limits are more than a single symbol. For example, \sum_{i=0}^\infty i^2 is $$\sum_{i=0}^\infty i^2$$. Similarly, \prod $$\prod$$, \int $$\int$$, \bigcup $$\bigcup$$, \bigcap $$\bigcap$$, \iint $$\iint$$, \iiint $$\iiint$$, \idotsint $$\idotsint$$. 8. Fractions There are three ways to make these. \frac ab applies to the next two groups, and produces $$\frac ab$$; for more complicated numerators and denominators use {}: \frac{a+1}{b+1} is $$\frac{a+1}{b+1}$$. If the numerator and denominator are complicated, you may prefer \over, which splits up the group that it is in: {a+1\over b+1} is $${a+1\over b+1}$$. Using \cfrac{a}{b} command is useful for continued fractions $$\cfrac{a}{b}$$, more details for which are given in this sub-article. 9. Fonts • Use \mathbb or \Bbb for "blackboard bold": $$\mathbb{CHNQRZ}$$. • Use \mathbf for boldface: $$\mathbf{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$$ $$\mathbf{abcdefghijklmnopqrstuvwxyz}$$. • Use \mathit for italics: $$\mathit{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$$ $$\mathit{abcdefghijklmnopqrstuvwxyz}$$. • Use \pmb for boldfaced italics: $$\pmb{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$$ $$\pmb{abcdefghijklmnopqrstuvwxyz}$$. • Use \mathtt for "typewriter" font: $$\mathtt{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$$ $$\mathtt{abcdefghijklmnopqrstuvwxyz}$$. • Use \mathrm for roman font: $$\mathrm{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$$ $$\mathrm{abcdefghijklmnopqrstuvwxyz}$$. • Use \mathsf for sans-serif font: $$\mathsf{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$$ $$\mathsf{abcdefghijklmnopqrstuvwxyz}$$. • Use \mathcal for "calligraphic" letters: $$\mathcal{ ABCDEFGHIJKLMNOPQRSTUVWXYZ}$$ • Use \mathscr for script letters: $$\mathscr{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$$ • Use \mathfrak for "Fraktur" (old German style) letters: $$\mathfrak{ABCDEFGHIJKLMNOPQRSTUVWXYZ} \mathfrak{abcdefghijklmnopqrstuvwxyz}$$. 10. Radical signs Use sqrt, which adjusts to the size of its argument: \sqrt{x^3} $$\sqrt{x^3}$$; \sqrt[3]{\frac xy} $$\sqrt[3]{\frac xy}$$. For complicated expressions, consider using {...}^{1/2} instead. 11. Some special functions such as "lim", "sin", "max", "ln", and so on are normally set in roman font instead of italic font. Use \lim, \sin, etc. to make these: \sin x $$\sin x$$, not sin x $$sin x$$. Use subscripts to attach a notation to \lim: \lim_{x\to 0} $$\lim_{x\to 0}$$ Nonstandard function names can be set with \operatorname{foo}(x) $$\operatorname{foo}(x)$$. 12. There are a very large number of special symbols and notations, too many to list here; see this shorter listing, or this exhaustive listing. Some of the most common include: • \lt \gt \le \leq \leqq \leqslant \ge \geq \geqq \geqslant \neq $$\lt\, \gt\, \le\, \leq\, \leqq\, \leqslant\, \ge\, \geq\, \geqq\, \geqslant\, \neq$$. You can use \not to put a slash through almost anything: \not\lt $$\not\lt$$ but it often looks bad. • \times \div \pm \mp $$\times\, \div\, \pm\, \mp$$. \cdot is a centered dot: $$x\cdot y$$ • \cup \cap \setminus \subset \subseteq \subsetneq \supset \in \notin \emptyset \varnothing $$\cup\, \cap\, \setminus\, \subset\, \subseteq \,\subsetneq \,\supset\, \in\, \notin\, \emptyset\, \varnothing$$ • {n+1 \choose 2k} or \binom{n+1}{2k} $${n+1 \choose 2k}$$ • \to \rightarrow \leftarrow \Rightarrow \Leftarrow \mapsto $$\to\, \rightarrow\, \leftarrow\, \Rightarrow\, \Leftarrow\, \mapsto$$ • \land \lor \lnot \forall \exists \top \bot \vdash \vDash $$\land\, \lor\, \lnot\, \forall\, \exists\, \top\, \bot\, \vdash\, \vDash$$ • \star \ast \oplus \circ \bullet $$\star\, \ast\, \oplus\, \circ\, \bullet$$ • \approx \sim \simeq \cong \equiv \prec \lhd \therefore $$\approx\, \sim \, \simeq\, \cong\, \equiv\, \prec\, \lhd\, \therefore$$ • \infty \aleph_0 $$\infty\, \aleph_0$$ \nabla \partial $$\nabla\, \partial$$ \Im \Re $$\Im\, \Re$$ • For modular equivalence, use \pmod like this: a\equiv b\pmod n $$a\equiv b\pmod n$$. • \ldots is the dots in $$a_1, a_2, \ldots ,a_n$$ \cdots is the dots in $$a_1+a_2+\cdots+a_n$$ • Some Greek letters have variant forms: \epsilon \varepsilon $$\epsilon\, \varepsilon$$, \phi \varphi $$\phi\, \varphi$$, and others. Script lowercase l is \ell $$\ell$$. Detexify lets you draw a symbol on a web page and then lists the $$\TeX$$ symbols that seem to resemble it. These are not guaranteed to work in MathJax but are a good place to start. To check that a command is supported, note that MathJax.org maintains a list of currently supported $$\LaTeX$$ commands, and one can also check Dr. Carol JVF Burns's page of $$\TeX$$ Commands Available in MathJax. 13. Spaces MathJax usually decides for itself how to space formulas, using a complex set of rules. Putting extra literal spaces into formulas will not change the amount of space MathJax puts in: a␣b and a␣␣␣␣b are both $$a b$$. To add more space, use \, for a thin space $$a\,b$$; \; for a wider space $$a\;b$$. \quad and \qquad are large spaces: $$a\quad b$$, $$a\qquad b$$. To set plain text, use \text{…}: $$\{x\in s\mid x\text{ is extra large}\}$$. You can nest $…$ inside of \text{…}, for example to access spaces. 14. Accents and diacritical marks Use \hat for a single symbol $$\hat x$$, \widehat for a larger formula $$\widehat{xy}$$. If you make it too wide, it will look silly. Similarly, there are \bar $$\bar x$$ and \overline $$\overline{xyz}$$, and \vec $$\vec x$$ and \overrightarrow $$\overrightarrow{xy}$$ and \overleftrightarrow $$\overleftrightarrow{xy}$$. For dots, as in $$\frac d{dx}x\dot x = \dot x^2 + x\ddot x$$, use \dot and \ddot. 15. Special characters used for MathJax interpreting can be escaped using the \ character: \$ $$\$$, \{ $$\{$$, \_ $$\_$$, etc. If you want \ itself, you should use \backslash $$\backslash$$, because \\ is for a new line.

(Tutorial ends here.)

It is important that this note be reasonably short and not suffer from too much bloat. To include more topics, please create short addenda and post them as answers instead of inserting them into this post.

## Contents

Alphabetical list of links to To MathJax Topics, by title:

• Some capital Greek letters are the same as the Roman equivalents, so they are not separated in $\LaTeX$. For a capital beta, one must use something like \mathrm{B}: $\mathrm{B}$ – robjohn Aug 28 '12 at 2:06
• Two related questions: How do I insert a table when asking a question? and How to show the integral symbol on this site? – Martin Sleziak Aug 28 '12 at 13:26
• A quick addition to point 11: If you want to use a $\sin$-like symbol that is not already defined, the command is \operatorname: e.g., \operatorname{Spec} A gives $\operatorname{Spec} A$. – Charles Staats Aug 28 '12 at 16:45
• It might be useful to mention hanging subscripts for things like _5C_3 $_5C_3$. You could also mention \frac vs \dfrac. – axblount Aug 29 '12 at 18:09
• My basic idea is that if a beginner can express a formula clearly, then someone else can come in and clean up the typesetting afterwards. I am considering getting rid of the section about \big, \left, and \right for this reason, and trimming the section on spacing. – MJD Aug 30 '12 at 2:06
• Most of the references to TeX or LaTeX in this and the answers ought to be to MathJaX (the exception that I can see being the output of Detexify). I know this is a bit pedantic, but would it be alright to correct this? – Andrew Stacey Sep 11 '12 at 14:13
• @AndrewStacey Thanks for pointing this out. Let's by all means be as correct as possible, particularly when there's no extra cost. – MJD Sep 11 '12 at 14:15
• @MJD Okay, I've had a go (also the answer about arrays). I wonder also whether or not it is worth a sentence at the end pointing out that whilst MathJaX does its best to emulate TeX, it isn't TeX and so while knowing how something is done in TeX gives you a starting point, it isn't a guarantee that the same thing works in MathJaX. (As a case in point, questions about MathJaX are generally off-topic over on TeX-SX.) – Andrew Stacey Sep 11 '12 at 14:22
• @AndrewStacey I wouldn't. They are close enough that it seems to me to be a needless refinement. I might even argue that MathJax is $\TeX$, although an alternative implementation. We're willing to accept that other programming languages (JavaScript, for example) that have slightly incompatible implementations are nevertheless the same language; why not in this case as well? – MJD Sep 11 '12 at 14:35
• @MJD Except that this is meant as a tutorial for those who aren't familiar with the distinction (and there really is a distinction: "slightly incompatible implementations" doesn't really fit the bill here). One thing tutorials often include is a "Where to find out more" section. This doesn't. Someone who doesn't know the distinction might be tempted to search for help on TeX or LaTeX instead and wonder why it doesn't work. – Andrew Stacey Sep 11 '12 at 14:40
• @AndrewStacey All the tips given here would work in any $\TeX$/$\LaTeX$ environment with the proper packages. MathJax is just the service used to render it. You wouldn't say "Miktex tutorial" or "texlive tutorial". – axblount Sep 11 '12 at 15:01
• @axblount But that's precisely the wrong way around to think about it! The likelihood is that someone will look at this tutorial to figure out how to write something on the Maths-SX site: i.e., to use MathJaX. If they can't find help here, where do they go? If they have the idea that MathJaX is "just a javascript implementation of TeX" then they might think to look for help with TeX, but that is quite possibly not going to be helpful. – Andrew Stacey Sep 11 '12 at 15:08
• @axblount For a start, you've changed the goalposts: "LaTeX math expressions". LaTeX is so much more than just a way of typesetting maths! Second, I don't really know but it wouldn't take me long to cook one up. I don't use MathJaX so I haven't explored it. But I know, for example, that it can't handle catcode changes. Which means that I can't make ( and ) automatically resizeable. I can in LaTeX. – Andrew Stacey Sep 11 '12 at 16:04
• I wish I saw this post when I first joined. This post should be a main link on the home page. There should be a button under each box: NEW TO LATEX, CLICK HERE FOR EXAMPLES. This is extremely useful, concise. – user1527227 May 31 '13 at 18:09
• @MJD: I use \mathrm in many places; e.g. $\mathrm{d}x$ in integrals and derivatives and for operator names that don't need the full force of \operatorname. \mathrm was intended for roman symbols in math mode; \text was intended for text because of the way it spaces things. See this TEX thread. Since I don't believe we can use preambles in MathJax, we can't use \DeclareMathOperator, though we can use \newcommand, but that is orthogonal to the use of \mathrm vs \text for math symbols. – robjohn Jun 10 '13 at 16:23

# Mixing code and MathJax formatting on lines

To give an example of how this might be useful, I wanted to express an algorithm in more or less the same indentation and symbolic way it appears in a paper.

On my desktop browsers (Chrome, Firefox) the following appears reasonably well spaced and indented, but loses indentation on my Android smartphone:

Input: positive integer $n$
Output: Tangent numbers $T_1,\ldots,T_n$
$T_1\gets 1$
for$k$ from $2$ to$n$
$T_k\gets (k−1)T_{k−1}$
for$k$ from $2$ to$n$
for$j$ from$k$ to$n$
$T_j\gets (j −k)T_{j−1} + (j −k+2)T_j$
return $\;T_1,T_2,\ldots,T_n$.

The source can be examined for specific techniques, but the basic trick is that a MathJax dollar-delimiter can follow a closing back-tick code delimiter, but an opening back-tick should be preceded by a space when following the (closing) dollar-sign delimiter.

Here is a version using \phantom rather than code monospacing to produce indents and tweaking the spacing between code and MathJax expressions with \;, so that the results appear clear on Android browsers:

Input: positive integer $n$
Output: Tangent numbers $T_1,\ldots,T_n$
$T_1\gets 1$
for $\;k\;$ from $2\;$ to $\;n$
$\phantom{{}++{}}$ $T_k\gets (k−1)T_{k−1}$
for $\;k\;$ from $2\;$ to $\;n$
$\phantom{{}++{}}$ for $\;j\;$ from $\;k\;$ to $\;n$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $T_j\gets (j −k)T_{j−1} + (j −k+2)T_j$
return $\;T_1,T_2,\ldots,T_n$.

• But this is why we have \space, \quad, and \qquad – Simply Beautiful Art Nov 7 '16 at 0:41
• While those are among the ways $\LaTeX$ provides control over spacing, they do not suffice for mixing code and MathJax formatting on a line. – hardmath Nov 7 '16 at 1:05
• I'm not sure if the topic of mixing other code in is well-suited here. – Simply Beautiful Art Nov 7 '16 at 2:05
• I would write the code in TeX using \texttt if I were you. Regardless, this answer probably does not belong here. – pzp May 21 '17 at 14:10
• @pzp: Thanks, that is an interesting suggestion. – hardmath May 22 '17 at 16:15
• @hardmath you can shorten the <code></code> spacers a bit by writing <codde/>, at least in my Jupyter notebooks in Chrome. – Reb.Cabin Feb 6 '18 at 22:28

# Markov Chains

This is a heuristic solution for coding Markov chains in Mathjax using a combination of commutative diagrams, the encircle tool and font sizes. There are a few minor issues with this method, for instance the arrows' ends should be attached closer to their targets. Also, it lacks double-headed diagonal arrows and it is difficult to attach probabilities to diagonal arrows. Therefore, it's mostly useful for small chains.

$$\require{enclose} \begin{array}{ccccccccc} \Large{\enclose{circle}{A}} & \xrightarrow{0.1} & \Large{\enclose{circle}{B}} & \xrightarrow{0.2} & \Large{\enclose{circle}{C}} & \xleftarrow{0.3} & \Large{\enclose{circle}{D}} & \xleftarrow{0.4} & \Large{\enclose{circle}{E}}\\\ \scriptsize{0.5}\large{\downarrow} & \scriptsize{0.6}\large{\searrow} & \scriptsize{0.7}\large{\downarrow} & \scriptsize{0.8}\large{\nearrow} & \scriptsize{0.9}\large{\downarrow} & \scriptsize{0.1}\large{\swarrow} & \scriptsize{0.2}\large{\downarrow} & \scriptsize{0.3}\large{\nwarrow} & \scriptsize{0.4}\large{\downarrow}\\\ \Large{\enclose{circle}{F}} & \xrightarrow[0.5]{} & \Large{\enclose{circle}{G}} & \xrightarrow[0.6]{} & \Large{\enclose{circle}{H}} & \xleftarrow[0.7]{} & \Large{\enclose{circle}{I}} & \xleftarrow[0.8]{} & \Large{\enclose{circle}{J}}\\ \circlearrowright\tfrac12\\ \end{array}$$

\require{enclose}
$$\begin{array}{ccccccccc} \Large{\enclose{circle}{A}} & \xrightarrow{0.1} & \Large{\enclose{circle}{B}} & \xrightarrow{0.2} & \Large{\enclose{circle}{C}} & \xleftarrow{0.3} & \Large{\enclose{circle}{D}} & \xleftarrow{0.4} & \Large{\enclose{circle}{E}}\\\ \scriptsize{0.5}\large{\downarrow} & \scriptsize{0.6}\large{\searrow} & \scriptsize{0.7}\large{\downarrow} & \scriptsize{0.8}\large{\nearrow} & \scriptsize{0.9}\large{\downarrow} & \scriptsize{0.1}\large{\swarrow} & \scriptsize{0.2}\large{\downarrow} & \scriptsize{0.3}\large{\nwarrow} & \scriptsize{0.4}\large{\downarrow}\\\ \Large{\enclose{circle}{F}} & \xrightarrow[0.5]{} & \Large{\enclose{circle}{G}} & \xrightarrow[0.6]{} & \Large{\enclose{circle}{H}} & \xleftarrow[0.7]{} & \Large{\enclose{circle}{I}} & \xleftarrow[0.8]{} & \Large{\enclose{circle}{J}}\\ \circlearrowright\tfrac12\\ \end{array}$$


Credit to Zev Chonoles for the commutative diagram.

## Tables

W/ Sub-Variables

After spending too much time searching for a way to make tables of this form (to no avail), I spent even longer searching for the pieces (of which most were found here) to Frankenstein my own. I made this table for a combinatorics q on MSE...

$$\begin{array}{l} \begin{array}{c|c} \hskip36.5pt & \hskip42.5pt\style{font-family:inherit}{\text{Ordering}} \end{array} \\[-7pt]\hline\hskip-5.5pt \begin{array}{c|c|c} \style{font-family:inherit}{\text{Repetition}} & \style{font-family:inherit}{\text{w/}} & \style{font-family:inherit}{\text{w/o}} \\\hline \style{font-family:inherit}{\text{w/}} & P_r^n=n^r & C_r^n=\left(\!\left(\begin{smallmatrix} n \\ r \end{smallmatrix}\right)\!\right)=\left(\begin{smallmatrix} n+r-1 \\ r \end{smallmatrix}\right) \\[0pt]\hline \style{font-family:inherit}{\text{w/o}} & nPr=\frac{n!}{(n-r)!} & nCr=\left(\begin{smallmatrix} n \\ r \end{smallmatrix}\right)=\frac{n!}{r!(n-r)!} \end{array}\hskip-5.5pt \end{array}$$

W/o Sub-Variables

While searching, I found several tables of this form...

$$\begin{array}{c|c|c|c} \style{font-family:inherit}{\text{Day}} & \style{font-family:inherit}{\text{Credit}} & \style{font-family:inherit}{\text{Debit}} & \style{font-family:inherit}{\text{Total}}\\\hline 0 & 0 & 0 & 10000 \\\hline 1 & 100 & 500 & 9600 \\\hline 2 & 0 & 400 & 10000 \\\hline 3 & 1000 & 500 & 10500 \end{array}$$

• I still consider myself a MathJax novice so please comment any code simplifications/corrections & I'll make the edit. – Landon Mar 20 '19 at 5:21
• – Martin Sleziak Mar 20 '19 at 5:35
• @MartinSleziak That was 1 of the questions I came across while searching for a way to make tables w/ sub-variables. – Landon Mar 20 '19 at 6:17

Is there a way to make the Greek letters displayed upright (non-italic)?

Asked in comment. There is a way using unicode characters, for which one can search here: http://unicode-table.com/en. Normal use of phi is $\phi = ...$, which gives $$\phi = ...$$.
Looking up phi on the above site gives a couple of results, if one uses 'Greek Capital Letter Phi' and copy it with 'Click to copy and paste symbol', and use it like
$Φ = ...$, the result is $$Φ = ...$$.
One might need to experiment which symbol(s) look(s) right.

Is there a tool to visually edit (prepare) the formulas with pre-defined symbols and paste here?