# Tutorial: How to use TeX/MathJax to render math notation?

$$\mathrm{\TeX}$$ is a markup language that allows us to insert content like equations and tables. MathJax is the add-in that StackExchange uses to render $$\mathrm{\TeX}$$ markup into $$\mathrm{\TeX}$$ graphics.

Using $$\mathrm{\TeX}$$ is simple:

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$$.

8. Fractions There are two 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}$$.

9. Fonts

• Use \mathbb or \Bbb for "blackboard bold": $$\mathbb{CHNQRZ}$$.
• Use \mathbf for boldface: $$\mathbf{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$$ $$\mathbf{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}$$

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 \ge \neq $$\lt\, \gt\, \le\, \ge\, \neq$$. You can use \not to put a slash through almost anything: \not\lt $$\not\lt$$ but it often looks bad (also see two lines down).
• \lessapprox\gtrapprox\lesssim\gtrsim
• \lnapprox\gnapprox\lnsim\gnsim
• \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 $$\approx\, \sim \, \simeq\, \cong\, \equiv\, \prec, \lhd$$.
• \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 or the list on Michael Downes "Math Symbol and Math Fonts" website.

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 reduce the space between a series of MathJax commands and a following word (doesn't work well with punctuation) use \! as many times as necessary.

To set plain text, use \text{…}: $$\{x\in s\mid x\text{ is extra large}\}$$. You can nest $$…$$ inside of \text{…}.

1. 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.

Accents from "Math symbols and math fonts" by Michael Downes:

\acute{x} $$\acute{x}$$ \bar{x} $$\bar{x}$$ \vec{x} $$\vec{x}$$ \widetilde{xxx} $$\widetilde{xxx}$$ \grave{x} $$\grave{x}$$ \breve{x} $$\breve{x}$$

\dot{x} $$\dot{x}$$ \widehat{xxx} $$\widehat{xxx}$$ \ddot{x} $$\ddot{x}$$ \check{x} $$\check{x}$$ \ddot{x} $$\ddot{x}$$ \tilde{x} $$\tilde{x}$$

\hat{x} $$\hat{x}$$ \dddot{x} $$\dddot{x}$$

1. 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.

Additional examples from math.stackexchange.com showing how to draw commutative diagrams using \array or \newcommand:




$$\begin{array}{ccccccccc} 0 & \xrightarrow{i} & A & \xrightarrow{f} & B & \xrightarrow{q} & C & \xrightarrow{d} & 0 \\ \downarrow & \searrow & \downarrow & \nearrow & \downarrow & \searrow & \downarrow & \nearrow & \downarrow \\ 0 & \xrightarrow{j} & D & \xrightarrow{g} & E & \xrightarrow{r} & F & \xrightarrow{e} & 0 \end{array}$$


$$\begin{array}{ccccccccc} 0 & \xrightarrow{i} & A & \xrightarrow{f} & B & \xrightarrow{q} & C & \xrightarrow{d} & 0 \\ \downarrow & \searrow & \downarrow & \nearrow & \downarrow & \searrow & \downarrow & \nearrow & \downarrow \\ 0 & \xrightarrow{j} & D & \xrightarrow{g} & E & \xrightarrow{r} & F & \xrightarrow{e} & 0 \end{array}$$

The word on Tables: Is there Markdown to create tables? - ASCII only, real Tables were denied.

Our whitelisted subset of HTML: What HTML tags are allowed on Stack Exchange sites?

Michael Downes of the American Mathematical Society has a useful MathJax website.

Another plethora of tips are offered on the LaTeX - Mathematics: Fractions and Binomials Wiki.

Hacking the quod erat demonstrandum symbol is discussed here: "\qed for MathJax here on stackexchange" - Spoiler: $$\tag*{\blacksquare}$$

$$\tag*{\blacksquare}$$

Note: It is important that this post 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.

# Typesetting chemistry in MathJax

The LaTeX package mhchem is not activated here, by default. However, you can load it yourself, by using the command \require{\mhchem} (you need to use it only once on a page).

Example: \require{\mhchem}\ce{H2O} generates $$\require{\mhchem}\ce{H2O}$$.

Courtesy: @DavidCervone on Physics SE

Chemistry Stack Exchange has a community wiki by @ManishEarth titled How can I format math/chemistry expressions here? which documents the MathJax commands for chemistry. Since they are free to alter/delete their copy without consulting us, let's have our own, using their template. Constructive edits below this line most welcome.

## Getting started

On chemistry.SE, we use MathJax to format mathematical as well as chemical equations and similar expressions in questions, answers, and comments. MathJax allows us to typeset expressions using $$\LaTeX$$ notation.

To use MathJax, enclose your math within single ($$...$$) or double ($$...$$) dollar signs. Single dollar signs make the math inline, for example, Let $$x$$ be a variable gives:

Let $$x$$ be a variable.

On the other hand, double dollar signs make the math a block element. It gets its own line, and is slightly larger. For example, The equation of motion is as follows: $$v=u+at$$ It is a SUVAT equation gives:

The equation of motion is as follows: $$v=u+at$$ It is a SUVAT equation

Note that, in math mode, MathJax ignores the spaces you type, e.g. $$a b$$ yields $$a b$$. MathJax formats expressions the way it is common in mathematics texts. However, the printing rules for signs and symbols used in the natural sciences and technology may require additional spaces (in particular, between the numerical value and the unit symbol). In math mode, use \ (backslash space) or ~ (tilde) if you want the equivalent of space in normal text. Where separation of numbers into groups of three digits is used, the groups shall be separated by a thin space \, (backslash comma); e.g. $$299\,792\,458$$ yields $$299\,792\,458$$.

## Basic chem

We use the mhchem package for chemistry. It lets us easily format chemical formulas and reactions without typing too much.

There really is only one command you need to know here: \ce{...}. \ce{...} takes its parameters and automatically formats it. For example,

$$\ce{HCl}$$ dissociates in water as follows:
$$\ce{H2O + HCl <=> H3O+ + Cl-}$$


Renders as

$$\ce{HCl}$$ dissociates in water as follows: $$\ce{H2O + HCl <=> H3O+ + Cl-}$$

Note that spaces are very important for mhchem to separate super/subscripts from normal text. \ce{H3O+} will display $$\ce{H3O+}$$, but \ce{H2O +} will display $$\ce{H2O +}$$. When typesetting ions with more than a single charge, the argument has to be raised using the caret (^; also known as the circumflex accent) character, e.g. $$\ce{Cu^2+}$$ renders as $$\ce{Cu^2+}$$, while $$\ce{Cu2+}$$ would incorrectly render to $$\ce{Cu2+}$$. (Also see basic math below.)

Various types of reaction arrows are supported, including ->, <=>, <==>>, etc.

It also supports various types of bonds, via the \bond{..} command (to be called inside \ce{...}). You need not call \bond for normal bonds.

Eg: \ce{H\bond{->}A-B=C#D\bond{~}E\bond{~-}F\bond{...}G\bond{<-}E} displays:

$$\ce{H\bond{->}A-B=C#D\bond{~}E\bond{~-}F\bond{...}G\bond{<-}E}$$

Full documentation of mhchem here

## Basic math

### Superscripts and subscripts

You can denote superscripts via the ^ character, and subscripts via _. For example, x^2 renders as $$x^2$$, x_1 renders as $$x_1$$, and x_1^3 renders as $$x_1^3$$.

If you want to include more than one character in the super/sub script, enclose it in curly braces ({...}).

For example, x^10 renders as $$x^10$$, but x^{10} renders as $$x^{10}$$

### Fractions and square roots

Fractions can be easily displayed using \frac{..}{..}. For example, $$\frac{a+b^c}{de+f}$$ renders as

$$\frac{a+b^c}{de+f}$$

However, where it is necessary to include fractions in the body text, they shall, where possible, be reduced to a single level by using a solidus (/) or, where applicable, the negative index. For example, instead of $$\frac{1}{\sqrt 2}$$ write $$1/\sqrt 2$$ or $$2^{-1/2}$$; instead of $$C_{\mathrm m,p} = 33.58\ \mathrm{\frac{J}{K \cdot mol}}$$ write $$C_{\mathrm m,p} = 33.58\ \mathrm{J/(K \cdot mol)}$$ or $$C_{\mathrm m,p} = 33.58\ \mathrm{J\cdot K^{-1} \cdot mol^{-1}}$$.

Protip: You can exclude the braces for single-character numerators/denominators (if the first character is a letter, you need to use a space after \frac, though). For example \frac12 renders as $$\frac12$$, and \frac ab renders as $$\frac ab$$.

Square roots can be added in a similar manner, via \sqrt{....}. For example, \sqrt{x+y} renders as $$\sqrt{x+y}$$.

### Greek letters

Greek letters can be added using a backslash ('\'), followed by the name of the letter. Captialise the first letter of the name for greek capital letters.

Eg \alpha \beta \gamma \Omega renders as $$\alpha \beta \gamma \Omega$$.

Make sure that you put spaces after these if you are typing normal alphabet characters. Eg e^{\pii} gives an error, you need to use e^{\pi i}.

Note that there are special commands \varepsilon , \varsigma , \varrho , and \varpi to distinguish between the lunate Greek letters.

## MathJax extensions

MathJax has available a variety of extensions enabling other features: strikeout lines, enclosures, text/background coloration, interactive equations, etc. Information about these extensions and how to enable them can be found at this meta post.

If you would like to know more, you can continue reading about which symbols are written in italic (sloping) type and which are printed in roman (upright) type.

Note: As of April 30 2017 the CDN hosting at cdn.mathjax.org was shut down, as a result directories such as https://github.com/mathjax/MathJax-third-party-extensions/tree/master/mhchem (like the one appearing in the original of the text below) will 404. According to Issue #39 "[IMPORTANT] MathJax CDN end-of-life & suggested changes to third-party extensions #39" all 3rd party extensions will be hosted in separate repositories (one per extensions). See link to Issue #39 for a partial list of the new locations.

MathJax/mhchem Extension 3.3 was moved to: https://github.com/mhchem/MathJax-mhchem. The full Mathjax/mhchem is now located at: https://mhchem.github.io/MathJax-mhchem/.

# Please be careful when using TeX/MathJax in titles!

This is more on how not to use MathJax, but still good to keep in mind.

A good overview of why not to use do this is given here, but let me briefly summarize:

1. Titles are visible in places that do not render TeX!
2. If you must describe your title with TeX, you probably aren't summarizing your question enough. Try to make a natural language title, that is usually possible and summarizes your question better.
3. If you insist, then by all means, use TeX in the title. But there's always room for improvement. IMO, TeX in title is never necessary and a sign that the title can be improved!