There are a number of software packages available for creating chemistry diagrams in LaTeX. One of those packages is ChemFig. ChemFig is a powerful package, with many features which aren’t available in other packages. The syntax is fairly easy to follow once you understand the structure, but going in blind can be a bit daunting. This post will describe the basic syntax.

For all examples below, \usepackage{chemfig} needs to be in the preamble. No other configuration needs to be set, and no other packages are required.

Let’s take a basic example, the water molecule. This can be drawn in a number of ways:

\chemfig{H-O-H} will create an image that looks much like the ASCII code:
ChemFig H2O molecule inline

This is useful for simple molecules, but it only shows the covalent bonds. If we also want to represent the angles between the bonds, we can do this in one of three ways:

  1. Add an angle step; a number between 0 and 7, where 0 means an upward (north) bond, 2 means a bond to the right (east), and so on. Each step adds 45 degrees.
    \chemfig{H-[1]O-[7]H}
    ChemFig H2O molecule with angle step
  2. Set the angle explicitly. This angle is added to the horizontal (east) in an anti-clockwise direction, and can be negative. For example, an upward bond would have an angle of 90.
    \chemfig{H-[:37.75]O-[:-37.75]H}
    ChemFig H2O molecule with angle set at 104.5 degrees
    (angle here is 104.5 degrees)
  3. Set the angle relative to the previous angle (or 0 if no branches have been added).
    \chemfig{H-[::37.75]O-[::-75.5]H}
    ChemFig H2O molecule with angle set at 104.5 degrees
    (angle here is also 104.5 degrees)

Some molecules contain a double covalent bond. These bonds can be represented using an equals sign:

\chemfig{O=O}
ChemFig oxygen molecule

You may have noticed that each branch is taken from the last atom/sub-molecule listed, but sometimes that’s not what you want. You can use brackets to indicate that no connection should be made from a branch. For example, methane can be drawn like so:

\chemfig{C(-[0]H)(-[2]H)(-[4]H)(-[6]H)}
ChemFig methane molecule

With these features, you should be able to create most simple chemistry diagrams in LaTeX, such as D-glucose:

\chemfig{
    H-[7]C(=[1]O)
    -[6]
    C(-[4]H)(-OH)
    -[6]
    C(-[4]OH)(-H)
    -[6]
    C(-[4]H)(-OH)
    -[6]
    C(-[4]H)(-OH)
    -[6]
    CH_2OH
}

ChemFig D-glucose molecule

ChemFig is much more powerful than described here. The docs on the project website are very thorough and clear, should you wish to do more than what is described here.