# How can I format math/chemistry expressions here?

This post is meant to teach new folks formatting with MathJax and its extension mhchem on chem.SE

## INDEX

Getting Started

Basic MathJax

Basic mhchem

Learning by example

• Tex & LaTex
• Italic vs upright characters
• MathJax extensions
• Full documentation for mhchem

### Getting started

There are three different rendering engines being used.

Markup

This is a basic variation on HTML tags. It adds some simple text formatting. See Markup (off page link)

MathJax

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.

• $...$ - A pair of single dollar signs make the math inline, for example, Let $x$ be a variable renders as:

Let $$x$$ be a variable.

• $$...$$ - A pair of double dollar signs make the math a block element. It gets its own line, its slightly larger, and centered. For example, The equation of motion is as follows: $$v=u+at$$ It is a SUVAT equation renders as:

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

mhchem extension for MathJax

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

The formatting command to invoke mhchem within MathJax is: \ce{...}. The mhchem command must appear with a MathJax markup - i.e with a $...$ or $$...$$ expression. Then the parameters within the braces are automatically passed to mhchem for rendering instead of MathJax. For example the markup:

$\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-}$$

## Back to INDEX

• This is nice. I will also put in a vote for adding this to the FAQ as soon as the higher-ups allow. – Colin McFaul Jun 28 '12 at 23:20
• @ColinMcFaul: I can add it to the faq myself. We already have a condensed version of this in the second section here. This page shall be linked to in the editing help (pops up when you're writing a post on the sidebar). (See meta.chemistry.stackexchange.com/questions/82/…) – ManishEarth Jun 29 '12 at 4:18
• @ManishEarth As per this question we should probably readd it to the new Help center – jonsca Jul 13 '13 at 8:14
• I think this meta-post should also be linked here: meta.chemistry.stackexchange.com/q/443/4945 – Martin - マーチン Jan 19 '15 at 10:51
• @ManishEarth Any suggestions on how to draw structures (for example in reaction mechanisms?) – Aritra Das Jan 1 '16 at 19:22
• I tried to load the mhchem pdf and got this error: Der folgende Fehler wurde beim Versuch die URL http://ctan.imsc.res.in/macros/latex/contrib/mhchem/mhchem.pdf zu holen festgestellt: Konnte keine IP Adresse vom Hostnamen "ctan.imsc.res.in" ermitteln. (Couldn't lookup host) Server Failure: The name server was unable to process this query. and in the pre-formatted mail to webmaster: Referer: http://meta.chemistry.stackexchange.com/questions/86/how-can-i-format-math-chemistry-expressions-here/88 Via: 1.1 s4de3esydie (squid/3.4.8)  – ott-- Feb 7 '16 at 5:11
• @AritraDas You may check my guide on how to create organic chemistry mechanisms. – Pritt says Reinstate Monica Aug 4 '19 at 4:29

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 • @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. – Karsten Theis May 21 '20 at 16:48 • Looks good, thank you for doing this! I have only a couple of minor corrections, one per comment below. – andselisk May 22 '20 at 7:51 • 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. – andselisk May 22 '20 at 7:51 • 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. – andselisk May 22 '20 at 7:51 • 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)}. – andselisk May 22 '20 at 7:52 • 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). – andselisk May 22 '20 at 7:52 • @andselisk OK, you just gave me some homework. I'll try to update those examples. – Karsten Theis May 22 '20 at 12:46 • @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 – MaxW May 23 '20 at 23:58 • Bummers - I reverted to previous version. You can't swipe/copy markup in the table. :-( – MaxW May 24 '20 at 0:09 ## 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 ### 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}$$ ## Back to INDEX ### Basic MathJax: 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}$$ ## Back to INDEX ### 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. 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}$$ ### Common signs and symbols $$\begin{array}{|c|c|c|}\hline \text{Expression} & \text{Markup (without \...\)} & \text{Symbol Meaning }\\ \hline a = b & \text{a = b}& \text{equal} \\ \hline a \ne b & \text{a \ne b} & \text{a is not equal to b}\\ \hline a \overset{\wedge}{=}b & \text{a \overset{\wedge}{=}b} & \text{ corresponds to}\\ \hline a \approx b & \text{a \approx b} & \text{approximately equal} \\ \hline a \simeq b &\text{a \simeq b} & \text{asymptotically equal}\\ \hline a \sim b & \text{a \sim b} & \text{proportional}\\ \hline a \propto b & \text{a \propto b} & \text{proportional}\\ & & \text{(Alternate)} \\ \hline a \lt b& \text{a \lt b}& \text{less than} \\ \hline b \gt a & \text{b \gt a} & \text{greater than}\\ \hline a \leqslant b & \text{a \leqslant b} & \text{less than or equal} \\ \hline a \leq b & \text{a \leq b} & \text{less than or equal}\\ & & \text{(Alternate)} \\ \hline b \geqslant a & \text{b \geqslant a} & \text{greater than or equal }\\ \hline b \geq a & \text{b \geq a} & \text{greater than or equal}\\ & & \text{(Alternate)} \\ \hline a \ll b & \text{a \ll b} & \text{much less than}\\ \hline b \gg a & \text{b \gg a} & \text{much greater than}\\ \hline \infty& \text{\infty} & \text{infinity symbol}\\ \hline \pi & \text{\pi} & \text{\pi Symbol} \\ \hline \end{array}$$ ## Back to INDEX ### Basic MathJax: 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. • e.g. \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}. There are special commands to distinguish between the lunate Greek letters. • e.g. \varepsilon , \varsigma , \varrho , and \varpi render as $$\varepsilon ,\ \varsigma ,\ \varrho ,\text{and}\ \varpi$$ ## Back to INDEX ### 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 ### Basic math: Fractions and square roots Fractions 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

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

## Back to INDEX

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