我怎样才能在这个树形图的分支上放置一个空框?

我怎样才能在这个树形图的分支上放置一个空框?
\documentclass{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\pagestyle{empty}


% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=2cm]

% Define styles for bags and leafs
\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]

% The sloped option gives rotated edge labels. Personally
% I find sloped labels a bit difficult to read. Remove the sloped options
% to get horizontal labels. 
\begin{tikzpicture}[grow=right, sloped]
\node[bag] {Bag 1 $4W, 3B$}
    child {
        node[bag] {Bag 2 $4W, 5B$}        
                child {
                    node[end, label=right:
                    {$P(W_1\cap W_2)=\frac{4}{7}\cdot\frac{4}{9}$}] {}
                edge from parent
                node[above] {$W$}
                node[below]  {$\frac{4}{9}$}
            }
                child {
                node[end, label=right:
                    {$P(W_1\cap B_2)=\frac{4}{7}\cdot\frac{5}{9}$}] {}
                edge from parent
                node[above] {$B$}
                node[below]  {$\frac{5}{9}$}
            }
            edge from parent 
            node[above] {$W$}
            node[below]  {$\frac{4}{7}$}
    }
    child {
        node[bag] {Bag 2 $3W, 6B$}        
        child {
            node[end, label=right:
                    {$P(B_1\cap W_2)=\frac{3}{7}\cdot\frac{3}{9}$}] {}
                edge from parent
                node[above] {$B$}
                node[below]  {$\frac{3}{9}$}
                }
            child {
                node[end, label=right:
                    {$P(B_1\cap B_2)=\frac{3}{7}\cdot\frac{6}{9}$}] {}
                edge from parent
                node[above] {$W$}
                node[below]  {$\frac{6}{9}$}
            }
        edge from parent         
            node[above] {$B$}
            node[below]  {$\frac{3}{7}$}
    };
\end{tikzpicture}

\end{document}

附件中的 MWE 取自 tikz 示例网站。我想用一个方框替换每个分支上的概率。提前致谢

答案1

改编:

  • 我定义了一个命令\emptybox来在树叶上的文本内(等式符号后)创建框。
  • ebox我使用以下方式定义了一种风格: ebox/.style={below=1mm, minimum width=10mm, minimum height=6mm, draw}
  • 使用tikzset代替tikzstyle(见应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?)。
  • 我将B其与W顶部分支进行了交换。

代码:

\documentclass{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\pagestyle{empty}

\tikzset{
    % Set the overall layout of the tree
    level 1/.style={level distance=3.5cm, sibling distance=3.5cm},
    level 2/.style={level distance=3.5cm, sibling distance=2cm},
    % Define styles for bags and leafs
    bag/.style={text width=4em, text centered},
    end/.style={circle, minimum width=3pt,fill, inner sep=0pt}
}

\newcommand{\emptybox}{\fbox{\parbox[c][8mm]{20mm}{\ }}}

% The sloped option gives rotated edge labels. Personally
% I find sloped labels a bit difficult to read. Remove the sloped options
% to get horizontal labels. 
\begin{tikzpicture}[grow=right, sloped,
    ebox/.style={below=1mm, minimum width=10mm, minimum height=6mm, draw}
]

\node[bag] {Bag 1 $4W, 3B$}
    child {
        node[bag] {Bag 2 $4W, 5B$}        
                child {
                    node[end, label=right:
                    {$P(W_1\cap W_2)=$ \emptybox}] {}
                edge from parent
                node[above] {$W$}
                node[ebox]  {}
            }
                child {
                node[end, label=right:
                    {$P(W_1\cap B_2)=$ \emptybox}] {}
                edge from parent
                node[above] {$B$}
                node[ebox]  {}
            }
            edge from parent 
            node[above] {$W$}
            node[ebox]  {}
    }
    child {
        node[bag] {Bag 2 $3W, 6B$}        
        child {
            node[end, label=right:
                    {$P(B_1\cap W_2)=$ \emptybox}] {} %
                edge from parent
                node[above] {$W$}
                node[ebox]  {}
                }
            child {
                node[end, label=right:
                    {$P(B_1\cap B_2)=$ \emptybox}] {}
                edge from parent
                node[above] {$B$}
                node[ebox]  {}
            }
        edge from parent         
            node[above] {$B$}
            node[ebox]  {}
    };
\end{tikzpicture}

\end{document}

结果:

在此处输入图片描述

相关内容