0

How do I center Tikz trees on the page, like the one used in the answer to this question:

Horizontal Probability Tree with Level Labels

Having modified that code to the below code, my tree is off-center and overflowing:

\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] {AAB ({\color{red} 1/18})} 
                edge from parent
                node[ans,left] {$1/2$}}
            child {node[end] {AAC ({\color{red} 1/18})} 
                edge from parent
                node[ans,left] {$1/2$}}
            edge from parent
            node[ans,left] {$1/3$}
        }
        child [black] { node {B} 
            child {node[end] {ABC ({\color{red} 1/9})} 
                edge from parent
                node[ans,left] {$1$}}
            edge from parent
            node[ans,left] {$1/3$}
        }
        child [black] { node {C} 
            child {node[end] {ACB ({\color{red} 1/9})} 
                edge from parent
                node[ans,left] {$1$}}
            edge from parent
            node[ans,left] {$1/3$}
        }
        edge from parent
        node[ans,above] {$1/3$}
    }
    child {
        node {Door B}
        child { node {A} 
            child {node[end] {BAC ({\color{red} 1/9})} 
                edge from parent
                node[ans,left] {$1$}
            }
            edge from parent
            node[ans,left] {$1/3$}
        }
        child { node {B} 
            child {node[end] {BBA ({\color{red} 1/18})} 
                edge from parent
                node[ans,left] {$1/2$}}
            child {node[end] {BBC ({\color{red} 1/18})} 
                edge from parent
                node[ans,left] {$1/2$}}
            edge from parent
            node[ans,left] {$1/3$}
        }
        child { node {C} 
            child {node[end] {BCA ({\color{red} 1/9})} 
                edge from parent
                node[ans,left] {$1$}}
            edge from parent
            node[ans,left] {$1/3$}
        }
        edge from parent
        node[ans,above] {$1/3$}
    }
    child {
        node {Door C}
        child { node {A} 
            child {node[end] {CAB ({\color{red} 1/9})} 
                edge from parent
                node[ans,left] {$1$}}
            edge from parent
            node[ans,left] {$1/3$}
        }
        child { node {B}
            child {node[end] {CBA ({\color{red} 1/9})} 
                edge from parent
                node[ans,left] {$1$}}
            edge from parent
            node[ans,left] {$1/3$}
        }
        child { node {C}
            child {node[end] {CCA ({\color{red} 1/18})} 
                edge from parent
                node[ans,left] {$1/2$}}
            child {node[end] {CCB ({\color{red} 1/18})} 
                edge from parent
                node[ans,left] {$1/2$}}
            edge from parent
            node[ans,left] {$1/3$}
        }
        edge from parent
        node[ans,above] {$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) {Monty's pick / Outcome};
        \node[at =(labels-level-|Root-1-1)] {Contestant picked};
        \node[at =(labels-level-|Root-1)] {Prize is behind};
        \node[at =(labels-level-|Root)] {};

    \end{scope}
\end{tikzpicture}

enter image description here

1
  • 1
    Welcome to TeX-SE! In order to see to which extent the tree overshoots the boundaries of your document we need to see the specifics. Therefore please add a fully compilable code that starts with \documentclass and ends with \end{document}. Otherwise we won't know what the page parameters are, nor the font size. (Side remark: \tikzstyle is deprecated.)
    – user121799
    Jun 21 '19 at 2:04
1

I do not know if this goes in the right direction but at least it is a complete compilable example that does not go out of bounds.

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning,trees}
\tikzset{level 1/.style={level distance=3.5cm, sibling distance=3.5cm},
level 2/.style={level distance=4.5cm, sibling distance=1.5cm},
level 3/.style={level distance=3.5cm, sibling distance=2cm},
bag/.style = {text width=4em, text centered},
end/.style = {rectangle, draw=none, minimum width=3pt, inner sep=0pt,right},
ans/.style = {color=red}}
\begin{document}
\begin{tikzpicture}[level distance=2.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] {AAB (\textcolor{red}{1/18})} 
                edge from parent
                node[ans,pos=0.7,above,sloped] {$1/2$}}
            child {node[end] {AAC (\textcolor{red}{1/18})} 
                edge from parent
                node[ans,pos=0.7,above,sloped] {$1/2$}}
            edge from parent
            node[ans,pos=0.7,above,sloped] {$1/3$}
        }
        child [black] { node {B} 
            child {node[end] {ABC (\textcolor{red}{1/9})} 
                edge from parent
                node[ans,pos=0.7,above,sloped] {$1$}}
            edge from parent
            node[ans,pos=0.7,above,sloped] {$1/3$}
        }
        child [black] { node {C} 
            child {node[end] {ACB (\textcolor{red}{1/9})} 
                edge from parent
                node[ans,pos=0.7,above,sloped] {$1$}}
            edge from parent
            node[ans,pos=0.7,above,sloped] {$1/3$}
        }
        edge from parent
        node[ans,pos=0.7,above,sloped] {$1/3$}
    }
    child {
        node {Door B}
        child { node {A} 
            child {node[end] {BAC (\textcolor{red}{1/9})} 
                edge from parent
                node[ans,pos=0.7,above,sloped] {$1$}
            }
            edge from parent
            node[ans,pos=0.7,above,sloped] {$1/3$}
        }
        child { node {B} 
            child {node[end] {BBA (\textcolor{red}{1/18})} 
                edge from parent
                node[ans,pos=0.7,above,sloped] {$1/2$}}
            child {node[end] {BBC (\textcolor{red}{1/18})} 
                edge from parent
                node[ans,pos=0.7,above,sloped] {$1/2$}}
            edge from parent
            node[ans,pos=0.7,above,sloped] {$1/3$}
        }
        child { node {C} 
            child {node[end] {BCA (\textcolor{red}{1/9})} 
                edge from parent
                node[ans,pos=0.7,above,sloped] {$1$}}
            edge from parent
            node[ans,pos=0.7,above,sloped] {$1/3$}
        }
        edge from parent
        node[ans,pos=0.7,above,sloped] {$1/3$}
    }
    child {
        node {Door C}
        child { node {A} 
            child {node[end] {CAB (\textcolor{red}{1/9})} 
                edge from parent
                node[ans,pos=0.7,above,sloped] {$1$}}
            edge from parent
            node[ans,pos=0.7,above,sloped] {$1/3$}
        }
        child { node {B}
            child {node[end] {CBA (\textcolor{red}{1/9})} 
                edge from parent
                node[ans,pos=0.7,above,sloped] {$1$}}
            edge from parent
            node[ans,pos=0.7,above,sloped] {$1/3$}
        }
        child { node {C}
            child {node[end] {CCA (\textcolor{red}{1/18})} 
                edge from parent
                node[ans,pos=0.7,above,sloped] {$1/2$}}
            child {node[end] {CCB (\textcolor{red}{1/18})} 
                edge from parent
                node[ans,pos=0.7,above,sloped] {$1/2$}}
            edge from parent
            node[ans,pos=0.7,above,sloped] {$1/3$}
        }
        edge from parent
        node[ans,pos=0.7,above,sloped] {$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) {Monty's pick / Outcome};
        \node[at =(labels-level-|Root-1-1)] {Contestant picked};
        \node[at =(labels-level-|Root-1)] {Prize is behind};
        \node[at =(labels-level-|Root)] {};

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

enter image description here

My personal favorite for drawing such trees, though, is forest.

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{forest}
\usetikzlibrary{positioning}
\forestset{el/.style={edge label={node[above,pos=0.7,sloped,font=\sffamily\small,text=red]{#1}}},
el'/.style={edge label={node[below,pos=0.7,sloped,font=\sffamily\small,text=red]{#1}}}}
\tikzset{mono/.style={/tikz/execute at end node={~(\textcolor{red}{1/9})}},
duo/.style={/tikz/execute at end node={~(\textcolor{red}{1/18})}}}
\begin{document}
\begin{forest}
for tree={grow'=0,where level=1{l+=3em}{l+=2em},
    if level=3{}{el={1/3}},}
[Origin,alias=l0
 [Door A,alias=l1
  [A,alias=l2
   [AAB,el=1/2,duo,alias=l3]
   [AAC,el'=1/2,duo]
  ]
  [B
   [ABC,el=1,mono]
  ]
  [C
   [ACB,el=1,mono]
  ]
 ]
 [Door B
  [A
   [BAC,el=1,mono]
  ]
  [B
   [BBA,el=1/2,duo]
   [BBC,el'=1/2,duo]
  ]
  [C
   [BCA,el=1,mono]
  ]
 ]
 [Door C
  [A
   [CBA,el=1,mono]
  ]
  [B
   [CAB,el=1,mono]
  ]
  [C
   [CCA,el=1/2,duo]
   [CCB,el'=1/2,duo]
  ]
 ]
]
\begin{scope}[every node/.style={text width=2cm,align=center, anchor=center, font=\bfseries}]
  \node[above= 1cm of l3] (labels-level) {Monty's pick / Outcome};
  \node[at =(labels-level-|l2)] {Contestant picked};
  \node[at =(labels-level-|l1)] {Prize is behind};
  \node[at =(labels-level-|l0)] {};
\end{scope}
\end{forest}
\end{document}

enter image description here

The frame can be removed by removing showframe (or \usepackage[showframe]{geometry} altogether). This example does, by far, not show all the powers of forest.

2
  • Good to see 2 alternatives! As a side note, I think the red labels are a bit shifted. Why not use midway for these cases?
    – manooooh
    Jun 21 '19 at 5:48
  • 1
    @manooooh If you look at the connection Door C to B you see why. (One could solve this by putting the labels manually.)
    – user121799
    Jun 21 '19 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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