103
$\begingroup$

On Chemistry Stack Exchange, MathJax is used to typeset mathematical equations. MathJax's syntax is very similar to LaTeX, so if you have used LaTeX before, almost everything you are used to will work.

Chemical equations can be typeset with the mhchem package for MathJax, which largely behaves identically to the mhchem LaTeX package.

A few introductory guides to MathJax and mhchem are collected in the answers to this question. They are not likely to be ordered properly by the Stack Exchange website, so a table of contents is provided here.

Table of contents

  1. Getting started

  2. Basic MathJax

  3. Basic mhchem

  4. Learning by example

  5. Further reading

$\endgroup$
  • $\begingroup$ For more advanced formatting please see: Hidden points of editing you probably didn't know $\endgroup$ – A.K. Apr 18 '19 at 18:30
  • 1
    $\begingroup$ @MaxW I'm just slowly making everything prettier, and inserting real-life examples for most of the text, and I gotta say that that's a lot of work you put into this. Thank you! $\endgroup$ – orthocresol 17 hours ago

10 Answers 10

103
$\begingroup$

(1) Getting started

There are three different types of markup (formatting commands) that we can differentiate between.


Markdown

Markdown is a simple markup language that is widely used on the Internet, and is built into all Stack Exchange websites. It allows you to (for example) write bold text, italicised text, and create hyperlinks.

When writing a question or answer, there are a number of buttons in the editing box that can help you with this. For more information, please see the editing help.

A limited subset of raw HTML tags are allowed on Stack Exchange, but these should generally not be required to write an answer.


MathJax

MathJax is a library which allows your web browser to display expressions written in $\LaTeX$ syntax.

Expressions that are enclosed within single ($...$) or double ($$...$$) dollar signs are interpreted using MathJax. Where possible, all mathematical expressions, except those in titles, should be typed using MathJax.

  • $...$ - A pair of single dollar signs specify an inline equation. This means you can use it seamlessly inside a sentence.

    Type this:

    Let $V$ denote the volume of a gas.
    

    to get this:

    Let $V$ denote the volume of a gas.

  • $$...$$ - A pair of double dollar signs specify a display equation. It gets its own line, is generally slightly larger, and is centred on the page.

    Type this:

    The ideal gas law is written as:
    $$ pV = nRT $$
    

    to get this:

    The ideal gas law is written as: $$ pV = nRT $$


mhchem

The mhchem package adds extra functionality for chemistry equations in MathJax. It lets us format professional looking chemical formulas and reactions without too much trouble. Chemical equations should always be typeset using mhchem, and not simply in plain MathJax.

The formatting command to invoke mhchem within MathJax is: \ce{...}. The \ce{} command must appear within a MathJax expression: that means it must be enclosed either with $...$ or $$...$$. Whatever is placed within the braces will be automatically passed to the mhchem package for rendering.

Type this:

The chemical formula of water is $\ce{H2O}$.

to get this:

The chemical formula of water is $\ce{H2O}$.

Note that the use of plain MathJax, i.e. $H_2O$, leads to italicised chemical symbols like $H_2O$ which are not correct.


(Back to index)

Next section: (2.1) Basic MathJax: Superscripts and subscripts

$\endgroup$
4
$\begingroup$

Learning by example

