使用 Tikz 为树上色

使用 Tikz 为树上色

我想突出显示使用 tikz 和 forest 绘制的概率树上的某条路径(感谢此来源):

    \documentclass[11pt]{article}
    \usepackage{tikz}
    \usepackage{forest,nicefrac}
    \begin{document}
    % 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] {}
    child {
        node[bag] {II $(2R,1B)$}        
            child {
                node[end, label=right:
                    {$P(B\cap II)=\frac{1}{2}\cdot\frac{1}{3}$}] {}
                edge from parent
                node[above] {$B$}
                node[below]  {$\frac{1}{3}$}
            }
            child {
                node[end, label=right:
                    {$P(R\cap II)=\frac{1}{2}\cdot\frac{2}{3}$}] {}
                edge from parent
                node[above] {$R$}
                node[below]  {$\frac{2}{3}$}
            }
            edge from parent 
            node[above] {$II$}
            node[below]  {$\frac{1}{2}$}
    }
    child {
        node[bag] {I $(2R,4B)$}        
        child {
                node[end, label=right:
                    {$P(B\cap I)=\frac{1}{2}\cdot\frac{4}{6}$}] {}
                edge from parent
                node[above] {$B$}
                node[below]  {$\frac{4}{6}$}
            }
            child {
                node[end, label=right:
                    {$P(R\cap I)=\frac{1}{2}\cdot\frac{2}{6}$}] {}
                edge from parent
                node[above] {$R$}
                node[below]  {$\frac{2}{6}$}
            }
        edge from parent         
            node[above] {$I$}
            node[below]  {$\frac{1}{2}$}
    };
\end{tikzpicture}
    \end{document}

我如何突出显示路径 I、R?只需使边缘标记或使用其他颜色或更改背景颜色即可突出显示,无论您认为哪种做法更好。

答案1

像这样?

在此处输入图片描述

为此,您只需添加[draw=red,ultra thick]选项child

         child[draw=red,ultra thick] { % <-- added option for emphasize line R
            node[end, label=right:
                {$P(R\cap I)=\frac{1}{2}\cdot\frac{2}{6}$}] {}
            edge from parent
            node[above] {$R$}
            node[below]  {$\frac{2}{6}$}
            }

相关内容