I'm trying to redesign the below (ugly) picture using LaTeX with tikz and chemfig. I already can draw the chemical molecules that's no problem, I was placing them manually but then I though maybe the graphdrawing library could do that for me.

enter image description here

Of couse drawing everything on one go gave some erros (totally understandable). So I thought I could draw empty nodes sized like "frames" for these molecules, and have Lua position them, then I just draw the chemical molecules wherever the nodes were placed. The problem is that nodes distance does not behave as expected (or does it?). Setting node distance=4cm I expected to have nodes spreaded with at least 4cm distance between them...

Instead I set it to 10cm and get this:

enter image description here


    \begin{scope}[spring layout, node distance=10cm, every edge/.style={draw, very thick, -latex}, every node/.style={minimum width=6cm, minimum height=5cm, draw}]
    \draw   (CA)    edge    (FA)
                    edge    (DHCA)
                    edge    (IFA)
                    edge    (CA4OS)
                    edge    (CA3OS)
            (4OCQAL)edge    (CA)
                    edge    (5OFQA)
            (5OCQA) edge    (CA)
            (FA)    edge    (FA4OS)
                    edge    (FG)
                    edge    (DHFA)
            (5OFQA) edge    (FA)
            (DHCA)  edge    (DHFA)
                    edge    (DHCA3OG)
                    edge    (DHCA3OS)
                    edge    (DHIFA)
            (IFA)   edge    (IFA3OG)
                    edge    (IFA3OS)
            (DHFA)  edge    (DHFA4OG)
                    edge    (DHFA4OS)
            (DHIFA) edge    (DHIFA3OG);

Unrelated: another problem I'm encountering is the edges, even though they are specified with -> the arrow is not being drawn... :/

  • Er ... isn't an arrow what -> means? What did you think it meant? – cfr Dec 13 '16 at 23:05
  • Does this compile for you? I get an error: graphdrawing/lua/pgf/gd/interface/InterfaceToDisplay.lua:272: node already crea ted stack traceback: [C]: in function 'assert' ...graphdrawing/lua/pgf/gd/interface/InterfaceToDisplay.lua:272: in function ' createVertex' [\directlua]:1: in main chunk. \pgf@positionnodelater@macro ...) pgf.temp = nil } } l.148 ...,4OCQAL,CA3OS,CA4OS,DHCA3OS} \node (\i) {}; – cfr Dec 13 '16 at 23:08
  • 1
    Note that node distance means something different in graphs and something different, if anything, to different algorithms. But it generally will be the distance between node anchors, which are .center by default. – cfr Dec 14 '16 at 0:08
  • @cfr there's an implicit not before the "being" I don't know how you missed it... ;P Hmm, I was wondering if that was the case. But for some reason I expected node distance to be between the borders. And yes, the code compiled (it gave me this error once after I tried to use random seed but I cleaned the aux files and it compiled again). – Guilherme Zanotelli Dec 14 '16 at 5:32
  • Strange it compiled for you. I tried the cod as is. – cfr Dec 14 '16 at 22:31

As it stands, your example will not compile because the node names are not unique. Probably, you want to use the \graph command and create the nodes implicitly. When you repeat a name, only one node will be created but it may, of course, have many relationships to other nodes.

It is important to choose an appropriate algorithm and to then use the options relevant to that algorithm to adjust the layout. node distance is right for some cases, but not all.

Here's one version with a layered layout which uses sibling distance and level distance to enforce the desired spacing.

  \graph [layered layout, sibling distance=10cm, level distance=10cm, edges={draw, very thick}, nodes={minimum width=6cm, minimum height=5cm, draw, as=}]
    CA -- { FA, DHCA --  { DHFA, DHCA3OG -- { DHFA4OG, DHFA4OS }, DHCA3OS, DHIFA  -- DHIFA3OG }, IEA -- { IFA3OG, IFA3OS }, CA4OS, CA3OS, 4OCQAL -- 5OFQA -- FA -- { FA4OS, FG, DHFA }, 5OCQA };

layered graph


layered layout, sibling distance=10cm, level distance=10cm, 


spring layout, node distance=20cm,

we have

spring layout

The cooling factor, convergence tolerance etc. will also influence the result in this case.

Or change the above to

spring electrical layout, node distance=10cm, electric charge=5, 

to produce

spring layout with extra electric charge

Or without the increased electric charge:

spring layout with default electric charge

In case you meant that you wanted arrows, replace -- with -> or <- or <-> and set the arrow tip using >=Latex or whatever.

