5

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

MWE

\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usegdlibrary{force}
\begin{document}
    \begin{tikzpicture}
    \begin{scope}[spring layout, node distance=10cm, every edge/.style={draw, very thick, -latex}, every node/.style={minimum width=6cm, minimum height=5cm, draw}]
    \foreach \i in {FA,CA,IFA,IFA3OS,DHCA,DHFA,DHIFA,DHCA3OG,DHFA4OS,DHIFA3OG,IFA3OG,DHFA4OG,FG,IFA3OS,FA4OS,5OFQA,5OCQA,4OCQAL,CA3OS,CA4OS,DHCA3OS} \node (\i) {};
    \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);
    \end{scope}
    \end{tikzpicture}
\end{document}

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
4

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.

\RequirePackage{luatex85}
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{graphdrawing,graphs}
\usegdlibrary{layered}
\begin{document}
\begin{tikzpicture}
  \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 };
  };
\end{tikzpicture}
\end{document}

layered graph

Changing

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

to

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.

  • 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
2

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

\RequirePackage{luatex85}
\documentclass[margin=5mm]{standalone}
\usepackage{tikz,chemfig}
\renewcommand*\printatom[1]{\ensuremath{\mathsf{#1}}}
\newcommand*{\HOOC}[2]{HOOC-[1]-[2]-[1]*6(=-=(-#1)-(-#2)=-)}
\newcommand*{\radicals}[2]{\edef\X{#1}\edef\Y{#2}}
\definesubmol{hooc}{HOOC-[1]-[2]-[1]*6(=-=(-!{\X})-(-!{\Y})=-)}
\definesubmol{isohooc}{HOOC-[1]=[2]-[1]*6(=-=(-!{\X})-(-!{\Y})=-)}
\definesubmol{quinic}{HOOC-[7]?(-[6]OH)-[:15]-[:-15](-[6]OH)-[:60](-[:-30]OH)-[:195](-[:60,3]O-[7](=[6]O)-[1]=[2]-[1]*6(=-=(-!{\X})-(-!{\Y})=-))-[:165]?}
\def\glucuronide{O-[:30](-[:60](-[:190,0.75]OH)-[:-15](-[:75,0.75]OH)-[:15]?-[:-15,0.75]OH)(-[:15]O-[::-30]?-[0,0.75]COOH)}
\setatomsep{2em}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{layered,force}
\begin{document}
    \begin{tikzpicture}[label position=below,
                        every label/.style={red},
                        >=latex,
                        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},
                                                    /tikz/4OCQAL/.style={},
                                                    /tikz/DHIFA3OG/.style={},
                                                    /tikz/IFA/.style={},
                                                    /tikz/CA/.style={},
                                                    /tikz/DHCA/.style={}
                        },
                        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/IFA/.style={},
                        /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},
                    DHCA3OG,
                    DHCA3OS,
                    DHIFA -> DHIFA3OG[DHIFA3OG]},
           CA4OS,
           CA3OS,
           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)
    {\radicals{OH}{OH}\chemfig*{!{hooc}}};

    \node[label={Dihydrocaffeic acid-3-O-glucuronide}] at (DHCA3OG)
    {\radicals{OH}{\glucuronide}\chemfig*{!{hooc}}};

    \node[label={Dihydroferulic acid-4-O-sulfate}] at (DHFA4OS)
    {\radicals{OSO_3^{-}}{OCH_3}\chemfig*{!{hooc}}};

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

    \node[label={Dihydro(iso)ferulic acid-3-O-glucuronide}] at (DHIFA3OG)
    {\radicals{OCH_3}{\glucuronide}\chemfig*{!{hooc}}};

    \node[label={Dihydro(iso)ferulic acid}] at (DHIFA)
    {\radicals{OCH_3}{OH}\chemfig*{!{hooc}}};

    \node[label={Isoferulic acid-3-O-glucuronide}] at (IFA3OG)
    {\radicals{OCH_3}{\glucuronide}\chemfig*{!{isohooc}}};

    \def\glucuronide{O-[:-30,1.5](-[:60](-[:190,0.5]OH)-[:-15](-[:75,0.75]OH)-[:15]?-[:-15,0.75]OH)(-[:15]O-[::-30]?-[0,0.75]COOH)}
    \node[label={Dihydroferulic acid-4-O-glucuronide}] at (DHFA4OG)
    {\radicals{\glucuronide}{OCH_3}\chemfig*{!{hooc}}};

    \node[label={Dihydroferulic acid}] at (DHFA)
    {\radicals{OH}{OCH_3}\chemfig*{!{hooc}}};

    \node[label={Caffeic acid-4-O-sulfate}] at (CA4OS)
    {\radicals{OSO_3^{-}}{OH}\chemfig*{!{hooc}}};

    \node[label={Caffeic acid-3-O-sulfate}] at (CA3OS)
    {\radicals{OCH_3}{OSO_3^{-}}\chemfig*{!{hooc}}};

    \node[label={Ferulic acid-4-O-sulfate}] at (FA4OS)
    {\radicals{OSO_3^{-}}{OCH_3}\chemfig*{!{isohooc}}};

    \node[label={Feruloglycine}] at (FG)
    {\chemfig*{HOOC-[:60]-[:20]NH-[:80](=[:120]O)(-[1]=[2]-[1]*6(=-=(-OH)-(-OCH_3)=-))}};

    \node[label={5-O-Feruloyquinic acid}] at (5OFQA)
    {\radicals{OH}{OCH_3}\chemfig*{!{quinic}}};

    \node[label={5-O-Caffeoylquinic acid}] at (5OCQA)
    {\radicals{OH}{OH}\chemfig*{!{quinic}}};

    \node[label={4-O-Caffeoylquinic acid lactone}] at (4OCQAL)
    {\chemfig*{?[a](=[:135]O)-[:280,2.5]?[b](-[6]OH)-[:15]-[:-15](-[6]OH)-[:60](-[:-30]O-[7](=[6]O)-[1]=[2]-[1]*6(=-=(-OH)-(-OH)=-))-[:195](-[:60]O?[a])-[:165]?[b]}};
    \end{tikzpicture}
\end{document}

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 :)

  • 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.