3

Is there a smarter way to achieve the same result as the one from the following MWE:

1) by using some kind of loop instead of hand typing every node,

2) by drawing the arrows with tikz instead of using LaTeX math-mode

\documentclass[border=0.5cm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
  \usetikzlibrary{arrows,
chains,
scopes}

\begin{document}
  \tikzset{>=stealth',
  monomer/.style={
    rectangle, 
    rounded corners, 
    draw=black, very thick,
    text width=1em, 
    minimum height=1.5em, 
    text centered, 
    on chain},
  fleche/.style={
    text width=1.5em, 
    text centered, 
    on chain},
  plus/.style={ 
    text width=1em,
  text centered,
  on chain},
  }

\begin{tikzpicture}
[node distance=.3cm and 0.1cm,
  start chain=going right,scale=0.45]
{ [start chain=vert going below]
  \node[monomer, fill=yellow!55] (G12r) {G};
    { [start branch=first going right]
      \node[monomer, join, fill=yellow!55] (G11r) {G};
      \node[monomer, join, fill=yellow!55] (G10r) {G};       
      \node[monomer, join, fill=yellow!55] (G9r) {G};
      \node[monomer, join, fill=yellow!55] (G8r) {G};        
      \node[monomer, join, fill=yellow!55] (G7r) {G};
      \node[monomer, join, fill=yellow!55] (G6r) {G};
      \node[monomer, join, fill=yellow!55] (G5r) {G};
      \node[monomer, join, fill=yellow!55] (G4r) {G};
      \node[monomer, join, fill=yellow!55] (G3r) {G};
      \node[monomer, join, fill=yellow!55] (G2r) {G};
      \node[monomer, join, fill=yellow!55] (G1r) {G};
      \node[fleche] (A) {$\rightarrow$};
      \node[monomer, fill=yellow!50] (G11p) {G};
      \node[monomer, join, fill=yellow!50] (G10p) {G};       
      \node[monomer, join, fill=yellow!50] (G9p) {G};
      \node[monomer, join, fill=yellow!50] (G8p) {G};        
      \node[monomer, join, fill=yellow!50] (G7p) {G};
      \node[monomer, join, fill=yellow!50] (G6p) {G};        
      \node[monomer, join, fill=yellow!50] (G5p) {G};        
      \node[monomer, join, fill=yellow!50] (G4p) {G};
      \node[monomer, join, fill=yellow!50] (G3p) {G};
      \node[monomer, join, fill=yellow!50] (G2p) {G};
      \node[monomer, join, fill=yellow!50] (G1p) {G};
      \node[plus] (B) {+};  
      \node[monomer] (M) {M};     
    } 
  \node[monomer, fill=yellow!50] (G11r) {G};
    { [start branch=first going right]
     \node[monomer, join, fill=yellow!50] (G10r) {G};
     \node[monomer, join, fill=yellow!50] (G9r) {G};
     \node[monomer, join, fill=yellow!50] (G8r) {G};         
     \node[monomer, join, fill=yellow!50] (G7r) {G};
     \node[monomer, join, fill=yellow!50] (G6r) {G};
     \node[monomer, join, fill=yellow!50] (G5r) {G};
     \node[monomer, join, fill=yellow!50] (G4r) {G};
     \node[monomer, join, fill=yellow!50] (G3r) {G};
     \node[monomer, join, fill=yellow!50] (G2r) {G};
     \node[monomer, join, fill=yellow!50] (G1r) {G};
     \node[fleche] (A) {$\rightarrow$};
     \node[monomer, fill=yellow!45] (G10p) {G};
     \node[monomer, join, fill=yellow!45] (G9p) {G};
     \node[monomer, join, fill=yellow!45] (G8p) {G};         
     \node[monomer, join, fill=yellow!45] (G7p) {G};
     \node[monomer, join, fill=yellow!45] (G6p) {G};         
     \node[monomer, join, fill=yellow!45] (G5p) {G};         
     \node[monomer, join, fill=yellow!45] (G4p) {G};
     \node[monomer, join, fill=yellow!45] (G3p) {G};
     \node[monomer, join, fill=yellow!45] (G2p) {G};
     \node[monomer, join, fill=yellow!45] (G1p) {G};
     \node[plus] (B) {+};  
     \node[monomer] (M) {M};      
   }  
 \node[monomer, fill=yellow!45] (G10r) {G};
   { [start branch=first going right]
     \node[monomer, join, fill=yellow!45] (G9r) {G};
     \node[monomer, join, fill=yellow!45] (G8r) {G};         
     \node[monomer, join, fill=yellow!45] (G7r) {G};
     \node[monomer, join, fill=yellow!45] (G6r) {G};
     \node[monomer, join, fill=yellow!45] (G5r) {G};
     \node[monomer, join, fill=yellow!45] (G4r) {G};
     \node[monomer, join, fill=yellow!45] (G3r) {G};
     \node[monomer, join, fill=yellow!45] (G2r) {G};
     \node[monomer, join, fill=yellow!45] (G1r) {G};
     \node[fleche] (A) {$\rightarrow$};
     \node[monomer, fill=yellow!40] (G9p) {G};
     \node[monomer, join, fill=yellow!40] (G8p) {G};
     \node[monomer, join, fill=yellow!40] (G7p) {G};
     \node[monomer, join, fill=yellow!40] (G6p) {G};         
     \node[monomer, join, fill=yellow!40] (G5p) {G};         
     \node[monomer, join, fill=yellow!40] (G4p) {G};
     \node[monomer, join, fill=yellow!40] (G3p) {G};
     \node[monomer, join, fill=yellow!40] (G2p) {G};
     \node[monomer, join, fill=yellow!40] (G1p) {G};
     \node[plus] (B) {+};  
     \node[monomer] (M) {M};      
   } 
\node[monomer, fill=yellow!40] (G9r) {G};
   { [start branch=first going right]
     \node[monomer, join, fill=yellow!40] (G8r) {G};
     \node[monomer, join, fill=yellow!40] (G7r) {G};
     \node[monomer, join, fill=yellow!40] (G6r) {G};
     \node[monomer, join, fill=yellow!40] (G5r) {G};
     \node[monomer, join, fill=yellow!40] (G4r) {G};
     \node[monomer, join, fill=yellow!40] (G3r) {G};
     \node[monomer, join, fill=yellow!40] (G2r) {G};
     \node[monomer, join, fill=yellow!40] (G1r) {G};
     \node[fleche] (A) {$\rightarrow$};
     \node[monomer, fill=yellow!35] (G8p) {G};
     \node[monomer, join, fill=yellow!35] (G7p) {G};
     \node[monomer, join, fill=yellow!35] (G6p) {G};         
     \node[monomer, join, fill=yellow!35] (G5p) {G};         
     \node[monomer, join, fill=yellow!35] (G4p) {G};
     \node[monomer, join, fill=yellow!35] (G3p) {G};
     \node[monomer, join, fill=yellow!35] (G2p) {G};
     \node[monomer, join, fill=yellow!35] (G1p) {G};
     \node[plus] (B) {+};  
     \node[monomer] (M) {M};      
   } 
\node[monomer, fill=yellow!35] (G8r) {G};
   { [start branch=first going right]
     \node[monomer, join, fill=yellow!35] (G7r) {G};
     \node[monomer, join, fill=yellow!35] (G6r) {G};
     \node[monomer, join, fill=yellow!35] (G5r) {G};
     \node[monomer, join, fill=yellow!35] (G4r) {G};
     \node[monomer, join, fill=yellow!35] (G3r) {G};
     \node[monomer, join, fill=yellow!35] (G2r) {G};
     \node[monomer, join, fill=yellow!35] (G1r) {G};
     \node[fleche] (A) {$\rightarrow$};
     \node[monomer, fill=yellow!30] (G7p) {G};
     \node[monomer, join, fill=yellow!30] (G6p) {G};         
     \node[monomer, join, fill=yellow!30] (G5p) {G};         
     \node[monomer, join, fill=yellow!30] (G4p) {G};
     \node[monomer, join, fill=yellow!30] (G3p) {G};
     \node[monomer, join, fill=yellow!30] (G2p) {G};
     \node[monomer, join, fill=yellow!30] (G1p) {G};
     \node[plus] (B) {+};  
     \node[monomer] (M) {M};      
   } 
\node[monomer, fill=yellow!30] (Gr7) {G};
   { [start branch=first going right]
     \node[monomer, join, fill=yellow!30] (G6r) {G};
     \node[monomer, join, fill=yellow!30] (G5r) {G};
     \node[monomer, join, fill=yellow!30] (G4r) {G};
     \node[monomer, join, fill=yellow!30] (G3r) {G};
     \node[monomer, join, fill=yellow!30] (G2r) {G};
     \node[monomer, join, fill=yellow!30] (G1r) {G};
     \node[fleche] (A) {$\rightarrow$};
     \node[monomer, fill=yellow!25] (G6p) {G};
     \node[monomer, join, fill=yellow!25] (G5p) {G};         
     \node[monomer, join, fill=yellow!25] (G4p) {G};
     \node[monomer, join, fill=yellow!25] (G3p) {G};
     \node[monomer, join, fill=yellow!25] (G2p) {G};
     \node[monomer, join, fill=yellow!25] (G1p) {G};
     \node[plus] (B) {+};  
     \node[monomer] (M) {M};      
   }  
\node[monomer, fill=yellow!25] (G6r) {G};
   { [start branch=first going right]
     \node[monomer, join, fill=yellow!25] (G5r) {G};
     \node[monomer, join, fill=yellow!25] (G4r) {G};
     \node[monomer, join, fill=yellow!25] (G3r) {G};
     \node[monomer, join, fill=yellow!25] (G2r) {G};
     \node[monomer, join, fill=yellow!25] (G1r) {G};
     \node[fleche] (A) {$\rightarrow$};
     \node[monomer, fill=yellow!20] (G5p) {G};
     \node[monomer, join, fill=yellow!20] (G4p) {G};
     \node[monomer, join, fill=yellow!20] (G3p) {G};
     \node[monomer, join, fill=yellow!20] (G2p) {G};
     \node[monomer, join, fill=yellow!20] (G1p) {G};
     \node[plus] (B) {+};  
     \node[monomer] (M) {M};      
   }
\node[monomer, fill=yellow!20] (G5r) {G};
   { [start branch=first going right]
     \node[monomer, join, fill=yellow!20] (G4r) {G};
     \node[monomer, join, fill=yellow!20] (G3r) {G};
     \node[monomer, join, fill=yellow!20] (G2r) {G};
     \node[monomer, join, fill=yellow!20] (G1r) {G};
     \node[fleche] (A) {$\rightarrow$};
     \node[monomer, fill=yellow!15] (G4p) {G};
     \node[monomer, join, fill=yellow!15] (G3p) {G};
     \node[monomer, join, fill=yellow!15] (G2p) {G};
     \node[monomer, join, fill=yellow!15] (G1p) {G};
     \node[plus] (B) {+};  
     \node[monomer] (M) {M};      
   }
\node[monomer, fill=yellow!15] (G4r) {G};
    { [start branch=second going right]
     \node[monomer, join, fill=yellow!15] (G3r) {G};
     \node[monomer, join, fill=yellow!15] (G2r) {G};
     \node[monomer, join, fill=yellow!15] (G1r) {G};
     \node[fleche] (A) {$\rightarrow$};
     \node[monomer, fill=yellow!10] (G3p) {G};
     \node[monomer, join, fill=yellow!10] (Gp2) {G};
     \node[monomer, join, fill=yellow!10] (G1p) {G};
     \node[plus] (B) {+};  
     \node[monomer] (M) {M};      
   }
\node[monomer, fill=yellow!10] (G3r) {G};
    { [start branch=third going right]
      \node[monomer, join, fill=yellow!10] (G2r) {G};
      \node[monomer, join, fill=yellow!10] (G1r) {G};
      \node[fleche] (A) {$\rightarrow$};
      \node[monomer, fill=yellow!5] (G2p) {G};
      \node[monomer, join, fill=yellow!5] (G1p) {G};
      \node[plus] (B) {+};  
      \node[monomer] (M) {M};     
   }
\node[monomer, fill=yellow!5] (G2r) {G};
   { [start branch=fouth going right]
     \node[monomer, join, fill=yellow!5] (G1r) {G};
     \node[fleche] (A) {$\rightarrow$};
     \node[monomer] (G1pp) {M};
     \node[plus] (B) {+};  
     \node[monomer] (M) {M};      
   }
}
\end{tikzpicture}

\end{document}

PS : yes I know the the name of the nodes are used many times

enter image description here

3
  • @egreg, thanks, you were faster than me adding the picture.
    – DRi
    Commented Apr 24, 2015 at 13:10
  • is that last line supposed to be G-G -> M + M or G-G -> G + M ?
    – Thruston
    Commented Apr 24, 2015 at 13:45
  • It doesn't matter really. Chemically (it is a polymer degradation scheme) the leaving part (M) could be different from the monomer (G), but it might be simpler for the scheme to consider G-G -> G + M
    – DRi
    Commented Apr 24, 2015 at 14:16

3 Answers 3

5

I don't think this is quite what is required at all, but anyway... I went for brevity over anything/everything else. Needs PGF v3 for the node contents key.

\documentclass[tikz, border=5]{standalone}
\begin{document}
\begin{tikzpicture}[x=2.5em, y=2.5em,  
  every slot/.style={fill=yellow!\m!white, draw, very thick, 
    rounded corners, node contents=G, text width=1em, align=center}]
\foreach \i [evaluate={\j=int(\i+1); \k=int(\i*2+1); 
  \l=int(\k+1); \m=\i*5;}] in {12,11,...,2}{
 \draw (1,\i-12) -- (\i,\i-12)  (\i+2,\i-12) -- (2*\i,\i-12);
 \tikzset{%
   slot \j/.style={draw=none, fill=none, node contents=$\rightarrow$},
   slot \k/.style={draw=none, fill=none, node contents=$+$},
   slot \l/.style={node contents=M}
 }
 \foreach \j in {1,...,\l} 
   \node at (\j, \i-12) [every slot/.try, slot \j/.try];
}
\end{tikzpicture}  
\end{document}

enter image description here

1
  • Wow ! still shorter, just needs the adjustment mentionned above for M (fill=none) and it is OK
    – DRi
    Commented Apr 24, 2015 at 16:36
5

With a few nested loops and some conditions (with the help of a simple style from the forest package) this can be done rather easily.

There might be a shorter graphs solution which will do basically the same.

Code

\documentclass[tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta,chains}
\tikzset{if/.style={/forest/if=#1},
         If/.style n args=3{if={#1}{/utils/exec={#2}}{/utils/exec={#3}}}}
\begin{document}
\begin{tikzpicture}[monomer/.style={
   rectangle, rounded corners, 
   draw=black, very thick, text width=1em, 
   minimum height=1.5em, align=center},
monomer G/.style=monomer, monomer M/.style=monomer,
fleche/.style={text width=1.5em, align=center, on chain},
plus/.style={text width=1em, align=center},
start chain=mch going below, node distance=.3cm and 0.1cm,
monomer G join/.style={
 xshift=.1cm+1em+2*\pgfkeysvalueof{/pgf/inner xsep}+2*\pgfkeysvalueof{/pgf/outer xsep},
 join=by {-Stealth, shorten >=4\pgflinewidth, shorten <=4\pgflinewidth}},
monomer M join/.style={
 xshift=.1cm+1em+2*\pgfkeysvalueof{/pgf/inner xsep}+2*\pgfkeysvalueof{/pgf/outer xsep},
 join=by {draw=none, to path={-- node[midway, plus] {+} (\tikztotarget) }}}]
\foreach \row[evaluate={\col=int(\row-1);
                        \val=int(min(100,(\row-1)*5));
                        \valB=int(min(100,(\row-2)*5));
                       }] in {12, ..., 2}{
  \tikzset{monomer G/.append style={fill=yellow!\val}}
  \node [monomer G, on chain=mch] {G};
  \begin{scope}[start branch/.expanded=br-\row\space going base right]
    \foreach \throwaway in {1,...,\col} \node[monomer G, on chain, join] {G};

    \tikzset{
      If={\col>1}{
        \foreach \throwaway in {1,...,\col}
          \node[monomer G, fill=yellow!\valB, on chain,
            if={\throwaway==1}{monomer G join}{join}] {G};
      }{\node[on chain, monomer M, monomer G join] {M};}}
    \node[on chain, monomer M, monomer M join] {M};
  \end{scope}}
\end{tikzpicture}
\end{document}

Output

enter image description here

5
  • Wow, the code is harder than I thought it would need to be.
    – Alenanno
    Commented Apr 24, 2015 at 15:09
  • I am really impressed. I'll try to understand all the details and try to reproduce it keeping the M background white
    – DRi
    Commented Apr 24, 2015 at 15:28
  • @DRi In this case just add fill=none to the nodes with the M. Or use a different style (say monomer M so that it doesn't get the fill appended). Commented Apr 24, 2015 at 15:30
  • I'm giving up on the graphs solution. I don't understand the concept of it (neither of the subgraphs). Commented Apr 24, 2015 at 16:29
  • @Qrrbrbirlbel M background, indeed that is so simple. Thanks
    – DRi
    Commented Apr 24, 2015 at 16:31
4

While you're waiting for a TikZ-loop-expert, here's a Metapost version for your entertainment that features two nested loops.

enter image description here

prologues := 3;
outputtemplate := "%j%c.eps";
beginfig(1);
path s; s = superellipse(7 right, 6 up, 7 left,6 down, 0.8);
color yellow, filler; yellow = .4[red+green,white];
boolean connect; 
pair t;
y = 0;
for n=12 downto 2:
  filler := ((14-n)/11)[yellow,white];
  y := y - 20;
  connect := false;
  for x=1 upto 2n+2:
     t := (18x,y);
     if x=n+1:
       label(btex $\to$ etex, t);
       connect := false;
     elseif x=2n+1:
       label(btex $+$ etex, t);
     elseif (x=2n+2) or ((x=2n)and(n=2)):
       draw s shifted t;
       label("M",t);
     else:
       if connect:
         draw t+11left--t;
       fi
       fill s shifted t withcolor filler;
       draw s shifted t;
       label("G",t);
       connect := true;
     fi
  endfor
endfor
endfig;
end
2
  • thanks a lot, sounds like a nice piece of code, unfortunately I am not very (i.e. at all) familiar with Metapost. That the reason why I used TikZ instead.
    – DRi
    Commented Apr 24, 2015 at 14:30
  • 2
    @DRi In fairness, you were once unfamiliar with TikZ as well :) Commented Apr 24, 2015 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .