Featured Book

LaTeX Beginners Guide

Supporters and Partners

Sponsored by DANTE e.V.: The German speaking TeX Users Group
TeXnique.fr - questions & réponses sur LaTeX
TeXwelt - Fragen und Antworten zu LaTeX
LaTeX and Chemistry - Drawing Reaction Schemes PDF Print E-mail
(10 votes, average: 4.90 out of 5)
LaTeX - Math & Science
Written by Clemens Niederberger   
Sunday, 17 June 2012 19:42

The start

Let's see, what would I be talking about that's related to graphics? Well – almost everything I do, TeXnically speaking, is somehow related to chemistry. And in chemistry you have all kinds of graphics: potential energy curves, Haegg-diagrams, molecular orbital diagrams and of course all the sceletal formulas and reaction schemes common in (not only) organic chemistry.

The standard diagrams are not too complicated but are maybe worth an article in my blog. Molecular orbital diagrams are easily done with the package »MOdiagram«. So what's left? Sceletal formulas? They're easy if you glanced in the documentation of »ChemFig«. Well – most of them, anyway.

What about reaction schemes? Ever created one with LaTeX? No? Then it's time! And »ChemFig« has just the tools for this.

- A contribution to the LaTeX and Graphics contest -

»ChemFig« and reaction schemes

A note before I start: all the examples later use the packages chemfig and chemmacros and should run with a preamble like follows and an up to date TeX distribution.



An easy start

In my experience the scheming mechanisms of »ChemFig« are still rather unknown although they're also not too complicated once you've mastered the one command:


The last option reminds us: »ChemFig« uses TikZ to draw molecules and schemes. I will also make use of TikZ but I won't explicitly explain TikZ code. This is about »ChemFig« after all. Back to \arrow. All of its arguments are optional. So the basic use is very intuitive again:

First scheme

Creating something like this poses not a big challenge, then:

Second scheme
  2 \chemfig{*6(=-=(-(=[2]O)-[::-60]\lewis{0.,O})-=-)}
  2 \chemfig{*6(=-=(-[,.15,,,draw=none]\lewis{0.,})-=-)}\+\ch{2 CO2 ^}

Get more sophisticated

But what if the scheme gets bigger, maybe has branches or something? Or is educational like this one:

Third scheme

The code looks as follows. Let's decrypt it step by step:

 \definearrow{0}{..}{\draw[dotted] (\CF@arrow@start@node)--(\CF@arrow@end@node);}
 \chemfig{[6]Me-(!\conf{\text{\tiny S}})(-[0]Br)(-[4]H)-(!\conf{\text{\tiny S}})(-[0]H)(-[4]Cl)-Me}
 \arrow(a--b){<->[\tiny enantiomers]}[,2,line width=.4pt]
 \chemfig{[6]Me-(!\conf{\text{\tiny R}})(-[0]H)(-[4]Br)-(!\conf{\text{\tiny R}})(-[0]Cl)(-[4]H)-Me}
 \arrow{<->[*0\tiny diastereomers]}[-90,2,line width=.4pt]
 \chemfig{[6]Me-(!\conf{\text{\tiny R}})(-[0]H)(-[4]Br)-(!\conf{\text{\tiny S}})(-[0]H)(-[4]Cl)-Me}
 \arrow(c--d){<->[\tiny enantiomers]}[180,2,line width=.4pt]
 \chemfig{[6]Me-(!\conf{\text{\tiny S}})(-[0]Br)(-[4]H)-(!\conf{\text{\tiny R}})(-[0]Cl)(-[4]H)-Me}
 \arrow{<->[*0\tiny diastereomers]}[90,2,line width=.4pt]
 \arrow(@a--@c){<->}[,,line width=.4pt]
 \arrow(@b--@d){<->}[,,line width=.4pt]
 \arrow(@a--xx){0}[,1]{}\arrow{0}[-90,.5] \tiny diastereomers
 \arrow(@xx--){0}[90,1]{}\arrow{..}[-90,5.5] mirror plane

