29

I have a simple question about tikz tree, in which I want to have a edge label without that round box... I know that I have every node=circle, but I don't know how to change it for that specific element. Please help ;)

My code:

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1cm}]
\tikzstyle{every node}=[circle,draw]

\node (Root) [red] {3}
    child {
    node {1} 
    child { node {4} edge from parent node[left] {help!} }
    child { node {1} }
    child { node {3} }
}
child {
    node {2}
    child { node {3} }
    child { node {2} }
    child { node {5} }
};

\end{tikzpicture}
\end{document}
0

3 Answers 3

32

Update

A simple, cleaner, shorter code can be obtained with the powerful forest package:

\documentclass{article} 
\usepackage{forest}

\begin{document}

\begin{forest}
for tree={circle,draw, l sep=20pt}
[3,red 
    [1  
      [4,edge label={node[midway,left] {Help!}} ] 
      [1] 
      [3]
    ]
    [2
      [3] 
      [2] 
      [5]
  ] 
]
\end{forest}

\end{document}

enter image description here

Initial version

As Alan Munn says in his answer, using tikz-qtree could be preferable. However, if, for some reason, you want to stick to your code, you can use the draw=none option:

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1cm}]
\tikzstyle{every node}=[circle,draw]

\node (Root) [red] {3}
    child {
    node {1} 
    child { node {4} edge from parent node[left,draw=none] {help!} }
    child { node {1} }
    child { node {3} }
}
child {
    node {2}
    child { node {3} }
    child { node {2} }
    child { node {5} }
};

\end{tikzpicture}
\end{document}

enter image description here

18

As with all simple trees like this, it's almost always easier to use the tikz-qtree package:

\documentclass{article} 
\usepackage{tikz-qtree}

\begin{document}
\begin{tikzpicture}[every tree node/.style={draw,circle},
   level distance=1.25cm,sibling distance=.5cm, 
   edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree [.\node[red] {3}; 
    [.1  
      \edge node[auto=right] {Help!};  
      [.4  ] [.1 ] [.3 ]
    ]
    [.2
      [.3 ] [.2 ] [.5 ]
    ] ]
\end{tikzpicture}
\end{document}

output of code

3

There is also a style called every child node that is applied to every node in a child and leaves the others unchanged. This will mostly do what you want, but you will have to manually apply the style to the root node.

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1cm},
tree node/.style={circle,draw},
every child node/.style={tree node}]

\node[tree node] (Root) [red] {3}
    child {
    node {1} 
    child { node {4} edge from parent node[left] {help!} }
    child { node {1} }
    child { node {3} }
}
child {
    node {2}
    child { node {3} }
    child { node {2} }
    child { node {5} }
};

\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .