91
$\begingroup$

This post is meant to teach new folks how to use MathJax and mhchem formatting on chem.SE

$\endgroup$
90
$\begingroup$

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.

Further reading

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.

$\endgroup$
  • 2
    $\begingroup$ This is nice. I will also put in a vote for adding this to the FAQ as soon as the higher-ups allow. $\endgroup$ – Colin McFaul Jun 28 '12 at 23:20
  • $\begingroup$ @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/…) $\endgroup$ – ManishEarth Jun 29 '12 at 4:18
  • $\begingroup$ @ManishEarth As per this question we should probably readd it to the new Help center $\endgroup$ – jonsca Jul 13 '13 at 8:14
  • 2
    $\begingroup$ I think this meta-post should also be linked here: meta.chemistry.stackexchange.com/q/443/4945 $\endgroup$ – Martin - マーチン Jan 19 '15 at 10:51
  • 3
    $\begingroup$ @ManishEarth Any suggestions on how to draw structures (for example in reaction mechanisms?) $\endgroup$ – Aritra Das Jan 1 '16 at 19:22
  • $\begingroup$ 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) $\endgroup$ – ott-- Feb 7 '16 at 5:11

You must log in to answer this question.

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