具有多個來源的機率樹,突出顯示特定路徑

具有多個來源的機率樹,突出顯示特定路徑

我正在嘗試使用乳膠重新創建圖形。我想建立一個樹狀形狀,其中還包含機率為 0 的事件。它應該看起來類似於目標影像

我嘗試使用 tikz 和 Forest 套件創建它。然而,我沒有設法將機率為 0 的灰色事件包含在圖形中。是否有其他套件或方法可以輕鬆產生這樣的結果?或者我必須“暴力”並使用坐標手動定義每個節點和邊緣?

已經非常感謝了。

答案1

nicematrixtikz

在此輸入影像描述

\documentclass{article}

\usepackage{nicematrix, tikz}

\begin{document}

\[
\tikzset{outer sep=1mm}
\renewcommand{\arraystretch}{4}
\setlength{\arraycolsep}{1cm}
\begin{NiceArray}{lll}
    \color{gray}p(A)=0 & p(A)=\frac{1}{3} & p(A)=\frac{1}{6}\\
    \color{gray}p(B)=0 & p(B)=\frac{1}{3} & p(A)=\frac{1}{3}+\frac{1}{9}=\frac{4}{9}\\
    p(C)=1 & \color{gray}p(C)=0 & p(A)=\frac{1}{6}+\frac{1}{9}=\frac{5}{18}\\
    \color{gray}p(D)=0 & \color{gray}p(D)=0 & p(A)=\frac{1}{9}\\
    \color{gray}p(E)=0 & p(E)=\frac{1}{3} & \color{gray}p(A)=0
  \CodeAfter
    \tikz
    \draw[thick] (3-1.east)--node[above]{$\frac{1}{3}$}(1-2.west)
        (1-2.east)--node[pos=.7, right=2pt]{$\frac{1}{3}\cdot1$}(2-3.west)
        (3-1.east)--node[below]{$\frac{1}{3}$}(2-2.west) 
        (2-2.east)--node[pos=.7, right]{$\frac{1}{3}\cdot\frac{1}{2}$}(1-3.west) 
        (2-2.east)--node[pos=.7, right=2pt]{$\frac{1}{3}\cdot\frac{1}{22}$}(3-3.west)
        (3-1.east)--node[below]{$\frac{1}{3}$}(5-2.west) 
        (5-2.east)--node[very near end, right=-2pt]{$\frac{1}{3}\cdot\frac{1}{3}$}(2-3.west) 
        (5-2.east)--node[near end, right=-2pt]{$\frac{1}{3}\cdot\frac{1}{3}$}(3-3.west) 
        (5-2.east)--node[below right=-4pt]{$\frac{1}{3}\cdot\frac{1}{3}$}(4-3.west)
        ;    
\end{NiceArray}
\]

\end{document}

相關內容