We'll ignore the first few lines for the moment and we'll also ignore the formulas. Then we have this:

 upper left compound
 \arrow(a--b){<->[\tiny enantiomers]}[,2,line width=.4pt]

The first argument (a--b) names the node of the "upper left compound" a and the one following the arrow b. We'll see later what's that for. The second argument {<->[\tiny enantiomers]} chooses the type <-> and a label that should be written above the arrow. The third argument [,2,line width=.4pt] multiplies the length of the arrow by 2 and adds a TikZ style for the line width.

 upper right compound
 \arrow{<->[*0\tiny diastereomers]}[-90,2,line width=.4pt]

This is almost the same. There are two differences, though: 1) the last argument [-90,2,line width=.4pt] tells the arrow to go down (angle = -90). 2) The optional argument of the arrow type begins with *0 which means that the label should be rotated by 0 degrees. Otherwise it would be rotated together with the arrow.

There's nothing new in the next four lines:

 lower right compound
 \arrow(c--d){<->[\tiny enantiomers]}[180,2,line width=.4pt]
 lower left compound
 \arrow{<->[*0\tiny diastereomers]}[90,2,line width=.4pt]

But then it gets more interesting:

 \arrow(@a--@c){<->}[,,line width=.4pt]
 \arrow(@b--@d){<->}[,,line width=.4pt]

These are the diagonal arrows. Now we can profit from the fact that we named the compounds. (@a--@c) connects the upper left with the lower right compound, for example. You can see that you use the names you've given with an @ before it. »ChemFig« names the compounds itself, by the way. If you don't choose names, they're named c1, c2 and so forth. To help you not to lose track you can visualize the names, the nodes the compounds are in and a few additional information by saying \schemedebug{true}.

In the last lines we do some invisible things. The arrow type 0 is an invisible arrow. We use it to add an inscription above the crossed arrows in the center of the scheme: go left by 1 arrow length from compound a, name the empty compound xx, then go down by half an arrow length:

 \arrow(@a--xx){0}[,1]{}\arrow{0}[-90,.5] \tiny diastereomers
 \arrow(@xx--){0}[90,1]{}\arrow{..}[-90,5.5] mirror plane

In line 18 we go up from the empty compound xx and use the arrow type we've defined in lines 2–4 to draw the dotted line indicating the mirror plane. Yes, that's true: you can define your own arrow types. In this case it is a fairly simple one:

 \definearrow{0}{..}{\draw[dotted] (\CF@arrow@start@node)--(\CF@arrow@end@node);}

An arrow type with 0 optional arguments whose symbol is .. and simply draws a dotted line from the start to the end \draw[dotted] (\CF@arrow@start@node)--(\CF@arrow@end@node);.

Get colorful

