Introduction to Chemistry in LaTeX
For scientists, researchers, and students, representing chemical structures accurately is essential. While you could use external drawing software and import images, LaTeX offers a more powerful and integrated solution: the chemfig package.
Using chemfig, you can "code" your molecules directly into your document. This ensures that your diagrams have the same font, line weight, and professional aesthetic as your text. In this guide, we will focus on the fundamentals of drawing 2D chemical structures using chemfig.
Note: To follow along with these examples, you must include
\usepackage{chemfig}in your document's preamble.
2. Drawing Basic Bonds and Linear Chains
The core of chemfig is the \chemfig{} command. Within the curly braces, you describe the atoms and the bonds connecting them.
Basic Bond Types
The symbols used for bonds are intuitive:
-: Single bond=: Double bond~: Triple bond
Specifying Angles
By default, chemfig draws bonds horizontally to the right. To change direction, you use an optional argument in square brackets [angle].
There are two ways to define angles:
- Absolute Angles:
[0]is right,[90]is up,[180]is left, and[270]is down. (Increments of 45 degrees are standard: 0, 1, 2... up to 7). - Relative Angles: Using
[:angle]allows you to specify a precise degree (e.g.,[:30]).
Example 1: A Simple Alkene
Here is how you would draw a simple ethene and propane chain:
% Simple Ethene
\chemfig{H_2C=CH_2}
% Propane with angles
\chemfig{H_3C-CH_2-CH_3}
% Propane in a "zig-zag" format using angles
\chemfig{A-[:30]B-[:-30]C}3. Creating Branches and Side Chains
Most molecules aren't just straight lines. To create a branch, you use parentheses (). Anything inside the parentheses is treated as a side chain attached to the preceding atom.
How Branching Works
When chemfig encounters a parenthesis, it "remembers" the current atom, draws the branch inside the parenthesis, and then returns to that "remembered" atom to continue the main chain.
Example 2: Branched Hydrocarbons
Let's look at Isopropanol and a more complex branched structure.
% Isopropanol (Propan-2-ol)
\chemfig{CH_3-CH(-[2]OH)-CH_3}
% A molecule with multiple branches
\chemfig{A-B(-[2]C)-D(-[-2]E)-F}Note: In the example above,
-[2]tells the hydroxyl group (OH) to point straight up (90 degrees), while-[-2]tells the group to point straight down.
4. Drawing Ring Structures
Rings are a fundamental part of organic chemistry. In chemfig, rings are created using the syntax *n(content), where n is the number of sides in the ring.
Building the Ring
The ring starts from the last atom defined. The "content" inside the parentheses defines the bonds and atoms that make up the ring.
Example 3: Benzene and Cyclohexane
Benzene is the most common ring. Here is how to draw it with alternating double bonds:
% Cyclohexane (Simple 6-membered ring)
\chemfig{*6(------)}
% Benzene with alternating double bonds
\chemfig{*6(-=-=-=)}
% Benzene with a functional group (Phenol)
\chemfig{HO-*6(-=-=-=)}Tip: You can also use the shortcut
**6(------)to draw a hexagon with a circle inside, representing the delocalized electron system in benzene.
5. Stereochemistry: Wedges and Dashes
To represent 3D depth on a 2D page, chemists use "wedge" and "dash" bonds. chemfig provides specific symbols for these:
<: A solid wedge bond (pointing toward the viewer).<:: A dashed/striped wedge bond (pointing away).<|: A bold bond.
Example 4: A Chiral Center
This example demonstrates a central carbon atom with four different groups arranged in space.
\chemfig{C(-[2]Up)(<:[-1]Away)(<[7]Toward)-Left}This code creates a central carbon (C) with:
- An "Up" group at 90 degrees.
- An "Away" group using a dashed wedge.
- A "Toward" group using a solid wedge.
- A "Left" group (default angle is 180 when following another bond).
6. Best Practices and Common Pitfalls
Drawing complex molecules can become confusing quickly. Follow these best practices to keep your code clean:
1. Use Meaningful Whitespace
LaTeX ignores extra spaces in the code, so you can use them to make your \chemfig commands more readable.
% Hard to read
\chemfig{A-B(-[2]C)-D}
% Easier to read
\chemfig{
A - B ( -[2]C ) - D
}2. Consistency in Angles
Stick to a consistent "zig-zag" angle (usually 30 or 60 degrees) throughout your document to ensure all your molecules look like they belong to the same set.
3. Common Mistakes to Avoid
- Forgetting Parentheses: If you forget to close a parenthesis, LaTeX will throw a "Runaway argument" error.
- Overlapping Bonds: If your molecule looks messy, try adjusting the angles (e.g., changing
[1]to[:60]) to give the atoms more breathing room. - Atom Alignment: Sometimes atoms like "OH" or "NH2" look slightly off-center. You can use the
|symbol to tellchemfigexactly where the bond should attach:\chemfig{C-H|O}.
4. Integrating with Formulas
While chemfig is for structures, use the mhchem package (command \ce{}) for simple chemical formulas in your text.
- Use
\ce{H2O}for water. - Use
\chemfig{H-O-H}for the structure of water.
By combining these two packages, you can create professional, publication-ready chemistry documents entirely within LaTeX.
