线两侧带有标签的决策树

线两侧带有标签的决策树

我正在尝试生成类似于此扫描示例的决策树:

在此处输入图片描述

请注意,有些分支的顶部和底部都有文本。我还没搞清楚该怎么做。我可以在顶部或底部显示文本,但不能同时显示两者。

我附上了一个部分解决方案。我尝试将“爆发”添加为顶部标签,将“0.060”添加为底部标签,但这会导致第一个标签消失。

   \documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{trees,shapes,snakes}
\usepackage{forest}


\tikzset{
    decision/.style={rectangle, minimum height=10pt, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
    chance/.style={circle, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
    leaf-chance/.style={isosceles triangle, minimum width=10pt, draw=black, thick, fill=white, inner sep=0pt, shape border rotate=180, outer sep=-\pgflinewidth}
  }

\newsavebox\notgood
\sbox\notgood{\textbardbl}
\forestset{
  declare toks={optimality}{},
  sub optimal/.style={
    optimality={
      node [midway, sloped] {\usebox\notgood}
    }
  }
}

\begin{document}


\begin{frame}[fragile]

\scalebox{0.85}{
 \begin{forest}
   %my label/.style={
   %   edge label={node[auto, sloped,pos=.75,anchor=south]{#1}}
   % },
   my label/.style={
     edge label={node[midway,above,font=\scriptsize,text width=1.5cm,align=center]{#1}}
   },
   my below/.style={
     edge label={node[midway,below,font=\scriptsize,text width=1.5cm,align=center]{#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={},
          leaf-chance,
        },
      }{},
      edge path={
        \noexpand\path[\forestoption{edge}]
       (!u.parent anchor) -- ([xshift=-22.5mm].child anchor)\forestove{optimality} --  (.child anchor)\forestoption{edge label};
  },
    }
    [,decision
      [0.8351,chance,my label=Insure]
      [,chance,my label=Don't Insure, sub optimal      
        [0.8394,chance,my label=No outbreak
        ]
        [,chance,my label=Outbreak, my below=0.060
          [0.8334,chance,my label=Bans only
          ]
          [0.6663,chance,my label=Slaughter
          ]
        ]
      ]
    ]
\end{forest}
}

\end{frame}
\end{document}

在此处输入图片描述

我发现先前的答案对于在上方/下方之间切换很有帮助,但没有显示如何同时获得两者:https://tex.stackexchange.com/a/177254/80455

另外,这是同样的例子在决策树分支上添加短线(但问题不同)。

答案1

edge label将覆盖任何特定节点的任何先前值。因此第一个标签消失。但我们可以使用与我用于样式相同的方法,即sub optimal声明另一个选项,就像edge label然后将它添加到的重新定义中edge path

\documentclass{beamer}

\usepackage{etex,forest}
\usetikzlibrary{shapes,snakes}


\tikzset{
    decision/.style={rectangle, minimum height=10pt, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
    chance/.style={circle, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
    leaf-chance/.style={isosceles triangle, minimum width=10pt, draw=black, thick, fill=white, inner sep=0pt, shape border rotate=180, outer sep=-\pgflinewidth}
  }

\newsavebox\notgood
\sbox\notgood{\textbardbl}
\forestset{
  declare toks={optimality}{},
  sub optimal/.style={
    optimality={
      node [midway, sloped] {\usebox\notgood}
    }
  },
  declare toks={edge label below}{},
}

\begin{document}


\begin{frame}[fragile]
\scalebox{0.85}{%
 \begin{forest}
   my label/.style={
     edge label={node[midway,above,font=\scriptsize,text width=1.5cm,align=center]{#1}}
   },
  my below/.style={
    edge label below={node[midway,below,font=\scriptsize,text width=1.5cm,align=center]{#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={},
          leaf-chance,
        },
      }{},
      edge path={
        \noexpand\path[\forestoption{edge}]
        (!u.parent anchor) -- ([xshift=-22.5mm].child anchor)\forestove{optimality} --  (.child anchor)\forestoption{edge label below}\forestoption{edge label};
  },
    }
    [,decision
      [0.8351,chance,my label=Insure]
      [,chance,my label=Don't Insure, sub optimal
        [0.8394,chance,my label=No outbreak
        ]
        [,chance,my label=Outbreak, my below=0.060
          [0.8334,chance,my label=Bans only
          ]
          [0.6663,chance,my label=Slaughter
          ]
        ]
      ]
    ]
\end{forest}%
}%
\end{frame}
\end{document}

上方和下方的边缘标签

为了避免拥挤,可以允许更长的标签(增加或删除text width)或增加的值s sep

取消以下限制text width

   my label/.style={
     edge label={node[midway,above,font=\scriptsize,align=center]{#1}}
   },
  my below/.style={
    edge label below={node[midway,below,font=\scriptsize,align=center]{#1}}
  },

更长的标签

增加s sep

  s sep+=7.5mm,

更远的兄弟姐妹

答案2

这有点像“黑客”,但您可以简单地设置一个额外的标签,而无需使用abovebelow键。然后\\当您想切换到“另一行”时,它们将显示一个在上面,一个在下面。

因此基本上,您将拥有:

  • my above
  • my below
  • my label(两个都)

正如您所展示的,这是可行的。如果在某些情况下此行为会中断,请说明。

输出

图1

代码

\documentclass{beamer}   
\usepackage{tikz}
\usetikzlibrary{trees,shapes,snakes}
\usepackage{forest}


\tikzset{
    decision/.style={rectangle, minimum height=10pt, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
    chance/.style={circle, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
    leaf-chance/.style={isosceles triangle, minimum width=10pt, draw=black, thick, fill=white, inner sep=0pt, shape border rotate=180, outer sep=-\pgflinewidth}
  }

\newsavebox\notgood
\sbox\notgood{\textbardbl}
\forestset{
  declare toks={optimality}{},
  sub optimal/.style={
    optimality={
      node [midway, sloped] {\usebox\notgood}
    }
  }
}

\begin{document}


\begin{frame}[fragile]

\scalebox{0.85}{
 \begin{forest}
    my above/.style={
        edge label={node[midway,above,font=\scriptsize,align=center]{#1}}
    },
    my below/.style={
        edge label={node[midway,below,font=\scriptsize,align=center]{#1}}
    },
    my label/.style={
        edge label={node[midway,font=\scriptsize,align=center]{#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={},
          leaf-chance,
        },
      }{},
      edge path={
        \noexpand\path[\forestoption{edge}]
       (!u.parent anchor) -- ([xshift=-22.5mm].child anchor)\forestove{optimality} --  (.child anchor)\forestoption{edge label};
  },
    }
    [,decision
      [0.8351,chance,my above=Insure]
      [,chance,my below=Don't Insure, sub optimal      
        [0.8394,chance,my above=No outbreak
        ]
        [,chance,my label=Outbreak\\0.060
          [0.8334,chance,my above=Bans only
          ]
          [0.6663,chance,my below=Slaughter
          ]
        ]
      ]
    ]
\end{forest}
}
\end{frame}
\end{document}

相关内容