Chemistry formulae
There are a few LaTeX packages to create chemistry formulae: chemfig, ochem, streetex, and xymtex. The most intuitive is probably the chemfig
package. This article explains how to use the chemfig
package to create chemical formulae in LaTeX.
Introduction
Drawing a molecule consists mainly of connecting groups of atoms with lines. Simple linear formulae can be easily drawn using the chemfig
package, as shown in the following example:
\documentclass{article}
\usepackage{chemfig}
\begin{document}
\section{Introduction}
Writing chemical formulae with chemfig is straightforward.
\chemfig{A=B}
\end{document}
Open this chemfig
example in Overleaf
The following image shows the output produced by the example above:
The package is imported by \usepackage{chemfig}
in the preamble. The command \chemfig{A=B}
the draws the molecule. The symbol =
determines the type of bond. See the reference guide for a list of bond types.
Angles
There are several ways to define angles between bonds in molecules.
To define chemical formulae you can use units that define the angles
\chemfig{A-[1]B-[7]C}
Absolute angles
\chemfig{A-[:50]B-[:-25]C}
Relative angles
\chemfig{A-[::50]B-[::-25]C}
Open this chemfig
example in Overleaf
The following image shows the output produced by the example above:
Each one of the three commands in the example above uses a different method to determine the angle between bonds.
default units
: In the command\chemfig{A-[1]B-[7]C}
the parameters inside brackets set the angle in special units, each unit equals 45°. Hence in the example the angles are 45° and 315°.absolute units
: The angles can be set in absolute units, in the command\chemfig{A-[:50]B-[:-25]C}
the parameter inside the brackets represent the angle, in degrees, measured from the horizontal baseline. Negative angles are allowed.relative angles
: In the third example\chemfig{A-[::50]B-[::-25]C}
the angles are measured from the previous bond, instead of the baseline.
Rings
The example below presents the syntax to draw regular polygons:
Regular polygons:
\chemfig{A*5(-B=C-D-E=)}
Incomplete rings are also possible:
\chemfig{A*5(-B=C-D)}
Open this chemfig
example in Overleaf
The following image shows the output produced by the example above:
The syntax of the command \chemfig{A*5(-B=C-D-E=)}
is explained below:
A
: This is the first atom, the rest of the atoms will be drawn from here*5
: Number of sides of the polygon(-B=C-D-E=)
: The rest of the atoms and bonds. If not enough are passed to complete the polygon, an incomplete ring will be drawn.
There is an additional parameter that can be passed to this command: a double asterisk. For instance, if **5
is set instead of *5
a circle is drawn inside the polygon.
Connected rings
The next example draws the chemical structure of caffeine:
\documentclass{article}
\usepackage{chemfig}
\begin{document}
\section{I need caffeine.}
\chemfig{*6((=O)-N(-H)-(*5(-N=-N(-H)-))=-(=O)-N(-H)-)}
\end{document}
Open this chemfig
example in Overleaf
The following graphic shows the output produced by the caffeine example:
Branches
Chemical formulae are not always linear, branched formulae are actually the most common type. Below an example on how to create them.
Branched molecule \vspace{.5cm}
\chemfig{H-C(-[2]H)(-[6]H)-C(=[1]O)-[7]H}
Open this chemfig
example in Overleaf
The following image shows the output produced by the example above:
Branches in each node are created by adding formulas inside parentheses. For instance, the code
C(-[2]H)(-[6]H)
creates two branches in "C", one with a 2 units angle (90°) and other with a 6 units angle (270°).
Branches can also be added to rings:
Branched ring
\vspace{.5cm}
\chemfig{A*6(-B=C(-CH_3)-D-E-F(=G)=)}
Open this chemfig
example in Overleaf
The following image shows the output produced by the example above:
The syntax is similar, using parentheses a branch can be attached to a node (atom). For instance
F(=G)
attaches a branch to the node "F". More complex examples can be created using nested branches and even attaching rings as branches.
Customizing the formulae
Several parameters such as colours and the node separation can be changed, also additional text to describe the formula can be added.
{\huge
\setchemfig{atom sep=2em,bond style={line width=1pt,red,dash pattern=on 2pt off 2pt}}
\chemname
{\chemfig{H-C(-[2]H)(-[6]H)-C(=[1]O)-[7]H}}
{Acetaldehyde}
}
Open this chemfig
example in Overleaf
The following image shows the output produced by the example above:
There are three new commands here:
\setbondstyle{ }
: Inside the braces several style-related commands can be passed using the <code<tikz syntax.
\setatomstep{2em}
: The separation between atoms (nodes) in the formula is set to 2em. Other LaTeX units can be used.
\chemname{}{}
: The first parameter in this command is achemfig
formula, the second one is some text that will be printed below the formula. In the example, the text is "Acetaldehyde".
Notice also that the font used is huge
. You can use any other font size and the formula will be scaled accordingly.
Reference guide
Bond types
Bond type | code | output |
---|---|---|
single | \chemfig{O - H}
|
![]() |
double | \chemfig{O = H}
|
![]() |
triple | \chemfig{O ~ H}
|
![]() |
plain right cram | \chemfig{O > H}
|
![]() |
plain left cram | \chemfig{O < H}
|
![]() |
dashed right cram | \chemfig{O >: H}
|
![]() |
dashed left cram | \chemfig{O <: H}
|
![]() |
hollow right cram | \chemfig{O >| H}
|
![]() |
hollow left cram | \chemfig{O <| H}
|
![]() |
Further reading
For more information see
Overleaf guides
- Creating a document in Overleaf
- Uploading a project
- Copying a project
- Creating a project from a template
- Using the Overleaf project menu
- Including images in Overleaf
- Exporting your work from Overleaf
- Working offline in Overleaf
- Using Track Changes in Overleaf
- Using bibliographies in Overleaf
- Sharing your work with others
- Using the History feature
- Debugging Compilation timeout errors
- How-to guides
- Guide to Overleaf’s premium features
LaTeX Basics
- Creating your first LaTeX document
- Choosing a LaTeX Compiler
- Paragraphs and new lines
- Bold, italics and underlining
- Lists
- Errors
Mathematics
- Mathematical expressions
- Subscripts and superscripts
- Brackets and Parentheses
- Matrices
- Fractions and Binomials
- Aligning equations
- Operators
- Spacing in math mode
- Integrals, sums and limits
- Display style in math mode
- List of Greek letters and math symbols
- Mathematical fonts
- Using the Symbol Palette in Overleaf
Figures and tables
- Inserting Images
- Tables
- Positioning Images and Tables
- Lists of Tables and Figures
- Drawing Diagrams Directly in LaTeX
- TikZ package
References and Citations
- Bibliography management with bibtex
- Bibliography management with natbib
- Bibliography management with biblatex
- Bibtex bibliography styles
- Natbib bibliography styles
- Natbib citation styles
- Biblatex bibliography styles
- Biblatex citation styles
Languages
- Multilingual typesetting on Overleaf using polyglossia and fontspec
- Multilingual typesetting on Overleaf using babel and fontspec
- International language support
- Quotations and quotation marks
- Arabic
- Chinese
- French
- German
- Greek
- Italian
- Japanese
- Korean
- Portuguese
- Russian
- Spanish
Document structure
- Sections and chapters
- Table of contents
- Cross referencing sections, equations and floats
- Indices
- Glossaries
- Nomenclatures
- Management in a large project
- Multi-file LaTeX projects
- Hyperlinks
Formatting
- Lengths in LaTeX
- Headers and footers
- Page numbering
- Paragraph formatting
- Line breaks and blank spaces
- Text alignment
- Page size and margins
- Single sided and double sided documents
- Multiple columns
- Counters
- Code listing
- Code Highlighting with minted
- Using colours in LaTeX
- Footnotes
- Margin notes
Fonts
Presentations
Commands
Field specific
- Theorems and proofs
- Chemistry formulae
- Feynman diagrams
- Molecular orbital diagrams
- Chess notation
- Knitting patterns
- CircuiTikz package
- Pgfplots package
- Typesetting exams in LaTeX
- Knitr
- Attribute Value Matrices
Class files
- Understanding packages and class files
- List of packages and class files
- Writing your own package
- Writing your own class