居中 Tikz 树

居中 Tikz 树

如何将 Tikz 树置于页面中央,就像在回答这个问题时使用的那样:

带有级别标签的水平概率树

将该代码修改为以下代码后,我的树偏离中心并且溢出:

\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}

在此处输入图片描述

答案1

我不知道这是否朝着正确的方向发展,但至少它是一个完整的可编译示例,并且没有超出界限。

\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}

在此处输入图片描述

不过,我个人最喜欢画的是森林。

\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}

在此处输入图片描述

showframe可以通过删除(或全部删除)来删除框架\usepackage[showframe]{geometry}。到目前为止,此示例并未显示 的所有功能forest

相关内容