Example post to teach new users the basics of how to format expressions with mathjax and mhchem. Taken with minor edits from the Chem SE.
1 Answer
Getting started
On Matter Modeling 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.
Further reading
- Wikipedia TeX help page (extremely useful as a reference, useless as a tutorial)
- TeX/LaTeX Stack Exchange site
- Harvard intro to TeX
- LaTeX wikibook, Math section
- LaTeX wikibook, Advanced Math section
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.
-
$\begingroup$ Thanks for this! I found myself referring to this today while editing a new user's post! It's a lot of scrolling required! Perhaps this could be improved by adding a "table of contents" at the beginning with hyperlinks to the major sections like "Basic Chem" and "Basic Math". Also, I don't see the benefit of adding the green checkmark, though some new users might see that as encouraging self-accepting, which generally I discourage because it serves no purpose (doesn't pin the answer to the top, doesn't add +2 rep, etc.) other than telling others that the OP is not so interested in new answers. $\endgroup$ Dec 29, 2021 at 3:53
-
$\begingroup$ @NikeDattani On Meta, I would say self-accepting would be fine for pinning, but I agree this one doesn't need the pin for now. I'll see about adding links. I don't think I can hyperlink to a given line, but I might be able to hack something together to reference section labels. $\endgroup$– Tyberius ModJan 2 at 3:54
require{}
environments. Some examples are given on Chem SE: chemistry.meta.stackexchange.com/questions/3540/… $\endgroup$