Accurately and beautifully typesetting chemical formulas and reaction mechanisms is crucial for scientific publications in chemistry and related fields. While generic math mode can piece together subscripts and superscripts, the mhchem and chemfig packages provide dedicated syntax and drawing capabilities that integrate seamlessly into your LaTeX workflow. In this post, we’ll guide you through practical techniques—from simple inline formulas to complex reaction schemes and custom macros—so you can produce publication-quality chemical content without leaving your .tex source.
This tutorial is aimed at LaTeX users familiar with the basics who want to level up their chemistry typesetting. We’ll cover:
mhchem for inline formulas and reactionschemfigchemnum for numbered species
The Problem: Manually writing subscripts, superscripts, and reaction arrows in math mode leads to verbose and inconsistent code.
Before (Math Mode):
\[
\mathrm{H_2 + O_2 \rightarrow H_2O}
\]
After (mhchem):
\documentclass{article}
\usepackage[version=4]{mhchem}
\begin{document}
\[
\ce{2 H2 + O2 -> 2 H2O}
\]
\end{document}
Explanation: The \ce{…} command automatically handles stoichiometric subscripts, reaction arrows, and charges. It even aligns nested parentheses and charges on ions, ensuring concise and readable source code.
The Problem: Adding text above and below arrows using raw \xrightarrow commands is cumbersome.
Before (Manual Arrows):
A + B \xrightarrow[\text{heat}]{\text{cat.}} C
After (mhchem Arrows):
\[
\ce{A + B ->[\,\text{cat.}\,][\,\Delta\,] C}
\]
Explanation: In mhchem, you can place text both above and below the arrow with the syntax ->[above][below]. Whitespace controls prevent cramped arrows.
The Problem: Hand-drawing molecular skeletons in TikZ is laborious and syntax-heavy.
Before (Raw TikZ):
\begin{tikzpicture}
\draw (0,0)--(1,0) node[right]{CH3};
\draw (0,0)--(-1,0) node[left]{CH3};
\draw (0,0)--(0,1) node[above]{OH};
\end{tikzpicture}
After (chemfig):
\documentclass{article}
\usepackage{chemfig}
\begin{document}
\chemfig{CH_3-[:30]C(-[:90]OH)(-[:330]CH_3)-[:270]H}
\end{document}
Explanation: chemfig uses angle-based notation and automatic bond lengths. You can nest substituents in parentheses, and the library handles atom placement and bond drawing.
The Problem: Default bonds can be plain and rings require manual coordination.
Before (Default):
\chemfig{*6(=-=-=)}
After (Custom Styles):
\setchemfig{atom sep=2em, bond style={line width=1pt}, default bond=signed}
\definesubmol{Ph}{*6((=[:-30]Ph)-[:-30]-[,:60]Ph)}
\chemfig{Ph-CH_2-CH_2-Ph}
Explanation: You can adjust global settings via \setchemfig (e.g. bond thickness, atom separation). Custom submolecules like \definesubmol let you reuse rings or functional groups with a single identifier.
The Problem: Manually labeling each intermediate in a multi‐step scheme is error‐prone.
Before (Manual Labels):
\[
\text{Step 1: } A \to B \quad
\text{Step 2: } B \to C
\]
After (chemnum):
\usepackage{chemnum}
\begin{document}
\chemnumsetup{format=\bfseries}
\chemnum{A -> B -> C}
\end{document}
Explanation: The chemnum package labels each species in a reaction automatically and lets you refer back to them in text or captions, maintaining consistency.
The Problem: Repeating long chemfig definitions clutters the preamble.
Before (Inline):
\chemfig{C(-[:90]OH)(-[:150]CH_3)(-[:30]CH_3)-[:270]H}
After (Macro):
\newcommand{\tertbutanol}{
\chemfig{C(-[:90]OH)(-[:150]CH_3)(-[:30]CH_3)-[:270]H}
}
\begin{document}
\tertbutanol
\end{document}
Explanation: Defining commands for frequently used structures makes your document cleaner and eases tweaks—change the macro once, and all instances update.
The Problem: Flat structural diagrams lack depth and stereochemical information.
After (chemfig + TikZ 3D):
\usepackage{tikz-3dplot}
\tdplotsetmaincoords{70}{110}
\begin{tikzpicture}[tdplot_main_coords, scale=2]
\chemfig{C*2(-[2]H)(-[6]H)(-[0]OH)}
\draw[->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};
\end{tikzpicture}
Explanation: Leverage tikz-3dplot coordinates to rotate and project your chemfig diagrams in three dimensions, giving readers spatial insight into stereochemistry.
The Problem: Manually building a list of reaction schemes in a section is tedious and error‐prone.
After (tocloft + custom commands):
\usepackage{tocloft}
\newlistof{schemelist}{sch}{List of Schemes}
\newcommand{\scheme}[2][]{%
\refstepcounter{schemelist}%
\par\medskip
\noindent\textbf{Scheme \theschemelist. #1}\par
#2\par
\addcontentsline{sch}{schemelist}{Scheme \theschemelist: #1}%
}
\begin{document}
\listofschemes
\scheme[Hydrogenation]{\ce{R-CH=CH-R ->[H2][Pd/C] R-CH2-CH2-R}}
\end{document}
Explanation: Define a new “List of Schemes” and a wrapper command that numbers and collects each reaction. The list updates automatically as you add or remove schemes.
mhchem — \ce{…} for formulas & reactionschemfig — \chemfig{…} for 2D structures\setchemfig, \definesubmol — styling & reusable motifschemnum — automatic numbering of speciestikz-3dplot — 3D coordinate controltocloft — custom lists for schemesmacros — define commands for complex or repeated content