tikz.dev / chemfig Manual

chemfig

Advanced usage

11 Annotated examples

In this chapter, several molecules will be drawn, putting into use the methods previously described. The aim here is to show a logical order for putting together a molecule so that the user unfamiliar with chemfig will learn how to construct complex molecules. The construction steps will be shown to help with this learning process.

In addition, several possibilities — some intuitive and others less so — will be shown which give the same graphical results, with the objective being to show that chemfig allows some flexibility in encoding molecules. One can see how each is put together and adopt the methods with which one is most comfortable.

11.1 Ethanal

Here we will draw the ethanal (or acetaldehyde) molecule: Compiled output

The best method for non-cyclic molecules is to select the longest chain. Here one could take “H-C-C=0” for example. The bond C=O is tilted to 45° by using the predefined angle “[1]”. This give a “backbone” of the molecule to which the branches merely have to be added:

Compiled output

Backbone of ethanal
\chemfig{H-C-C=[1]O}

The three hydrogen atoms still have to placed at the correct orientation with the help of predefined angles. The first is at 90° with the branch “(-[2]H)”, the second at 270° with “(-[6H])”, and the one on the right at 315° with “(-[7]H)”:

Compiled output

Ethanal
\chemfig{H-C(-[2]H)(-[6]H)-C(-[7]H)=[1]O}
11.2 2-amino-4-oxohexanoic acid

Here is the molecule to be drawn: Compiled output

As is often the case for most molecules, there are several methods and for each several different ways of getting the result. Here we will look at four different methods.

11.2.1 Absolute angles

We will first of all draw the central chain with absolute angles. We set the default angle to \(+30\degres\) with the optional argument, and so only the descending bonds need to have their absolute angle set to \(-30\degres\):

Compiled output

Backbone (absolute angles)
\chemfig{[:30]--[:-30]--[:-30]--[:-30]OH}

The branches “(=[6]O)”, “(-[6]NH_2)” and “(=[2]O)” still have to be added to the correct vertices:

Compiled output

Molecule (absolute angles)
\chemfig{[:30]--[:-30](=[6]O)--[:-30](-[6]NH_2)-(=[2]O)-[:-30]OH}
11.2.2 Relative angles

A more general approach uses only relative angles, in this way:

Compiled output

Structure (relative angles)
\chemfig{[:30]--[::-60]--[::-60]--[::-60]OH}

then

Compiled output

Molecule (relative angles)
\chemfig{[:30]--[::-60](=[::-60]O)--[::-60](-[::-60]NH_2)
-(=[::60]O)-[::-60]OH}
11.2.3 Ring

Since the angles between the bonds are 120°, it is possible to use a 6-ring, although this method is less natural. Here we take advantage of the fact that a ring can be left unfinished. The ring must be rotated 120° so that the first vertex is to the south-east of the ring:

Compiled output

Backbone (ring)
\chemfig{[:120]NH_2*6(---=O)}

Now the branches must be added to the right vertices:

Compiled output

Molecule (ring)
\chemfig{[:120]NH_2*6(-(-(=[::60]O)-[::-60]OH)--(--[::60])=O)}
11.2.4 Nested rings

Delving deeper into the ring method, we can also consider nesting incomplete 6-rings. We could start with this backbone:

Compiled output

Backbone (nested rings)
\chemfig{*6(--*6(--=O))}

And then add the bonds which leave the vertices of these rings. There are no angles to worry about because the bonds leaving the rings are the bisectors of the sides of the ring, exactly what we want here:

Compiled output

Molecule (nested rings)
\chemfig{*6((-)-(=O)-*6(-(-NH_2)-(-OH)=O))}

A close look shows that the second line segment of the double bond to the oxygen atom is inside the incomplete 6-ring1 Despite its brevity, this code does not give a perfect drawing. This can of course be corrected by adding a little to the code:

Compiled output

Molecule (corrected nested rings)
\chemfig{*6((-)-(=O)-*6(-(-NH_2)-(-OH)(=[::60]O)))}
11.3 Glucose

The goal here is to represent the glucose molecule according to several different conventions.

11.3.1 Skeleton diagram

