5

I want to nominate node C4 at right of C1. so I have to define C1 before create C4.

Below code seems has one extra line from C0 to 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}

Output:

enter image description here

Maybe I need to remove one branch but not sure how to do it!

5

Is this what you want? You need to tell B1 branch that C3 is at left of C6, same as what you did for C4, saying that C4 is at right of C1. Note that C3 should be generated by B0 branch, which was not in the OP.

enter image description here

Code

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

Another way of organising things

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

enter image description here

  • Great, use two chain resolve this issue perfectly. – lucky1928 Jan 16 '15 at 14:45
4

In 3 lines of TikZ code (9 total lines):

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

enter image description here

3

A PSTricks solution:

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

output

Update

Here is a fully 'automated' version where all you have to do is choose the values of the parameters and the drawing will then be adjusted accordingly (also, instead of drawing nodes, circles are drawn):

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

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.