Here are some examples for quick copy and paste (copy the gray part to get the format shown at the beginning of the line. Feel free to append more if there are commands you use a lot fitting into a single line. Use $$ instead of $ to show items in a single line, centered, and often with better visibility.

$\ce{H2O}$ $\ce{H2O}$

$\ce{Ca^2+(aq) + 2 OH-(aq) <=> Ca(OH)2(s)}$ $\ce{Ca^2+(aq) + 2 OH-(aq) <=> Ca(OH)2(s)}$

$\ce{A ->[catalyst] B}$ $\ce{A ->[catalyst] B}$

$\pu{3.4E-8}$ $\pu{3.4E-8}$

$\pu{8.314 J K-1 mol-1}$ $\pu{8.314 J K-1 mol-1}$

$K_\mathrm{eq}$ $K_\mathrm{eq}$

$\mathrm{p}K_\mathrm{a}$ $\mathrm{p}K_\mathrm{a}$

$\Delta_\mathrm{r} G^\circ$ $\Delta_\mathrm{r} G^\circ$

$\displaystyle{E = E^\circ - \frac{R T}{z F} \ln{Q}}$ $\displaystyle{E = E^\circ - \frac{R T}{z F} \ln{Q}}$

$\displaystyle{\left(\frac{\frac{2}{3}}{\frac{6}{7}}\right)}$ $\displaystyle{\left(\frac{\frac{2}{3}}{\frac{6}{7}}\right)}$

$\ce{K\overset{+7}{Mn}O4}$ $\ce{K\overset{+7}{Mn}O4}$


$\left(\frac{\partial U}{\partial T}\right)_V = C_{V}$ $\left(\frac{\partial U}{\partial T}\right)_V = C_{V}$

$\displaystyle{\left(\frac{\partial U}{\partial T}\right)_V} = C_{V}$ $\displaystyle{\left(\frac{\partial U}{\partial T}\right)_V} = C_{V}$

$$\left(\frac{\partial U}{\partial T}\right)_V = C_{V}$$ $$\left(\frac{\partial U}{\partial T}\right)_V = C_{V}$$

[You can get a larger fraction with either \displaystyle{...} or $$...$$, the difference being that the former is left-justified, while the latter is centered.]


Back to INDEX

$\endgroup$
  • 1
    $\begingroup$ @andselisk I'm looking for a good place for this quick cheat sheet (I think it covers 90% of what I use), and I invite you to check whether they are correct and written in the best (shortest?) way possible. $\endgroup$ – Karsten Theis May 21 '20 at 16:48
  • $\begingroup$ Looks good, thank you for doing this! I have only a couple of minor corrections, one per comment below. $\endgroup$ – andselisk May 22 '20 at 7:51
  • $\begingroup$ It's better to leave a space between the coefficient and the formula, e.g. $\ce{2 OH-(aq)}$ instead of $\ce{2OH-(aq)}$. This assures better code semantics and saves a lot of hassle when one would switch to use another $\mathrm\LaTeX$ package, chemmacros for their desktop publishing, using the same code for equations. $\endgroup$ – andselisk May 22 '20 at 7:51
  • $\begingroup$ From ISO 80000-1:2009 Quantities and units: "If the point is used as the decimal sign, the cross and not the half-high dot should be used as the multiplication sign between numbers expressed with digits". This means $\pu{3.4e-8}$ ($\pu{3.4e-8}$) should be replaced with $\pu{3.4E-8}$ ($\pu{3.4E-8}$), whereas $\pu{3e-8}$ would be OK. $\endgroup$ – andselisk May 22 '20 at 7:51
  • $\begingroup$ It's better to use \displaystyle for inline math to adopt block geometry constrains and avoid \dfrac altogether for better compatibility. Also, my personal preference is to only use exponents with the symbols for physical quantities and never a solidus/slash or a fraction. $\pu{8.314 J K-1 mol-1}$ would work everywhere, whereas $8.314 {\frac{\pu{J}}{\pu{K mol}}}$ would always stick out and lead to wasted space when used inline, and $\pu{8.314 J/(K mol)}$ would look horrible when used, e.g. in a table header like such: $R/\pu{J/(K mol)}$. $\endgroup$ – andselisk May 22 '20 at 7:52
  • $\begingroup$ When oxidation number (O.N.) is displayed above the element symbol, Arabic numerals are used. Roman numerals are used to denote O.N. after the element name/symbol in braces or as a superscript after the element symbol. Examples: $\ce{\overset{+7}{Mn}}$ ($\ce{\overset{+7}{Mn}}$), but $\ce{Mn^{VII}}$ and $\ce{Mn(VII)}$ or manganese(VII). $\endgroup$ – andselisk May 22 '20 at 7:52
  • 1
    $\begingroup$ @andselisk OK, you just gave me some homework. I'll try to update those examples. $\endgroup$ – Karsten Theis May 22 '20 at 12:46
  • $\begingroup$ @KarstenTheis - I changed one equation and put all the markup in a table to reduce scrolling. The chemical equation with Ca was too long to fit into table without creating scroll bars so I changed it. If you want to revert back to your style feel free... Herb $\endgroup$ – MaxW May 23 '20 at 23:58
  • $\begingroup$ Bummers - I reverted to previous version. You can't swipe/copy markup in the table. :-( $\endgroup$ – MaxW May 24 '20 at 0:09
2
$\begingroup$

Basic mhchem

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

The formatting command to invoke mhchem with MathJax is: \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

Back to INDEX

$\endgroup$
2
$\begingroup$

How to make a table in LaTeX:

Easiest construction with an "array."

\begin{array}{|c|c|c|c|}\hline
\text{Alcolic beverage} & \text{Carboydrate content (%)} & \text{pH} \\ \hline
\text{Beer}           & 3.0-5.0         & 4.1-4.5 \\ \hline
\text{Wine}           & 0.0-12.9        & 2.8-3.8 \\ \hline
\text{Sweet liquor}   & 30.0-31.0       & 3.3-3.9 \\ \hline
\text{Strong alcohol} & 0.0-1.2         & 6.5-6.9 \\ \hline
\text{Vodka}          & \text{very few} & 6.0-7.0 \\ \hline
\text{Coca Cola}      & 10.6(?)         & 2.8     \\ \hline
\end{array}

\begin{array}{|c|c|c|c|}\hline \text{Alcolic beverage} & \text{Carboydrate content (%)} & \text{pH} \\ \hline \text{Beer} & 3.0-5.0 & 4.1-4.5 \\ \hline \text{Wine} & 0.0-12.9 & 2.8-3.8 \\ \hline \text{Sweet liquor} & 30.0-31.0 & 3.3-3.9 \\ \hline \text{Strong alcohol} & 0.0-1.2 & 6.5-6.9 \\ \hline \text{Vodka} & \text{very few} & 6.0-7.0 \\ \hline \text{Coca Cola} & 10.6(?) & 2.8 \\ \hline \end{array}

How to make the table with an array that looks like it wasn't done in math mode. It is considerably more code, but renders a cleaner looking rendering.

$$ \small
\begin{array}{lcc}
\hline
\text{Beverage} & \text{Carbohydrate Content (%)} & \text{pH} \\
\hline
\text{Beer} & 3.0-5.0 & 4.1-4.5 \\
\text{Wine} & 0.0-12.9 & 2.8-3.8 \\
\text{Sweet liquor } & 30.0-31.0 & 3.3-3.9 \\
\text{Strong alcohol} & 0.0-1.2 & 6.5-6.9 \\
\text{Vodka} & \text{very little} & 6.0-7.0 \\
\text{Coca Cola} & 10.6(?) & 2.8 \\
\hline
\end{array}
$$

$$ \small \begin{array}{lcc} \hline \text{Beverage} & \text{Carbohydrate Content (%)} & \text{pH} \\ \hline \text{Beer} & 3.0-5.0 & 4.1-4.5 \\ \text{Wine} & 0.0-12.9 & 2.8-3.8 \\ \text{Sweet liquor } & 30.0-31.0 & 3.3-3.9 \\ \text{Strong alcohol} & 0.0-1.2 & 6.5-6.9 \\ \text{Vodka} & \text{very little} & 6.0-7.0 \\ \text{Coca Cola} & 10.6(?) & 2.8 \\ \hline \end{array} $$

There is also a markdown syntax that works for tables, with less control over spacing:

| First  | Last   |
|--------|--------|
| Harry  | Potter |
| Mickey | Mouse  |
|Sherlock| Holmes |
First Last
Harry Potter
Mickey Mouse
Sherlock Holmes

Back to INDEX

$\endgroup$
2
$\begingroup$

(2.1) Basic MathJax: Superscripts and subscripts

You can denote superscripts using the ^ character, and subscripts using _.

Type this:

The heat capacity at constant volume is $C_V$.

Einstein's mass–energy relation is $E = mc^2$.

to get this:

The heat capacity at constant volume is $C_V$.

Einstein's mass–energy relation is $E = mc^2$.

If you want to include more than one character in the super- or subscript, you need to enclose it in curly braces {...}.

Type this:

The half-life of a substance is denoted by $t_{1/2}$ (not $t_1/2$).

to get this:

The half-life of a substance is denoted by $t_{1/2}$ (not $t_1/2$).


(Back to index)

Previous section: (1) Getting started

Next section: (2.2) Fractions and square roots

$\endgroup$
2
$\begingroup$

(2.3) Basic MathJax: Greek letters and other symbols

Note that all of the following must be typed within a MathJax expression (either in inline or display mode). If these commands are followed by alphabetical characters, they must be separated from the command by a space.

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.

Type this:

The Larmor frequency is $\nu = \gamma B_0 / 2 \pi$. Also, a photon of
wavelength $\lambda$ can excite an electron:

$$\Delta E = \frac{hc}{\lambda}$$

to get this:

The Larmor frequency is $\nu = \gamma B_0 / 2 \pi$. Also, a photon of wavelength $\lambda$ can excite an electron:

$$\Delta E = \frac{hc}{\lambda}$$

The lunate Greek letters can be typed using (for example) \varrho $\varrho$ instead of \rho $\rho$, but these are not often needed. See the links at the bottom of this answer for more information.


Other symbols

A non-exhaustive selection of commonly-used symbols is presented below.

Type... To get... Meaning
= $=$ equal to
\pm $\pm$ plus or minus
\times $\times$ multiplication symbol
\cdot $\cdot$ small centre dot (for multiplication)
\neq $\neq$ not equal to
< $<$ less than
> $>$ greater than
\leq $\leq$ less than or equal to
\geq $\geq$ greater than or equal to
\ll $\ll$ much less than
\gg $\gg$ much greater than
\geq $\geq$ greater than or equal to
\approx $\approx$ approximately equal to
\sim $\sim$ similar to
\propto $\propto$ proportional to
\infty $\infty$ infinity symbol
\partial $\partial$ partial derivative symbol
\hbar $\hbar$ reduced Planck constant

A more complete list of symbols may be found at the following websites:


I can't find a symbol I want!

You can try drawing it at https://detexify.kirelabs.org/classify.html. Note, however, that MathJax does not include, or support the use of, arbitrary LaTeX packages. Therefore, if Detexify indicates that some obscure package is needed for a particular symbol (it will say \usepackage{...}), there is a good chance that it will not work on MathJax.


(Back to index)

Previous section: (2.2) Basic MathJax: Fractions and square roots

Next section: (2.4) Basic MathJax: Spacing

$\endgroup$
1
$\begingroup$

Using arrows and the equal sign for chemical reactions

  • (a) On a microscopic level

\begin{array}{c cc c} Symbol & Markup & Example & Meaning \\ \hline \ce{->} & \text{->} & \ce{H + Br2 -> HBr + Br} & \text{one elementary step}\\ \ce{=} & \text{=} & \ce{H2 + Br2 = 2HBr} & \text{sum of several}\\ & & & \text{such elementary steps}\\ \end{array}

  • (b) On a macroscopic level

\begin{array}{c c c c} Symbol & Markup & Example & Meaning \\ \hline = & = & \ce{H2 + Br2 = 2HBr} & \text{stoichiometric equation} \\ \ce{->}& \text{->} & \ce{H2 + Br2 -> 2HBr} & \text{net forward reaction}\\ \ce{<-}& \text{<-} & \ce{H2 + Br2 <- 2HBr} & \text{net backward reaction}\\ \ce{<-->}& \text{<-->}& \ce{H2 + Br2 <--> HBr} & \text{reaction, both directions}\\ \ce{<=>}& \text<=> & \ce{H2 + Br2 <=> 2HBr} & \text{equilibrium}\\ \end{array}

  • (c) resonance structures

$\qquad$The two-sided arrow $\ce{<->}$ is used for resonance structures, e.g. the benzene resonance.

enter image description here

To write some markup over or under an arrow use brackets immediate after the arrow markup, e.g. $$\ce{A ->[over][under] B}$$ which renders as: $$\ce{A ->[over][under] B}$$

$\endgroup$
1
$\begingroup$

(2.2) Basic MathJax: Fractions and square roots

Fractions

Fractions can be typeset using \frac{<numerator>}{<denominator>}. It is preferable to typeset fractions in display style (i.e. with two dollar signs $$...$$).

Type this:

For an ideal gas, we have
$$ \frac{pV}{nRT} = 1. $$

to get this:

For an ideal gas, we have $$ \frac{pV}{nRT} = 1. $$

If fractions must be typed inline (using single dollar signs), it is better to write them using the '/' division sign (properly termed a solidus), or a negative power.

Type this:

For a real gas, we can define the compressibility factor $Z = pV/nRT$,
which is in general not equal to $1$.

to get this:

For a real gas, we can define the compressibility factor $Z = pV/nRT$, which is in general not equal to $1$.


Square roots

Square roots can be added in a similar manner using \sqrt{....}.

Type this:

The root-mean-square velocity of a gas is $\sqrt{3RT/M}$.

to get this:

The root-mean-square velocity of a gas is $\sqrt{3RT/M}$.


(Back to index)

Previous section: (2.1) Basic MathJax: Superscripts and subscripts

Next section: (2.3) Basic MathJax: Greek letters and other symbols

$\endgroup$
0
$\begingroup$

Spacing:

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

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

Back to INDEX

$\endgroup$
0
$\begingroup$

Further reading

Tex & LaTex

Italic vs upright characters

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.

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.

mhchem

The full documentation for mhchem is available.

Back to INDEX

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .