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.

I'm trying to create a Horizontal Probability Tree with Level Labels using tikz. (Not tikz-qtree). Right now, I have a horizontal tree. The labels are NOT aligned to the top of the diagram. I would like to have all the labels aligned without having to manually tweak positions.

Here is the MWE:

\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=4.5cm, sibling distance=1.5cm]
\tikzstyle{level 3}=[level distance=3.5cm, sibling distance=2cm]


\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [rectangle, draw=none, minimum width=3pt, inner sep=0pt]

\tikzstyle{ans} = [color=red]


\begin{tikzpicture}[level distance=5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1.2cm},
level 3/.style={level distance = 2cm},grow'=right]
\tikzstyle{every node}=[]
    \node (Root) [] {Origin}
        child [] {
        node {Door A}
        child { node {A} 
                child {node[end] {$0$} }
                edge from parent
                node[left] {$0$}
        }
        child [black] { node {B} 
                child {node[ans,end] {$\frac{1}{6}$} }
                edge from parent
                node[ans,left] {$\frac{1}{2}$}
        }
        child [black] { node {C} 
                child {node[ans,end] {$\frac{1}{6}$} }
            edge from parent
            node[ans,left] {$\frac{1}{2}$}
        }
        edge from parent
        node[above] {$\frac{1}{3}$}
    }
    child {
        node {Door B}
        child { node {A} 
                child {node[ans,end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {B} 
                child {node[end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {C} 
                child {node[ans,end] {$\frac{1}{3}$} }
                edge from parent
                node[ans,left] {$1$}
        }
        edge from parent
        node[ans,left] {$\frac{1}{3}$}
    }
    child {
        node {Door C}
        child { node {A} 
                child {node[ans,end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {B}
                child {node[ans,end] {$\frac{1}{3}$} }
                edge from parent
                node[ans,left] {$1$}
        }
        child { node {C}
                child {node[end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        edge from parent
        node[ans,left] {$\frac{1}{3}$}
    };
   % How I'm applying labels to each level. 
  % Need to be able to dynamically align nodes at top level
\begin{scope}[every node/.style={above}]
 \path (Root    -| Root)  ++(0,15mm) node {Test};
 \path (Root-1   -| Root-1)  ++(0,15mm) node {Contestant Chooses};
 \path (Root-1-1  -| Root-3-3-1)  ++(10mm,13mm) node {Host Reveals Door};
 \path (Root-1-1-1 -| Root-3-3) ++(0,15mm) node {Probability};
\end{scope}


\end{tikzpicture}

I get this

enter image description here

and want this enter image description here

share|improve this question
    
What do you mean by "aligned to the top of the diagram"? –  Kevin C Jun 19 at 20:20
    
yozh.org/wp/wp-content/uploads/2010/10/decision_tree.jpg Notice the labels for each level are aligned to the same height at the top of the diagram. –  Coatless Jun 19 at 20:27
    
Try this: \begin{scope}[every node/.style={anchor=base}] \path (Root-1-1-1 -| Root) ++(0,15mm) node(a) {Test}; \path (Root-1-1-1 -| Root-1) ++(0,15mm) node {Contestant Chooses}; \path (Root-1-1-1 -| Root-3-3-1) ++(10mm,15mm) node {Host Reveals Door}; \path (Root-1-1-1 -| Root-3-3) ++(0,15mm) node(b) {Probability}; \end{scope} –  Kevin C Jun 19 at 21:08
    
Please always post a complete small document. Much more useful than mere fragments. Have you considered forest? –  cfr Jun 20 at 3:01

1 Answer 1

up vote 4 down vote accepted

You can use intersections coordinate system to align all level labels. First place the label for the top most leaf where you want:

\node[above= 1cm of Root-1-1-1] (labels-level) {Host Reveals Door};

and later, use it as an horizontal reference for all other labels

\node[at =(labels-level-|Root-1-1)] {Probability};
\node[at =(labels-level-|Root-1)] {Contestant Chooses};
\node[at =(labels-level-|Root)] {Test};

I've used positioning library to fix first label position. The complete code is

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}

\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=4.5cm, sibling distance=1.5cm]
\tikzstyle{level 3}=[level distance=3.5cm, sibling distance=2cm]


\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [rectangle, draw=none, minimum width=3pt, inner sep=0pt]

\tikzstyle{ans} = [color=red]


\begin{tikzpicture}[level distance=5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1.2cm},
level 3/.style={level distance = 2cm},grow'=right]
\tikzstyle{every node}=[]
    \node (Root) [] {Origin}
        child [] {
        node {Door A}
        child { node {A} 
                child {node[end] {$0$} }
                edge from parent
                node[left] {$0$}
        }
        child [black] { node {B} 
                child {node[ans,end] {$\frac{1}{6}$} }
                edge from parent
                node[ans,left] {$\frac{1}{2}$}
        }
        child [black] { node {C} 
                child {node[ans,end] {$\frac{1}{6}$} }
            edge from parent
            node[ans,left] {$\frac{1}{2}$}
        }
        edge from parent
        node[above] {$\frac{1}{3}$}
    }
    child {
        node {Door B}
        child { node {A} 
                child {node[ans,end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {B} 
                child {node[end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {C} 
                child {node[ans,end] {$\frac{1}{3}$} }
                edge from parent
                node[ans,left] {$1$}
        }
        edge from parent
        node[ans,left] {$\frac{1}{3}$}
    }
    child {
        node {Door C}
        child { node {A} 
                child {node[ans,end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {B}
                child {node[ans,end] {$\frac{1}{3}$} }
                edge from parent
                node[ans,left] {$1$}
        }
        child { node {C}
                child {node[end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        edge from parent
        node[ans,left] {$\frac{1}{3}$}
    };
   % How I'm applying labels to each level. 
  % Need to be able to dynamically align nodes at top level
\begin{scope}[every node/.style={text width=2cm, align=center, anchor=center, font=\bfseries,}]
 \node[above= 1cm of Root-1-1-1] (labels-level) {Host Reveals Door};
 \node[at =(labels-level-|Root-1-1)] {Probability};
 \node[at =(labels-level-|Root-1)] {Contestant Chooses};
 \node[at =(labels-level-|Root)] {Test};

\end{scope}
\end{tikzpicture}
\end{document}

and the result (change labels aspect as you want)

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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