TikZ 链提名节点位置

TikZ 链提名节点位置

我想在 C1 的右边提名节点 C4。所以我必须在创建 C4 之前定义 C1。

下面的代码似乎从 C0 到 C3 多了一个行。

\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=M going above]

\node[sha] (C0) {$C_{0}$};
% main branch
\begin{scope}[start branch=B0 going above]
  \foreach \i in {1,...,2} {
    \node [sha] (C\i) {$C_{\i}$};
  }
\end{scope}
%
\begin{scope}[start branch=B1 going above]
  \node[sha,right=of C1,join=with C0] (C4) {$C_{4}$};
  \foreach \i in {5,6} {
    \node[sha] (C\i) {$C_{\i}$};
  }
\end{scope}

\node [sha,join=with M/B0-end,join=with M/B1-end,above=of C2] {$C_{3}$};
\end{tikzpicture}
\end{document}

输出:

在此处输入图片描述

也许我需要删除一个分支但不确定该怎么做!

答案1

这是你想要的吗?你需要告诉B1 branchC3在 的左边C6,就像你对 所做的那样C4,说它C4在 的右边C1。请注意C3应该由 生成B0 branch,而 OP 中没有这个。

在此处输入图片描述

代码

\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=M going above]

\node[sha] (C0) {$C_{0}$};
% main branch
\begin{scope}[start branch=B0 going above]
  \foreach \i in {1,...,3} {
    \node [sha] (C\i) {$C_{\i}$};
  }
\end{scope}
%
\begin{scope}[start branch=B1 going above]
  \node[sha,right=of C1,join=with C0] (C4) {$C_{4}$};
  \foreach \i in {5,6} {
  \node[sha] (C\i) {$C_{\i}$};
  }
  \node[sha,left=of C6] (C3) {$C_{3}$};  % same as C4, telling B1 that it is located right of C1
\end{scope}

\end{tikzpicture}
\end{document}

答案2

组织事物的另一种方式

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

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

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

\foreach \i in {5,6} {
    \node[sha,on chain=2,join] (C\i) {$C_{\i}$};
  }
\draw[->] (C6) -- (C3);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

在 3 行 TikZ 代码中(共 9 行):

\documentclass[tikz,border=7mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
  \begin{tikzpicture}[scale=1.5]
    \foreach \i in {0,...,6}
      \node[draw,circle,fill=green!21] (C\i) at ({div(\i,4)}, {div(\i,4)+mod(\i,4)}) {$C_\i$};
    \draw[-latex] foreach \i/\j in {0/1,1/2,2/3,0/4,4/5,5/6,6/3}{(C\i) edge (C\j)};
  \end{tikzpicture}
\end{document}

在此处输入图片描述

答案4

PSTricks 解决方案:

\documentclass{article}

\usepackage{multido,pst-node}

\begin{document}

\begin{pspicture}(1.9,4.6)
  \pnodes{P}(0.35,0.35)(0.35,1.65)(0.35,2.95)(0.35,4.25)(1.65,1.65)(1.65,2.95)(1.65,4.25)
  \multido{\i = 0+1}{7}{%
    \psset{fillstyle = solid, fillcolor = green!20, opacity = 0.8}
    \cnode(P\i){0.35}{C\i}
    \rput(C\i){$C_{\i}$}}
  \psset{arrows = ->}
  \ncline{C0}{C1}
  \ncline{C1}{C2}
  \ncline{C2}{C3}
  \ncline{C0}{C4}
  \ncline{C4}{C5}
  \ncline{C5}{C6}
  \ncline{C6}{C3}
\end{pspicture}

\end{document}

输出

更新

这是一个完全“自动化”的版本,您所要做的就是选择参数的值,然后绘图将进行相应的调整(此外,不是绘制节点,而是绘制圆圈):

\documentclass{article}

\usepackage{multido,pstricks}
\def\PScircle(#1,#2)#3{%
  \pscircle[fillstyle = solid, fillcolor = green!20, opacity = 0.8](#1,#2){\radius}
  \rput(#1,#2){#3}}

\usepackage{expl3}
\ExplSyntaxOn
  \cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff
\def\const#1#2{\calc{(#1)*\separation+(#2)*\radius}}

% parameters
\def\height{4}
\def\separation{1.3}
\def\radius{0.35}

\begin{document}

\begin{pspicture}(\const{1}{2},\const{\height-1}{2})
  \PScircle(\radius,\radius){$C_{0}$}
  \multido{\i = 1+1}{\calc{\height-1}}{%
    \PScircle(\radius,\const{\i}{1}){$C_{\i}$}
    \PScircle(\const{1}{1},\const{\i}{1}){$C_{\calc{\i+\height-1}}$}}
  \psset{arrows = ->}
  \psline(\radius,\const{0}{2})(\radius,\separation)
  \psline(\const{0}{1+1/sqrt(2)},\const{0}{1+1/sqrt(2)})%
         (\const{1}{1-1/sqrt(2)},\const{1}{1-1/sqrt(2)})
  \multido{\i = 1+1}{\calc{\height-2}}{%
    \psline(\radius,\const{\i}{2})(\radius,\const{\i+1}{0})
    \psline(\const{1}{1},\const{\i}{2})(\const{1}{1},\const{\i+1}{0})}
  \psline(\separation,\const{\height-1}{1})(\const{0}{2},\const{\height-1}{1})
\end{pspicture}

\end{document}

相关内容