| improve this answer | |
  • I was able to figure out the \graph thing a while after but went to sleep and didn't update the MWE, still I could not get the graph to look nice... And I think they're too spreaded the spring layouts, the layered doesn't seem to capture the work flow of the picture. I'll try to mess with the cooling factor you mentioned. Thanks! – Guilherme Zanotelli Dec 14 '16 at 6:45
  • Yep, now everything is functional! Thank's a lot for the tips. I'm browsing tha manual for more details on how to fine tune the final graph but it's no simple thing, unfortunately. :/ If it's of any interest, I put the results in the question. It may end up interesting someone else. – Guilherme Zanotelli Dec 14 '16 at 19:45
  • They don't seem to be there, but that's good. Post them as an answer if you've found something useful ;). And, no, it is no simple thing. But the spring with electric charge seemed to do better than those without. – cfr Dec 14 '16 at 22:33
  • Indeed I forgot to post the code. There it is now (posted as an answer, seems more like it). The electrical layout is better than non electrical, but I thought the layered layout to be more understandable though. – Guilherme Zanotelli Dec 14 '16 at 22:45

Ok, using @cfr's suggestions I implemented the layered layout and spring electrical layout using some tricks for the electrical layout. To change between layouts just change the style use layers to spreaded and uncomment the line

% ,O[the central] --[draw=none] {CA,DHCA,FA,DHFA}% For spreaded layout

Result with layered layout

enter image description here

Result with spring electrical layout

enter image description here

The code

    \begin{tikzpicture}[label position=below,
                        every label/.style={red},
                        rounded corners,
                        every edge/.append style={line width=6pt},
                        graphs/use layers/.style={layered layout,
                                                    sibling distance=8cm,
                                                    level distance=10cm,
                                                    /tikz/the central/.style={electric charge=-1.2},
                        graphs/spreaded/.style={spring electrical layout, node distance=11cm,
                        /tikz/the central/.style={electric charge=-1.2},
                        /tikz/4OCQAL/.style={node distance=15cm},
                        /tikz/DHIFA3OG/.style={orient=up, orient tail=DHIFA},
                        /tikz/CA/.style={electric charge=1.5, orient=up, orient tail=DHFA},
                        /tikz/DHCA/.style={orient=left, orient tail=FA}
    \graph[use layers, nodes={circle, minimum size=6cm, as=}]{%
    CA[CA] <- {4OCQAL[4OCQAL] -> 5OFQA -> FA, 5OCQA},                       
    CA -> {FA -> {FA4OS, FG, DHFA},
           DHCA[DHCA] -> {DHFA -> {DHFA4OG, DHFA4OS},
                    DHIFA -> DHIFA3OG[DHIFA3OG]},
           IFA[IFA] -> {IFA3OG, IFA3OS}
%   ,O[the central] --[draw=none] {CA,DHCA,FA,DHFA}% For spreaded layout
    \node[label={Ferulic acid}] at (FA) {\radicals{OH}{OCH_3}\chemfig*{!{isohooc}}};

    \node[label={Caffeic acid}] at (CA) {\radicals{OH}{OH}\chemfig*{!{isohooc}}};

    \node[label={Isoferulic acid}] at (IFA) {\radicals{OCH_3}{OH}\chemfig*{!{isohooc}}};

    \node[label={Isoferulic acid-3-O-sulfate}] at (IFA3OS) {\radicals{OSO_3^{-}}{OH}\chemfig*{!{isohooc}}};

    \node[label={Dihydrocaffeic acid}] at (DHCA)

    \node[label={Dihydrocaffeic acid-3-O-glucuronide}] at (DHCA3OG)

    \node[label={Dihydroferulic acid-4-O-sulfate}] at (DHFA4OS)

    \node[label={Dihydrocaffeic acid-3-O-sulfate}] at (DHCA3OS)

    \node[label={Dihydro(iso)ferulic acid-3-O-glucuronide}] at (DHIFA3OG)

    \node[label={Dihydro(iso)ferulic acid}] at (DHIFA)

    \node[label={Isoferulic acid-3-O-glucuronide}] at (IFA3OG)

    \node[label={Dihydroferulic acid-4-O-glucuronide}] at (DHFA4OG)

    \node[label={Dihydroferulic acid}] at (DHFA)

    \node[label={Caffeic acid-4-O-sulfate}] at (CA4OS)

    \node[label={Caffeic acid-3-O-sulfate}] at (CA3OS)

    \node[label={Ferulic acid-4-O-sulfate}] at (FA4OS)

    \node[label={Feruloglycine}] at (FG)

    \node[label={5-O-Feruloyquinic acid}] at (5OFQA)

    \node[label={5-O-Caffeoylquinic acid}] at (5OCQA)

    \node[label={4-O-Caffeoylquinic acid lactone}] at (4OCQAL)

Some things still need to be taken care of, but the main idea is there. Hope it can be of help to some chemist or whoever :)

| improve this answer | |
  • Yes, I agree about layered versus spring. I find the spring layouts the most difficult to understand. – cfr Dec 14 '16 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.