Typevia
Graphics and Diagrams

Getting Started with ChemFig

In the world of scientific writing, representing chemical structures accurately is a common requirement. While you could use external drawing software and import images, LaTeX offers a much more powerful and integrated solution: the chemfig package.

The chemfig package allows you to draw 2D chemical structures using a simple, text-based syntax. These drawings are rendered using TikZ, meaning they are vector-based, scale perfectly, and match the fonts of your document.

To begin using chemfig, you must include it in your document preamble:

\usepackage{chemfig}

The primary command you will use is \chemfig{...}. Inside the curly braces, you will describe the atoms and the bonds connecting them.


Basic Bonds and Linear Chains

The simplest way to use chemfig is by connecting atoms with bonds. The package uses intuitive symbols for different bond types:

  • - : Single bond
  • = : Double bond
  • ~ : Triple bond
  • > : Single cramped bond (stereochemistry)
  • < : Single cramped bond (stereochemistry)

Example 1: Simple Molecules

Let's look at how to draw a simple water molecule and an ethene molecule.

% Water molecule
\chemfig{H-O-H}

% Ethene molecule
\chemfig{H_2C=CH_2}

By default, chemfig draws bonds horizontally to the right. To create chains or specific shapes, we need to control the angles of the bonds.

Controlling Angles

You can specify the angle of a bond using the syntax -[angle]. There are two ways to do this:

  1. Predefined Increments: Use integers from 0 to 7, where each unit represents 45 degrees (0 = 0°, 1 = 45°, 2 = 90°, etc.).
  2. Absolute Angles: Use the syntax [:angle] for a specific degree (e.g., [:30]).
  3. Relative Angles: Use the syntax [::angle] to rotate relative to the previous bond.
% Propane using predefined increments
\chemfig{CH_3-[:30]CH_2-[:-30]CH_3}

Note: For beginners, using absolute angles (e.g., [:30]) is often the easiest way to ensure your molecule looks exactly how you intended.


Branching and Complex Structures

Most molecules aren't just straight lines; they have branches. In chemfig, branches are created using parentheses ().

When the "drawing pen" encounters a parenthesis, it "remembers" the current atom, draws everything inside the parenthesis, and then returns to that atom to continue the rest of the main chain.

Example 2: Branched Hydrocarbon (Isopropanol)

To draw isopropanol (2-propanol), we attach an -OH group to the middle carbon.

\chemfig{CH_3-CH(-[2]OH)-CH_3}

In this example:

  1. CH_3-CH draws the first part.
  2. (-[2]OH) tells LaTeX: "At this carbon, go up (angle 2 = 90°) and draw an OH group, then come back to the carbon."
  3. -CH_3 continues from the carbon where we left off.

You can even nest parentheses to create branches on branches!


Drawing Rings and Cycles

Rings are a fundamental part of organic chemistry. chemfig uses a special syntax for rings: *n(content), where n is the number of sides in the ring.

Example 3: Benzene and Cyclohexane

A 6-carbon ring is drawn using *6. To add double bonds (like in benzene), you place the = sign inside the ring description.

% Cyclohexane
\chemfig{*6(------)}

% Benzene (with alternating double bonds)
\chemfig{*6(-=-=-=)}

% Benzene with a functional group (Phenol)
\chemfig{OH-*6(-=-=-=)}

In the ring syntax, the bond - or = represents the connection between the vertices of the polygon. If you want to attach something to a specific corner of the ring, you simply place the atom or another bond command after the bond symbol for that side.

Note: If you need to draw a benzene ring with a circle inside (the resonance representation), you can use the \polymerhook or specific TikZ decorations, but for beginners, the alternating = - syntax is standard.


Customizing Appearance

You can modify the look of your molecules by passing parameters to the bonds. The full syntax for a bond is: -[angle, length, arrival atom, departure atom, tikz code]

Most of the time, you only need the first two or the last one.

Adjusting Bond Length

If a molecule looks too cramped, you can multiply the default bond length:

% Longer bonds for clarity
\chemfig{A-[,2]B-[,0.5]C} 
% B is twice as far from A; C is half as far from B.

Adding Color

Since chemfig uses TikZ, you can pass color commands directly into the bond's "tikz code" section (the 5th parameter).

% A molecule with a red double bond
\chemfig{CH_2=[,,,red]CH_2}

Best Practices and Common Pitfalls

To keep your chemfig code readable and your documents professional, follow these tips:

  1. Keep it Modular: For very large molecules, you can define parts of the molecule as macros using \newcommand. This prevents one \chemfig command from becoming 20 lines long.
  2. Watch your Parentheses: Just like in math, every opening ( must have a closing ). Forgetting a closing parenthesis is the most common cause of "Dimension too large" or "Paragraph ended before \chemfig was complete" errors.
  3. Atom Alignment: chemfig tries its best to align atoms, but sometimes text labels like CH_3 look slightly off. You can use the "departure" and "arrival" parameters (parameters 3 and 4 in the bond syntax) to fine-tune which character the bond touches.
  4. Use the \chemmove command: If you need to draw reaction mechanisms (electron pushing arrows), chemfig provides the \chemmove command to draw arrows between specific "nodes" within your molecules.

Summary Example: Aspirin

Let's combine everything we've learned to draw the molecule for Aspirin (Acetylsalicylic acid).

\chemfig{
            % The carboxylic acid group
    HO-C(=[2]O)-*6(-(-O-C(=[2]O)-CH_3)=-=-=)
}

In this example, we start with the hydroxyl group, move to a carbon that branches into a double-bonded oxygen, then move into a 6-membered ring. One of the positions on that ring branches off into the ester group.

By mastering these basic building blocks—bonds, angles, branches, and rings—you can represent almost any chemical structure directly within your LaTeX document with professional, publication-quality results.