The code here looks like that of 2-amino-4-oxohexanoic acid. This gives almost the same structure with absolute angles, except here the default angle is \(-30\degres\):

Compiled output

Backbone
\chemfig{[:-30]HO--[:30]--[:30]--[:30]-H}

Adding the branches is no problem. We use predefined absolute angles:

Compiled output

Glucose, skeleton diagram
\chemfig{[:-30]HO--[:30](<[2]OH)-(<:[6]OH)
-[:30](<:[2]OH)-(<:[6]OH)-[:30](=[2]O)-H}
11.3.2 Fisher projection

The goal is to get the molecule below:

Compiled output

The idea is to begin to draw the longest chain vertically by giving a default angle of “[2]”. Here is the skeleton, where we have added lower case letters at the end of each vertical bond:

Compiled output

Skeleton
\chemfig{[2]OH-[3]-a-b-c-d-=[1]O}

Next we define two aliases for the horizontal bonds and the atoms at their ends. Lets choose “x” which we will put in place of the lower case a, c and d, and “y” which will replace the letter c. Since these alias are just one character, we do not need braces and can write “!x” instead of “!{x}”:

Compiled output

Glucose (Fisher projection)
\definesubmol{x}{(-[4]H)(-[0]OH)}
\definesubmol{y}{(-[0]H)(-[4]OH)}
\chemfig{[2]OH-[3]-!x-!x-!y-!x-=[1]O}
11.3.3 “Chair” representation

We will depict the \(\alpha\)-D-glucose molecule:

Compiled output

To do this, we will first of all draw five sides of the chair and link the first vertex to the last with a hook “?”. We will use the following absolute angles, running counterclockwise: \(-50\degres\), \(10\degres\), \(-10\degres\), \(130\degres\), \(190\degres\).

Compiled output

Structure
\chemfig{?-[:-50]-[:10]-[:-10]-[:130]O-[:190]?}

Now we simply add the branches inside parentheses. The angles are chosen to give the best impression of perspective, and some bonds are shortened by a factor of 0.7:

Compiled output

Chair representation
\chemfig{?(-[:190]OH)-[:-50](-[:170]OH)-[:10](-[:-55,0.7]OH)
-[:-10](-[6,0.7]OH)-[:130]O-[:190]?(-[:150,0.7]-[2,0.7]OH)}
11.3.4 Haworth projection

The goal is to depict this D-glucopyranose molecule:

Compiled output

First of all we will choose the longest chain, which starts at the “HO” group on the left and continues through fives sides of the ring. The ring will be closed with a hook. For the vertical bond which leaves from the first “HO” group, we need to specify that it will leave from the second atom using the optional argument. Furthermore, it will be shortened with a coefficient of 0.5. Its optional argument will thus be “[2,0.5,2]”.

Next, to give the impression of perspective to the ring, the diagonal bonds will be shortened by a coefficient of 0.7. For the bold diagonal lines we will use Cram bonds, having redefined the base of the triangles to be 2pt. The bold horizontal bond needs to be drawn with a thickness of 2pt, and so its optional argument will be “[0,,,,line width=2pt]”. Here is the skeleton of the molecule:

Compiled output

Structure
\chemfig[cram width=2pt]{HO-[2,0.5,2]?<[7,0.7]-[,,,,
line width=2pt]>[1,0.7]-[3,0.7]O-[4]?}

All that needs to be done now is to add the branches at the correct places, giving the right absolute angles and sometimes reducing the length to better give the illusion of perspective:

Compiled output

Projection de Haworth
\chemfig[cram width=2pt]{HO-[2,0.5,2]?<[7,0.7](-[2,0.5]OH)-[,,,,
line width=2pt](-[6,0.5]OH)>[1,0.7](-[6,0.5]OH)-[3,0.7]
O-[4]?(-[2,0.3]-[3,0.5]OH)}
11.4 Adrenaline

We want to draw the adrenaline molecule:

Compiled output

We are going to use two different methods.

11.4.1 Using one ring

First of all, we start with a 6-ring and we draw the start of the branches which leave it:

Compiled output

Skeleton of adrenaline
\chemfig{*6((-HO)-=-(-)=-(-HO)=)}

The branch on the right still needs to be completed using, for example, relative angles:

Compiled output

