如何编辑森林树?

如何编辑森林树?

这是我找到的用于绘制树的示例乳胶代码

    \begin{forest}   shade me/.style={%
        bottom color=#1!25,
        top color=#1!5,
        draw=#1,
        drop shadow,
        font=\sffamily,   },   my label/.style n args=2{%
        edge label={node [midway, black, font=\sffamily\scriptsize, #1] {#2}}   },   where n children=0{%
        shade me=blue!50!cyan,   }{%
        if level=0{%
          shade me=green!75!gray,
        }{%
          shade me=blue!75!magenta,
        }   },   for tree={%
        delay={content/.wrap value={\strut #1}},
        edge={red,->},
        l sep+=25pt,
        s sep+=20pt   }   [Income  

 [Age, my label={above, sloped}{High}
          [No, my label={above, sloped}{$< 30$}]
          [Yes, my label={above, sloped}{$31\dots 40$}]
        ]
        [Student, my label={}{Medium}
          [Age
            [No, my label={above, sloped}{$< 30$}]
            [Yes, my label={above, sloped}{$31\dots 40$}]
            [CR, my label={above, sloped}{$> 40$}
              [Yes, my label={above, sloped}{Fair}]
              [No, my label={above, sloped}{Excellent}]
            ]
          ]
          [Yes]
        ]
        [CR, my label={above, sloped}{Low}
          [Yes, my label={above, sloped}{Fair}]
          [Age, my label={above, sloped}{Excellent}
            [No, my label={above, sloped}{$> 40$}]
            [Yes, my label={above, sloped}{$31\dots 40$}]]
          ]
        ]   ] \end{forest}

但是当我尝试修改它时,它不起作用,我想删除学生节点和所有链接它的叶子节点,我可以删除这个代码就可以了,但事实并非如此。我检查了括号和圆括号,它们都是封闭的。

[Student, my label={}{Medium}
      [Age
        [No, my label={above, sloped}{$< 30$}]
        [Yes, my label={above, sloped}{$31\dots 40$}]
        [CR, my label={above, sloped}{$> 40$}
          [Yes, my label={above, sloped}{Fair}]
          [No, my label={above, sloped}{Excellent}]
        ]
      ]
      [Yes]
    ]

我还想将另一件事改为删除年龄节点下的节点“yes”,只留下节点“no”。因此,我删除了此行,但无济于事。

[Yes, my label={above, sloped}{$31\dots 40$}]

答案1

要删除学生节点及其链接的节点,只需从树规范中删除相关的代码行。然而,你不能在forest环境中留下空行

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows}
\begin{document}
\begin{forest}
  shade me/.style={%
    bottom color=#1!25,
    top color=#1!5,
    draw=#1,
    drop shadow,
    font=\sffamily,
  },
  my label/.style n args=2{%
    edge label={node [midway, black, font=\sffamily\scriptsize, #1] {#2}}
  },
  where n children=0{%
    shade me=blue!50!cyan,
  }{%
    if level=0{%
      shade me=green!75!gray,
    }{%
      shade me=blue!75!magenta,
    }
  },
  for tree={%
    delay={content/.wrap value={\strut #1}},
    edge={red,->},
    l sep+=25pt,
    s sep+=20pt
  }
  [Income
  [Age, my label={above, sloped}{High}
      [No, my label={above, sloped}{$< 30$}]
      [Yes, my label={above, sloped}{$31\dots 40$}]
    ]
    [CR, my label={above, sloped}{Low}
      [Yes, my label={above, sloped}{Fair}]
      [Age, my label={above, sloped}{Excellent}
        [No, my label={above, sloped}{$> 40$}]
        [Yes, my label={above, sloped}{$31\dots 40$}]]
      ]
    ]
  ]
\end{forest}
\end{document}

修剪过的树

类似地,例如,要删除左侧Yes主节点下的节点,只需删除该行即可。Age

\begin{forest}
  shade me/.style={%
    bottom color=#1!25,
    top color=#1!5,
    draw=#1,
    drop shadow,
    font=\sffamily,
  },
  my label/.style n args=2{%
    edge label={node [midway, black, font=\sffamily\scriptsize, #1] {#2}}
  },
  where n children=0{%
    shade me=blue!50!cyan,
  }{%
    if level=0{%
      shade me=green!75!gray,
    }{%
      shade me=blue!75!magenta,
    }
  },
  for tree={%
    delay={content/.wrap value={\strut #1}},
    edge={red,->},
    l sep+=25pt,
    s sep+=20pt
  }
  [Income
    [Age, my label={above, sloped}{High}
      [No, my label={above, sloped}{$< 30$}]
    ]
    [CR, my label={above, sloped}{Low}
      [Yes, my label={above, sloped}{Fair}]
      [Age, my label={above, sloped}{Excellent}
        [No, my label={above, sloped}{$> 40$}]
        [Yes, my label={above, sloped}{$31\dots 40$}]]
      ]
    ]
  ]
\end{forest}

进一步修剪

如果您想要常规方向,即使只有一个子节点,而不是No直接在下方Age,您也可以使用phantom节点。例如,

  [Income
    [Age, my label={above, sloped}{High}
      [No, my label={above, sloped}{$< 30$}]
      [, phantom]
    ]

与幻影

相关内容