Take the 2-minute tour ×

I'm interested in drawing molecular connectivity graphs in TikZ. Using \node and child without any positions I am able to draw a tree diagram which is semi-useful as I can easily illustrate the hierarchical relationships of the implied z-matrix.

Here's an example for ethanol (CH3CH2OH) that I've been using in my dabblings.

\node[carbon] (C1) {C}
child {node [hydrogen] (H1) {H}}
child {node [hydrogen] (H2) {H}}
child {node [hydrogen] (H3) {H}}
child {node [carbon] (C2) {C}
child {node [hydrogen] (H4) {H}}
child {node [hydrogen] (H5) {H}}
child {node [oxygen] (O1) {O} child {node [hydrogen] (H6) {H}}}};
\draw [dashed] (H6) -- (H4);

A program like Graphviz, however, can take a similar graph and arrange the nodes automagically in a number of ways. The neato package in Graphviz can for instance arrange the nodes in a relaxed ball-and-spring fashion that is rather fitting for molecules. I've been looking through the TikZ documentation and have found mindmaps, petri nets, finite state automata, digraphs etc., however I have not seen any invocation of automatic layouts. Can it be done in TikZ?

share|improve this question
I would be very surprised to learn that this had been done. I don't know much about the algorithms involved, but I think that graphviz does a lot of work to figure out the placement of the nodes and implementing that in TeX would be quite a feat. It is possible to take the output of graphviz and feed it in to TikZ using, for example dot2tex: fauskes.net/code/dot2tex/documentation –  Loop Space Feb 8 '11 at 12:21
@Andrew Stacey: On balance, dot2tex looks like a very good solution. Thanks. Optimal node placement does seem like a fairly gnarly problem, especially for dense graphs. Combinatorial explosions ahoy. –  Richard Terrett Feb 8 '11 at 13:28

3 Answers 3

up vote 9 down vote accepted

You are better with the excellent »chemfig« package. It is based on »PGF/TikZ« and offers an easy syntax.




The package manual has the details.

enter image description here

share|improve this answer
This package looks extremely useful and I thank you for bringing it to my attention, however it also seems to require explicit positioning of nodes/atoms. –  Richard Terrett Feb 8 '11 at 11:37
But it's much easier than with any other related package I know. Sometimes you have to compromise. And I'm not aware of any solution with full automatism. –  Thorsten Donig Feb 8 '11 at 11:41

Automatic (or algorithmic, as we call it in the manual) graph drawing is a feature that is currently being worked on in the PGF/TikZ repository. It also includes a standard set of algorithms similar to what GraphViz provides (some ideas are derived from the same research papers even). This feature is far from stable but will hopefully be finished sometime in 2011.

share|improve this answer
@ Jannis Pohlmann - This is good news, thanks for the notification. –  Richard Terrett May 19 '11 at 11:07
I've mostly finished my work on graph drawing with TikZ. Neato and dot like placements are available in the repository on CVS. The results are presented on bit.ly/o6WF5y including a link to the PDF version of my thesis, which explains the new TikZ graph syntax, its Lua-based graph drawing engine and the spring/spring-electrical and layered drawing algorithms that were implemented. –  Jannis Pohlmann Oct 18 '11 at 15:04
So is it usable now? –  Thomas Ahle Mar 28 at 12:55

I'm late to the party, but you can also check out dot2tex. It's far from perfect, but so is Graphviz. (You can use it with either Dot or Neato.)

You call it something like:

dot2tex --preproc --usepdflatex --nominsize --prog neato graph.dot \
  | dot2tex -o graph.tex --figonly --nominsize --prog neato

and it gives you back a file that uses PGF which you can \include in your document.

share|improve this answer

Your Answer


