使用 foreach 简化 tikz 图

使用 foreach 简化 tikz 图

我有一个 tikz 图,但我认为应该用 foreach 循环来简化它。我尝试了很多次,但仍然无法得到正确的答案!

我的代码如下:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\def\r{5pt}
\def\dy{.5cm}
\tikzstyle{c}=[draw,circle,fill=black,minimum size=\r,inner sep=0pt]
\tikzstyle{line}=[-]
\tiny
\begin{tikzpicture}[]
\node [c,label=right:$C_0$] (C0) {};
\node [c,above=\dy of C0,label=right:$C_1$] (C1) {};
\node [c,above=\dy of C1,label=right:$C_2$] (C2) {};
\node [c,above=\dy of C2,label=right:$C_3$] (C3) {};
\node [c,above=\dy of C3,label=right:$C_4$] (C4) {};

\node [c,right=\dy of C1,label=right:$C_6$] (C6) {};
\node [c,above=\dy of C6,label=right:$C_7$] (C7) {};
\node [c,above=\dy of C7,label=right:$C_8$] (C8) {};

\draw [line] (C0) -- (C1);
\draw [line] (C1) -- (C2);
\draw [line] (C2) -- (C3);
\draw [line] (C3) -- (C4);

\draw [line] (C0) -- (C6);
\draw [line] (C6) -- (C7);
\draw [line] (C7) -- (C8);
\draw [line] (C8) -- (C4);
\end{tikzpicture}

\end{document}

输出:

在此处输入图片描述

答案1

像这样吗?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\def\r{5pt}
\def\dy{.5cm}
\tikzstyle{c}=[draw,circle,fill=black,minimum size=\r,inner sep=0pt, anchor=center]
\tiny
\begin{tikzpicture}
  \draw (0,0)
    node [c,label=right:$C_0$] (C0) {}
    foreach \i in {1,...,4}
      {
        -- ++(0,\dy+\r) node (C\i) [c,label=right:$C_\i$] {}
      };
  \draw (C1) ++(\dy+\r,0)
    node (C6) [c,label=right:$C_6$] {}
    foreach \i in {7,8}
      {
        -- ++(0,\dy+\r) node (C\i) [c,label=right:$C_\i$] {}
      }
    -- (C4)
    (C0) -- (C6);
\end{tikzpicture}

\end{document}

但你也可以去看看chains图书馆。

答案2

matrix of math nodes这是一个有且无 的解决方案chains

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{sha/.style={draw,circle,fill=green!20,opacity=.8,inner sep=.2pt}}

\begin{document}

\begin{tikzpicture}
\matrix (C) [matrix of math nodes, nodes={sha}, column sep=6mm, row sep=4mm]
{
C_3\\
C_2 & C_5\\
C_1 & C_4\\
C_0\\
};

\foreach \i/\j in {4/3,3/2,2/1}
    \draw [-latex] (C-\i-1)--(C-\j-1);

\draw [-latex] (C-4-1)--(C-3-2);
\draw [-latex]  (C-3-2)--(C-2-2);
\draw [-latex]  (C-2-2)--(C-1-1);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

这就是您要寻找的答案!;-)

其他答案使用链、多个 for 循环和其他奇怪的东西。你需要的是记住你的父母零初值。幸运的是,这已经内置在 PGF 中,这使得这个代码块更加简洁。

\tikzstyle{c}=[draw,circle,fill=black,minimum size=5pt,inner sep=0pt]
\tikzstyle{line}=[-]
\tiny
\begin{tikzpicture}[node distance={.5cm}]          
\node [c,label=right:$C_0$] (C0) {};                       %% Initial Node
\foreach \l/\r %
[remember=\l as \lp (initially 0), remember=\r as \rp (initially 0)] %
in {1/4,2/5,3/6} {                                         %% <- node index for left/right
    \node [c,above=of C\lp,label=right:$C_\l$] (C\l) {};   %% place left node above its parent
    \node [c,right=of C\l,label=right:$C_\r$] (C\r) {};    %% place right node next to left one
    \draw[line] (C\lp) -- (C\l);                           %% draw lines to parents
    \draw[line] (C\rp) -- (C\r);
}
\node [c,above=of C3,label=right:$C_4$] (C4) {};           %% finish manually with node C4
\draw[line] (C3) -- (C4);
\draw[line] (C6) -- (C4);
\end{tikzpicture}

我用了

  • l作为left node
  • lp作为left parent
  • r作为right node
  • rp作为right parent

结果看起来和你的图片一模一样,所以我就不在这里重复了。谢谢你的提问,我在寻找答案的过程中学到了很多东西。

答案4

总结了这么多好的答案我们就可以这样做了!

使用两个分支 M0 和 M1,然后将其合并在一起。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{chains}

\tikzstyle{sha}=[draw,circle,fill=green!20,opacity=.8,on chain,join,inner sep=.2pt]

\begin{document}

\begin{tikzpicture}[node distance=4mm and 6mm,
    every join/.style={->},>=latex,
    start chain=M0 going above,
    start chain=M1 going above,
    ]
  % chain M0 for left part
  \foreach \i in {0,...,2} {
    \node [sha,on chain=M0] (C\i) {$C_{\i}$};
  }
  % chain M1 for right part
  \foreach \i in {4,5} {
    \ifnum\i=4
      \node[sha,on chain=M1,right=of C1,join=with C0] (C\i) {$C_{\i}$};
    \else
      \node[sha,on chain=M1] (C\i) {$C_{\i}$};
    \fi       
  }
  % merge M0 and M1-end to C3
  \node [sha,on chain=M0,join=with M1-end] (C3) {$C_{3}$};
\end{tikzpicture}
\end{document}

输出:

在此处输入图片描述

或者删除 if 语句以节省更多行:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{chains}

\tikzstyle{sha}=[draw,circle,fill=green!20,opacity=.8,on chain,join,inner sep=.2pt]

\begin{document}
\begin{tikzpicture}[node distance=4mm and 6mm,
    every join/.style={->},>=latex,
    start chain=M0 going above,
    start chain=M1 going above,
    ]
  % chain M0 for left part
  \foreach \i in {0,...,2} {
    \node [sha,on chain=M0] (C\i) {$C_{\i}$};
  }
  % chain M1 for right part
  \node[sha,on chain=M1,right=of C1,join=with C0] (C4) {$C_{4}$};
  \foreach \i in {5} {
      \node[sha,on chain=M1] (C\i) {$C_{\i}$};       
  }
  % merge M0 and M1-end to C3
  \node [sha,on chain=M0,join=with M1-end] (C3) {$C_{3}$};
\end{tikzpicture}
\end{document}

相关内容