3
\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},
edge from parent fork down]
\tikzstyle{every node}=[circle,draw]

\node (Root) [red] {3}
    child {
    node {1}
    child { node {4} edge from parent node[above,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}

yields

enter image description here

but I'd like to add textual labels the positions marked with the red and blue text below.

enter image description here

This post somewhat addresses the red issue but not the blue issue.

Getting the (horizontally running) text along that first, short line before it splits is what is giving me the most trouble.

It seems that first stem needs to be taller to allow room for a label there, especially when the right branch will have its own label.

2
  • What do you mean by “horizontal label here”? You can use the pos key to change the position of a node along a path (in this case only on the horizontal part and the vertical part at the child node). The edge from parent operator needs to be given after all children. Commented Nov 4, 2013 at 22:28
  • I'd like for the text to run horizontally but be positioned just to the right of that vertical line. Imagine if I had placed that blue text in a horizontal line 2mm to the right of the center of that first vertical line stemming from the 3.
    – JohnD
    Commented Nov 4, 2013 at 22:46

2 Answers 2

5

Does this illustration help to understand the relation between path operators (like -- and -|) and the pos key?

For the last example, your will need my paths.ortho library which has been developed for this reason (amongst others).

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{trees}
\tikzset{level distance=1.5cm,
         level 1/.style={sibling distance=3.5cm},
         level 2/.style={sibling distance=1cm}}
\makeatletter
\def\tikz@ege@from@parent@macro@node#1#2{%
  \pgfextra{\def\tikz@@tonodes{#2}}
  [style=edge from parent, #1, /utils/exec=\tikz@node@is@a@labeltrue]
  \tikz@edge@to@parent@path}
\tikzset{
  edge from parent fork down*/.style={
    edge from parent macro=\tikz@ege@from@parent@macro@node,
    edge from parent path={(\tikzparentnode\tikzparentanchor) |-
    ([shift=(down:.5\tikzleveldistance)]
      \tikzparentnode\tikzparentanchor-|\tikzchildnode\tikzchildanchor)
      \tikz@@tonodes -- (\tikzchildnode\tikzchildanchor)}}}
\makeatother
\usetikzlibrary{paths.ortho}
\tikzset{
  edge from parent fork down ortho/.style={
    edge from parent path={{[hvvh/distance=.5\tikzleveldistance, hvvh/from center]
      (\tikzparentnode\tikzparentanchor) |-| (\tikzchildnode\tikzchildanchor)}}}}
\newcommand*\myTree[1]{%
\tikz[#1]\path [every child node/.append style={draw, circle}]
  node[every child node] (Root) [red] {3}
    child { node {1} 
      child { node {4} } child { node {1} } child { node {3} }
      edge from parent[font=\tiny] node[at start] {0} node [pos=.125] {.125}
        node[near start] {.25} node[midway] {.5} node[near end] {.75}
        node [pos=.875] {.875} node[at end] {1}}
    child { node {2}
      child { node {3} } child { node {2} } child { node {5} }
      edge from parent[font=\tiny] node[at start] {0} node [pos=.125] {.125}
        node[near start] {.25} node[midway] {.5} node[near end] {.75}
        node [pos=.875] {.875} node[at end] {1}};}
\begin{document}
\myTree{edge from parent fork down}
\myTree{edge from parent fork down*}
\myTree{edge from parent fork down ortho}
\end{document}

Output

enter image description here enter image description here enter image description here

1
1

Here is another approach where a internal label (A) is assigned to the ROOT which is the origin and then use shift={(x,y)} to add labels. Here the first level distance is increased a little bit to allow room for blue label. The last 3 lines tells how to add labels,

enter image description here

Code:

\documentclass{article} 
\usepackage[papersize={10cm,8cm}]{geometry}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}

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

\node (Root) [red]  (A) {3}
    child {
    node {1}
    child { node {4} edge from parent node[above,draw=none] {help!} }
    child { node{1} }
    child { node {3} }
}
child {
    node {2}
    child { node {3} }
    child { node{2} }
    child { node {5} }
};
\node [draw=none,blue,shift={(2.1cm,-0.5cm)}] (A) {Horizontal label here};
\node [draw=none,red, shift={(-1cm,-0.8cm)}] (A) {here A};
\node [draw=none,red, shift={( 1cm,-0.8cm)}] (A) {here B};
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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