3

Possible Duplicate:
How two connect any arbitrary nodes of tree in tikz

Hello everyone I'm new to latex. And just started using the tikz package to draw. I have here a code below, which is like networking with 6 childs. Now, I want to connect the two childs say child $v_1$ and child $v_2$ to form a triangle from vertices $v_0$, $v_1$ to $v_2$, and I want it to colour with red. Sorry for asking this simple question, I just find it difficult. And I'm actually stacked, thus any help would greatly be appreciated.

\begin{center}
\begin{tikzpicture}
[parent anchor=south,child anchor=north,grow=south]
\tikzstyle{every node}=[ball color=orange,circle,text=white]
\tikzstyle{edge from parent}=[draw,dashed,thick,red]
\node {$v_0$}
child {node {$v_1$}}
child {node {$v_2$}}
child {node {$v_3$}}
child {node {$v_4$}}
child {node {$v_5$}}
child {node {$v_6$}}
;
\end{tikzpicture}
\end{center}

And what if I have a child and a sub-child, and I want the edge that connect the child to the sub-child to be in different colour, say blue. Here's what I did,

\begin{center}
\begin{tikzpicture}
[parent anchor=south,child anchor=north,grow=south]
\tikzstyle{every node}=[ball color=orange,circle,text=white]
\tikzstyle{edge from parent}=[draw,dashed,thick,red]
\node {$v_0$}
child {node (a) {$v_1$}
child {node (b) {$v_2$}}
child {node (c) {$v_3$}}
child {node (d) {$v_4$}}
}
child {node (e) {$v_7$}}
child {node {$v_8$}}
child {node {$\cdots$}}
child {node {$v_{16}$}}
;
\path[draw=red,thick,dashed]
(a) edge[blue] (b)
(a) edge[blue] (c)
(a) edge[blue] (d)
(a) edge[bend right, blue] (e);
\end{tikzpicture}
\end{center}

My network now has an overlapping blue colour. How can I fix this? Thanks!

1 Answers1

5

Do you mean something like this?

enter image description here

This is the code that realized it:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{center}
\begin{tikzpicture}
[parent anchor=south,child anchor=north,grow=south]
\tikzset{every node/.style={ball color=orange,circle,text=white}}
\tikzset{edge from parent/.style={draw,dashed,thick,red}}
\node {$v_0$}
child {node (v1){$v_1$}}
child {node (v2){$v_2$}}
child {node {$v_3$}}
child {node {$v_4$}}
child {node {$v_5$}}
child {node {$v_6$}}
;
\path[draw=red,thick,dashed] (v1) edge(v2);
\end{tikzpicture}
\end{center}

\end{document}

First labels has been assigned to the two nodes to be connected and later on the connection has been established by means of a path (almost similarly to Jake's comment approach).


To connect for example v1 and v3 you can use bend right option for the edge.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{center}
\begin{tikzpicture}
[parent anchor=south,child anchor=north,grow=south]
\tikzset{every node/.style={ball color=orange,circle,text=white}}
\tikzset{edge from parent/.style={draw,dashed,thick,red}}
\node {$v_0$}
child {node (v1){$v_1$}}
child {node (v2){$v_2$}}
child {node (v3){$v_3$}}
child {node {$v_4$}}
child {node {$v_5$}}
child {node {$v_6$}}
;
\path[draw=red,thick,dashed] (v1) edge[bend right](v3);
\end{tikzpicture}
\end{center}

\end{document}

Result:

enter image description here


A quick solution just to fix the last edit (for who will read the question in the future).

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{center}
\begin{tikzpicture}
[parent anchor=south,child anchor=north,grow=south]
\tikzset{every node/.style={ball color=orange,circle,text=white}}
\tikzset{edge from parent/.style={draw,dashed,thick}}

% Style to levels: first one red connections, second one blue connections
\tikzset{level 1/.style={red,sibling distance=20mm},level 2/.style={blue,sibling distance=20mm}}
\node {$v_0$}
child {node (a) {$v_1$}
child {node (b) {$v_2$}}
child {node (c) {$v_3$}}
child {node (d) {$v_4$}}
}
child {node (e) {$v_7$}}
child {node {$v_8$}}
child {node {$\cdots$}}
child {node {$v_{16}$}}
;
\end{tikzpicture}
\end{center}

\end{document}

Result:

enter image description here