Mastering Chemical Typesetting in LaTeX with mhchem & Chemfig

Keywords: mhchem, chemfig, chemical equations, reaction arrows, molecular diagrams, TikZ, macros, chemnum

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:

  1. Setting up mhchem for inline formulas and reactions
  2. Advanced reaction arrows and annotated schemes
  3. Drawing structural formulas with chemfig
  4. Customizing bond styles and ring templates
  5. Combining chemnum for numbered species
  6. Macro definitions for repeated motifs
  7. 3D coordinate representations with TikZ hooks
  8. Automating tables of contents for schemes

 

1. Minimal Setup for mhchem

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.

2. Annotated Reaction Mechanisms

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.

3. Drawing Molecules with Chemfig

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.

4. Custom Bond Styles and Rings

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.

5. Numbering Molecules with chemnum

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.

6. Crafting Reusable Macros

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.

7. Integrating 3D Perspectives

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.

8. Automated Scheme Listings

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.

Why Native Chemical Typesetting Matters

  • Consistency: All formulas, arrows, and diagrams share your document’s fonts and styles.
  • Reproducibility: Adjust reaction parameters or structures directly in LaTeX; no external image tweaks.
  • Collaboration: Version control diffs show clear changes in your source rather than binary PDFs.
  • Extensibility: Combine multiple packages (mhchem, chemfig, chemnum, TikZ) to cover every notation need.

Key Packages & Commands

  • mhchem\ce{…} for formulas & reactions
  • chemfig\chemfig{…} for 2D structures
  • \setchemfig, \definesubmol — styling & reusable motifs
  • chemnum — automatic numbering of species
  • tikz-3dplot — 3D coordinate control
  • tocloft — custom lists for schemes
  • macros — define commands for complex or repeated content