0

Here is the sample latex code i found for drawing tree

    \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}

However when I try to modify it, it didnt work,I thought to remove student node and all leaves linked it, I can just remove this code and it will be ok,but it wasnt.I have checked the brackets and parentheses they are all closed.

[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]
    ]

Other thing I wanted to change to delete node yes which is under age node and leave just node "no". So I have removed this line but it didnt help.

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

To remove the student node and those linked to it, you just need to remove the relevant lines of code from the tree specification. However, you must not leave blank lines within the forest environment.

\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}

pruned tree

Similarly, to remove the Yes node under the main Age node on the left, for example, you can just remove the line.

\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}

further pruning

If you want the regular direction, even though there's only one child, rather than the No directly below Age, you can use a phantom node. For example,

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

with phantom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.