# Chemical structures

I have just spent a couple of days working out how best to do molecular structures within LaTeX. I want to do it programmatically rather than importing an encapsulated postscript (EPS) file because it’s just so much better. For this reason, and others, I do like the tikz package, an interface to PGF a lot (there’s all manner of reasons given in the documentation (PDF file) as to why LaTeX-created pictures are better).

I tried to draw the structures using tikz, but it’s tedious, and not ideal — I see no easy way to draw stereochemical bonds, for a start. I looked elsewhere.

I found the XyMTeX package, which looks horrendous to use. I also discovered it’s obsolete, even though it’s only from 2009. Still, I’m not surprised with that level of excess complexity. I looked elsewhere.

I found the chemstruct package, which doesn’t seem to be what I want. I looked elsewhere.

Eventually, I came across chemfig. At first glance, this seems exactly what I want: it’s built on top of the tikz package, for a start — hooray! — and it seems reasonably straightforward to use. Something like thisis always going to be complicated due to the nature of the beast, but we’ll see.

## Example

\documentclass[a4paper,12pt]{article}
\usepackage{chemfig}% Includes the tikz package

\usetikzlibrary{calc}% Needed for coordinate calculation

\begin{document}

\begin{tikzpicture}
\def\xa{0}
\def\xb{5}
\node (mol1) at (\xa,0) {\chemname{\chemfig{C(<[:250]Cl)(-[:330]Cl)(<:[:190]Cl)(-[:90]H)}}{trichloromethane}};
\end{tikzpicture}

\end{document}

## Update 1

There is a conflict between the chemfig package as provided by TeXlive and the gb4e package, which does example numbering for linguistics. This has probably never come up before — who draws chemical structures in the same document as linguistic examples?

I suspect the problem is that both packages mess with the mathematical subscripts; they both seem to allow roman fonts in subscripts, but put the two together, and you get italicised digits in your chemical formulae. This is not right!

Changing the order the packages are declared in makes no difference. I wonder what, if anything, the solution is. I am so disappointed!

## Update 2

Problem solved! Using the version of the gb4e package provided by MiKTeX solves the problem! (MiKTeX is available on Windows and Linux.)

I’m now happy with my chemical structures. They’re easy to draw and they look good.

One problem, however, is that I can’t name individual atoms anymore, which would be useful to do for annotations and so on. However, if I put the \chemfig{} command in a \node command, I can use the node’s generic anchor points, which will do for my current needs.

## 5 thoughts on “Chemical structures”

1. Yes! I’ve tried many packages, some listed here and I totally agree with you, chemfig is the best package ever! Only one issue: I’m curious to know if you worked out how to draw for example an aromatic ring with a circle of whatsoever to show the aromaticity and a charge, for example a + in the center. Thank you in advance for your help!!!

1. Thanks for your comment, Luigi!

I’ve just skimmed through the documentation (http://anorien.csc.warwick.ac.uk/mirrors/CTAN/macros/latex/contrib/chemfig/chemfig_doc_en.pdf). On p.70, there are some examples of aromatic rings with the aromaticity shown as circles. On p.80, there’s an example of rings with charges; I’m not sure this is exactly what you want, but the author does invite suggestions, though (see p.71), so if that isn’t suitable, perhaps you could email him.

2. Due to an error with the template, the comments weren’t showing up. Sorry, Luigi – I wasn’t ignoring you! We were being failed by the technology. All fixed now, as you can see.

3. Great post. What are your thoughts on ppchtex? Also, is there a way to generate chemfig code from say SMILES — that would be really useful, right?

1. Hi Karthik, thanks for your comment!

I can’t say I’ve used ppchtex, but I’ve just skimmed a little of the documentation, and it looks like it does what Luigi wants with the charge in the centre of aromatic rings.

http://tex.loria.fr/graph-pack/ppchtex.pdf

I don’t know of any way to convert SMILES into chemfig code, although you are right, it would be really useful. I just did a quick google search and found a couple of posts on a blog about support for SMILES. I see from the comments that you’ve beaten me to it 😉

http://baoilleach.blogspot.co.uk/2012/04/cheer-up-your-latex-with-smiles-support.html

This site uses Akismet to reduce spam. Learn how your comment data is processed.