編輯

編輯

我正在嘗試在來自 Tikzpicture 決策樹中機會節點的行上方添加文字。在行尾或父節點行上方新增文字似乎很容易,但我不知道如何標記最右邊的行。這是我到目前為止所擁有的:

\documentclass{article}
\usepackage{tikz}
\tikzstyle{decision} = [rectangle, minimum height=18pt, minimum width=18pt, draw=blue, fill=none, thick, inner sep=0pt]
\tikzstyle{chance} = [circle, minimum width=18pt, draw=blue, fill=none, thick, inner sep=0pt]
\tikzstyle{line} = [draw=none]

\tikzset{
grow=right,
sloped,
join=miter,
level 1/.style={sibling distance=5cm,level distance=5.2cm},
level 2/.style={sibling distance=4cm, level distance=6.7cm},
level 3/.style={sibling distance=3cm, level distance=6.7cm},
edge from parent/.style={thick, draw=blue},
edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)},
every node/.style={text ragged, inner sep=1mm}
}

\begin{tikzpicture}[]
\small
\node[decision]{}
 child{node[chance]{}
      child{node[below]{-2900}}
      child{node[line]{-900}}
      edge from parent
            node[above]{Plant B}
    }
    child{node[chance]{}
      child{node[line]{-2820}}
      child{node[line]{-820}}
      edge from parent
            node[above]{Plant A}
        };
\end{tikzpicture}
\end{document}

這就是它的樣子

答案1

您可以簡單地edge from parent在子節點中新增更多指令:

\documentclass{article}
\usepackage{tikz}

\tikzstyle{decision} = [rectangle, minimum height=18pt, minimum width=18pt, draw=blue, fill=none, thick, inner sep=0pt]
\tikzstyle{chance} = [circle, minimum width=18pt, draw=blue, fill=none, thick, inner sep=0pt]
\tikzstyle{line} = [draw=none]

\tikzset{
grow=right,
sloped,
join=miter,
level 1/.style={sibling distance=5cm,level distance=5.2cm},
level 2/.style={sibling distance=4cm, level distance=6.7cm},
level 3/.style={sibling distance=3cm, level distance=6.7cm},
edge from parent/.style={thick, draw=blue},
edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)},
every node/.style={text ragged, inner sep=1mm}
}
\begin{document}
\begin{tikzpicture}[]
\small
\node[decision]{}
 child{node[chance]{}
      child{node[below]{-2900}}
      child{node[line]{-900}}
      edge from parent
            node[above]{Plant B}
    }
    child{node[chance]{} 
      child{node[line] {-2820}
      edge from parent 
        node[above] {Foo}}
      child{node[line]{-820}
      edge from parent 
        node[above] {Bar}}
      edge from parent
            node[above]{Plant A}
        };
\end{tikzpicture}
\end{document}

程式碼的輸出

順便說一句,使用該包製作這種樹要簡單得多forest。當我寫下這個答案時,我確信CFR正在編造一個答案來說明如何做。

答案2

我無法弄清楚你的程式碼,所以我重新繪製了樹forest。請注意使用此套件可以更緊湊地指定樹。 (這不是什麼秘密,如果它是一棵樹,我會用它來畫它forest......)

\documentclass[tikz,border=5pt,multi]{standalone}
\usepackage{forest}
\standaloneenv{forest,tikzpicture}
\begin{document}

  \tikzset{
    decision/.style={rectangle, minimum height=18pt, minimum width=18pt,     draw=blue, fill=none, thick, inner sep=0pt},
    chance/.style={circle, minimum width=18pt, draw=blue, fill=none, thick, inner sep=0pt},
    line/.style={draw=none},
  }

  \begin{forest}
    my label/.style={
      edge label={node[auto,sloped,midway,anchor=south]{#1}}
    },
    for tree={
      grow=0,
      parent anchor=east,
      child anchor=west,
      anchor=west,
      text ragged,
      inner sep=1mm,
      edge={thick, draw=blue},
      if level=1{
        for parent={
          l sep=52mm
        },
        s sep=50mm,
        l sep=67mm
      }{
        if level=2{
          s sep=40mm
        }{
          if level=3{
            s sep=30mm
          }{}
        }
      },
    }
    [, decision
      [, chance, my label=Some Label
        [-2900, my label=Fails
        ]
        [-900, my label=Plant B
        ]
      ]
      [, chance
        [-2820, my label=Some Other Thing
        ]
        [-820, my label=Plant A
        ]
      ]
    ]
  \end{forest}

\end{document}

重畫樹

編輯

如果您有興趣,這裡有一個變體:

另一種形狀

\documentclass[tikz,border=5pt,multi]{standalone}
\usepackage{forest}
\standaloneenv{forest,tikzpicture}
\begin{document}
  \tikzset{
    decision/.style={rectangle, minimum height=10pt, minimum width=10pt, draw=blue, fill=none, thick, inner sep=0pt},
    chance/.style={circle, minimum width=10pt, draw=blue, fill=none, thick, inner sep=0pt},
  }
  \begin{forest}
    my label/.style={
      edge label={node[auto,sloped,pos=.75,anchor=south]{#1}}
    },
    for tree={
      grow=0,
      child anchor=west,
      anchor=west,
      text ragged,
      inner sep=1mm,
      edge={thick, draw=blue},
      l sep+=30mm,
      s sep+=5mm,
      if n children=0{
        before typesetting nodes={
          label/.wrap pgfmath arg={right:#1}{content()},
          content={},
          chance,
        },
      }{},
      edge path={
        \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) |- (.child anchor)\forestoption{edge label};
      }
    }
    [, decision
      [, chance, my label=Plant B
        [-2900, my label=Fails
        ]
        [-900, my label=Succeeds
        ]
      ]
      [, chance, my label=Plant A
        [-2820, my label=Something
        ]
        [-820, my label=Other Thing
        ]
      ]
    ]
  \end{forest}
\end{document}

相關內容