Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question

marked as duplicate by Jake, Stephan Lehmke, Gonzalo Medina, percusse, Claudio Fiandrino Sep 5 '12 at 6:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Welcome to TeX.sx! You can name your nodes by using child {node (<name>) {$v_1$}}, then you can draw a connecting line using \draw [dashed, red] (<first name>) -- (<second name>);. See How two connect any arbitrary nodes of tree in tikz –  Jake Sep 4 '12 at 12:26
    
Thank you! So you mean if I name my first node with a then that would be child {node (a) {$v_1$}}. And if I connect the child $v_2$ with node name b that is child {node (b) {$v_2$}}. Then if I connect it I would use child {node (a) {$v_1$}}`child {node (b) {$v_2$}}? But I got error. I think that I'm wrong with connecting? –  Al-Ahmadgaid Asaad Sep 4 '12 at 12:35
    
Notice that \tikzstyle is not recommended as per Should \tikzset or \tikzstyle be used to define TikZ styles? –  Claudio Fiandrino Sep 6 '12 at 7:54
add comment

1 Answer

up vote 3 down vote accepted

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

share|improve this answer
    
Thanks Claudio, and to you Jake also. You save my time :) Thank you again! –  Al-Ahmadgaid Asaad Sep 4 '12 at 12:37
    
Sorry I'm back again. Just one last question, I want to connect node v1 and node v3. How can I bent the line from node v1 to node v3 without striking the node v2? –  Al-Ahmadgaid Asaad Sep 4 '12 at 12:41
1  
See my updated answer. :) –  Claudio Fiandrino Sep 4 '12 at 12:46
    
Cool! Thank you very much! –  Al-Ahmadgaid Asaad Sep 4 '12 at 12:49
    
Hello Claudio, I updated my question. Any suggestion? –  Al-Ahmadgaid Asaad Sep 4 '12 at 13:42
show 1 more comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.