Want more? Let's get educational one more time (that's what I do…) and add some fancy colors (I'm sorry this is in German, but that's my native language and frankly I've been to lazy to translate it):

Fourth scheme

Now, there's some work to do before we can start the actual scheme:

 % chemfig settings:
 % shortcuts for two circled numbers:
 \def\eins{\tikz{\node[orange,draw=orange,circle,inner sep=1pt,text width=1.2ex]{1};}}
 \def\zwei{\tikz{\node[orange,draw=orange,circle,inner sep=1pt,text width=1.2ex]{2};}}
 % we need to define a decoration that we can use for a bond type between
 % single and double, with some delocalized electrons; this is TikZ/pgf,
 % actually:
 % finally some styles:
   info/.style   ={draw=red!50!black,fill=red!25,rounded corners,inner sep=3pt,font=\small},
   Uez/.style    ={left delimiter={[},right delimiter={]},inner sep=3pt},

Since I'm not talking about TikZ here I won't go into detail of this code, I simply added a few comments. You'll have to live with this for now. Here's the code for the scheme:

  % Label an die Bindungen:
  \arrow(@b1--[yshift=-.9em]){<-}[90,.5,draw=orange,shorten <=-.9em]\eins
  \arrow(@b2--[yshift=-.9em]){<-}[90,.5,draw=orange,shorten <=-.9em]\zwei
  % 1.)
  \arrow(@c1--.south west[Uez]){->[\lewis{4:,ba\mch}]}[30]
  \arrow{->[$-$ \ch{baH}][$-$ \ch{Het-}]}
  \arrow(--[info,text width=1.8cm]){0}[,.2]
  {\eins\ und \zwei\ gleichzeitig: \textcolor{red}{\mech[e2]}}
  \arrow(@c4.north east--){0}[20,.1]\transitionstatesymbol
  % 2.)
  \arrow(@c1--){->[$-$ \ch{Het-}]}
  \arrow{->[$-$ \Hpl]}
  \arrow(--[info,text width=2.9cm]){0}[,.2]
  {zuerst \zwei, dann \eins: \textcolor{red}{\mech[e1]}}
  % 3.)
  \arrow(@c1--.north west){->[$+$ \ch{M^+ba-}][$-$ \ch{baH}]}[-30]
  \arrow{->[$-$ \ch{M^+Het-}][][6pt]}
  \arrow(--[info,text width=2.9cm]){0}[,.2]
  {zuerst \eins, dann \zwei: \textcolor{red}{\mech[cb]}}

You'll notice some unfamiliar macros in there, probably, like \delm or \mech. They're from the package »chemmacros« and of no further interest here. Maybe something for another blog post.

Let's pick some lines that tell us something new:

  % Label an die Bindungen:
  \arrow(@b1--[yshift=-.9em]){<-}[90,.5,draw=orange,shorten <=-.9em]\eins

You maybe know that you can name nodes inside a formula created with \chemfig. This is done by adding @{name} at the appropriate place in the formula. We use this to add an arrow with a description to a bond. The arrow starts at the denoted bond b1 \arrow(@b1--[yshift=-.9em]){<-}. The node where it is ending is shifted a little bit down. That's new: you can add TikZ options to the nodes where an arrow starts or ends with an optional argument.


The scheme uses this possibility quite frequently, for example here:

  \arrow(@c1--.south west[Uez]){->[\lewis{4:,ba\mch}]}[30]

This means: start from compound c1, use an angle of 30 degrees and end at the south west corner of the following compound. Use the TikZ style Uez here.

The end

I could add loads and loads of other examples but I think the basics should be clear now. You have seen that »ChemFig's« \arrow is a powerful thing and one can create nice schemes. From my personal experience I want to add: if you're more or less familiar with TikZ and have learned the syntax of the \arrow creating these themes doesn't really take much time and also is fun!

A teaser: rebuilt the one below with »ChemFig« and »chemmacros«! And most importantly: have fun!

Fifth scheme

Happy TeXing!

About the author: Clemens is perhaps better known under the name "cgnieder" with which he frequently visits several LaTeX forums. He is a LaTeX user for about eight years now but only in the last two to three years he began to look a little bit more under the hood. Since then he wrote some mostly chemistry related LaTeX packages and writes about LaTeX and chemistry in his blog www.mychemistry.eu.


Please login to post comments or replies.

Latest Forum Posts

Re: Change thesis geometry
29/09/2016 19:37, cgnieder

Change thesis geometry
29/09/2016 14:11, alexis1704

Re: Problems compiling, math already defined
29/09/2016 10:01, Stefan Kottwitz

Re: Beamer graph comments
29/09/2016 10:00, layuval

Re: Re-starting
29/09/2016 09:58, Stefan Kottwitz

Re: Beamer graph comments
29/09/2016 09:52, Stefan Kottwitz

Beamer graph comments
29/09/2016 09:33, layuval

Re: Changing the default build directory
28/09/2016 23:10, erwann

Problem with chapter's name on bybliography
28/09/2016 21:32, tysh

Re: Problem with table location when margins has been modifi
28/09/2016 11:39, cgnieder