使用带有链条的循环?

使用带有链条的循环?

有没有更聪明的方法来实现与以下 MWE 相同的结果:

1)通过使用某种循环,而不是手动输入每个节点,

2)使用 tikz 绘制箭头,而不是使用 LaTeX 数学模式

\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:是的,我知道节点的名称被使用了很多次

在此处输入图片描述

答案1

我认为这根本不是必需的,但无论如何……我追求简洁。需要PGF v3关键node contents

\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}

在此处输入图片描述

答案2

利用一些嵌套循环和一些条件(借助forest包中的简单样式),可以相当容易地完成此操作。

可能存在一个更短的graphs解决方案,但其作用基本相同。

代码

\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}

输出

在此处输入图片描述

答案3

当你在等待 TikZ-loop 专家时,这里有一个元帖子为您的娱乐提供两个嵌套循环的版本。

在此处输入图片描述

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

相关内容