Adrenaline, step two
\chemfig{*6((-HO)-=-(--[::-60]-[::-60]
HN-[::+60]CH_3)=-(-HO)=)}

Then we need to add a Cram-bonded OH and indicate that the bond which arrives at “HN” does so on the second atom, i.e., “N”. We use the fourth optional argument of the bond:

Compiled output

Adrenaline
\chemfig{*6((-HO)-=-(-(<[::60]OH)-[::-60]-[::-60,,,2]
HN-[::+60]CH_3)=-(-HO)=)}
11.4.2 Using two rings

This method is less natural, but the goal is to show here how to make a bond invisible.

We could improve this code by considering that the drawing of the adrenaline molecule is made of two 6-rings adjacent to each other:

Compiled output

Adrenaline, two-ring skeleton
\chemfig{*6((-HO)-=*6(--HN---)-=-(-HO)=)}

Now the first two bonds of the ring on the right need to be made invisible. To do this we use the argument that is passed to TikZ, specifying “draw=none”. These bonds will thus have this code: “-[,,,,,draw=none]”. To keep the code readable, we define an alias named “&” for these bonds:

Compiled output

Adrenaline, step two
\definesubmol{&}{-[,,,,draw=none]}
\chemfig{*6((-HO)-=*6(!&!&HN---)-=-(-HO)=)}

The rest becomes easy; just add the branches to the right vertices:

Compiled output

Adrenaline, step three
\definesubmol{&}{-[,,,,draw=none]}
\chemfig{*6((-HO)-=*6(!&!&HN(-CH_3)--(<OH)-)-=-(-HO)=)}

To finish, we specify that the bonds that arrive at and leave from “HN” must do so at the second atom. We therefore define another alias for the invisible bond which arrives at “HN”:

Compiled output

Adrenaline
\definesubmol{&}{-[,,,,draw=none]}
\definesubmol{&&}{-[,,,2,draw=none]}
\chemfig{*6((-HO)-=*6(!&!{&&}HN(-CH_3)-[,,2]-(<OH)-)-=-(-HO)=)}
11.5 Guanine

We will draw the guanine molecule:

Compiled output

First of all, let’s begin by drawing the nested rings, putting just the nitrogen atoms at the vertices:

Compiled output

Guanine, skeleton
\chemfig{*6(=N-*6(-N-=N)=--N-)}

Then we can draw the horizontal bond in the right ring with a hook. We will also place a hydrogen atom under the nitrogen atom of the 5-ring with the command \chembelow{N}{H}. We also need to write “HN” instead of “N” at the vertex at the upper left of the molecule:

Compiled output

Guanine, step two
\chemfig{*6(=N-*6(-\chembelow{N}{H}-=N?)=?--HN-)}

We note that one bond leaves from the wrong atom2! The automatic calculation mechanism must be corrected so that the bond leaves from the second atom “N” instead of the first. To do this we give an optional argument for the last bond of the first 6-ring “[,,2]”:

Compiled output

Guanine, step three
\chemfig{*6(=N-*6(-\chembelow{N}{H}-=N?)=?--HN-[,,2])}

Simply add the branches to the right vertices. Note especially the branch leaving the first vertex of the first 6-ring “(-N_2N)”:

Compiled output

Guanine
\chemfig{*6((-H_2N)=N-*6(-\chembelow{N}{H}-=N?)=?-(=O)-HN-[,,2])}

We could also draw the same molecule with a regular 5-ring, as is sometimes done:

Compiled output

Guanine with 5-ring
\chemfig{*6((-H_2N)=N-*5(-\chembelow{N}{H}-=N-)=-(=O)-HN-[,,2])}

1 This was also true for the preceding method with one ring.

2 This seems illogical because the angle of the bond from the HN group toward the first vertex lies between \(-90\degres\) and \(90\degres\); chemfig should therefore leave from the second atom. To explain this contradiction, one must know that in rings, the last bond always links the last vertex to the first, ignoring the calculated theoretical angle of this bond (which here is \(-90\degres\)). chemfig uses this theoretical angle to determine the departure and arrival atoms, but does not use it to draw the bond because the two ends are already defined. The departure atom for the last bond is thus the first atom.