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

The attached MWE is taken from the tikz example website. I would like to replace the probabilities on each branch with a box. Many thanks in advance

  • 1
    Remove the text from the nodes and add draw,minimum size=1cm? – Torbjørn T. Jun 2 '18 at 11:22
  • 1
    Can you please provide a link to the source? The 'tikz example website' is (1) huge and (2) potentially meaningless to those not 'in the know'. Does your editor really save your documents in latin1 encoding? – cfr Jun 3 '18 at 21:54
2

Adapations:

  • I defined a command \emptybox to create the box inside the text (after the equation sign) at the leaves of the tree.
  • I defined a style ebox with: ebox/.style={below=1mm, minimum width=10mm, minimum height=6mm, draw}.
  • Use tikzset instead of tikzstyle (see Should \tikzset or \tikzstyle be used to define TikZ styles?).
  • I swapped B and W at the top branch.

Code:

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

Result:

enter image description here

  • \tikzstyle is deprecated. – cfr Jun 3 '18 at 21:52
  • @cfr Ok, I know. I changed it now. – dexteritas Jun 4 '18 at 7:05

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.