4

Following an example in the forest package documentation, I have made the following decision tree:

\documentclass{article}
\usepackage{mathtools,forest}
\def\getfirst#1;#2\endget{#1}
\def\getsecond#1;#2\endget{#2}
\forestset{declare toks={elo}{}} % edge label options
\begin{document}
\color{red}
\begin{forest}
%=============================================
  anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
  for tree={font=\footnotesize,
    s sep=5mm,l=15mm,
    if n children=0{anchors=north}{
    if n=1{anchors=south east}{anchors=south west}},
    content format={$\forestoption{content}$}
  },
  anchors=south, outer sep=2pt,
  nomath/.style={content format=\forestoption{content}},
  dot/.style={tikz+={\draw[#1](.child anchor)circle[radius=2pt];}},
  dot={fill={white}},for descendants={dot={fill}}, % initial node hollow, rest solid
  decision edge label/.style n args=3{
    edge label/.expanded={node[midway,auto=#1,anchor=#2,\forestoption{elo}]{\strut$#3$}}
  },
  decision/.style={if n=1
    {decision edge label={left}{east}{#1}}
    {decision edge label={right}{west}{#1}}
  },
  delay={for descendants={
    decision/.expanded/.wrap pgfmath arg={\getsecond#1\endget}{content},
    content/.expanded/.wrap pgfmath arg={\getfirst#1\endget}{content},
  }},
%=============================================
  [Name,nomath
    [Name;{+\epsilon},nomath
      [;{H}]
      [;{T}]
    ]
    [Name;{-\epsilon},nomath
      [;{H}]
      [;{T}]
    ]
  ]
\end{forest}
\end{document}

enter image description here

While the syntax of the tree is relatively simple, the preparation work--defining the styles/keys/options (stuff in between the two === lines)--are quite a lot.

If I want to draw multiple such trees in a document with pretty much the same style/key/option setup, is there a way to "pre-define" these in the preamble? I've tried to copy the setup into a \forestset{mytree/.style={...}} command, but then TeX tells me that the number of arguments are wrong. If I use \forestset{mytree/.style n args=3{...}}, I don't know what arguments to give when using mytree inside a forest environment.

1
  • Probably you just forgot to double the hash symbols when defining the style? It works fine for me sticking it in a forest style.
    – cfr
    Commented Nov 14, 2015 at 2:51

1 Answer 1

2

You can create a style to use in the tree's preamble. This is pretty simple:

\forestset{
  decision tree/.style={
    <stuff from preamble>
  }
}

The only thing to remember is not specific to forest and that is that you need to double hash symbols when defining the style in order to pick up the relevant arguments. Otherwise, TeX expects the arguments to be passed to the style and not what the style defines.

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{mathtools,forest}
\def\getfirst#1;#2\endget{#1}
\def\getsecond#1;#2\endget{#2}
\forestset{declare toks={elo}{}}
\forestset{
  decision tree/.style={
    anchors/.style={
      anchor=##1,
      child anchor=##1,
      parent anchor=##1
    },
    for tree={
      font=\footnotesize,
      s sep=5mm,
      l=15mm,
      if n children=0{anchors=north}{
        if n=1{anchors=south east}{anchors=south west}
      },
      content format={$\forestoption{content}$}
    },
    anchors=south,
    outer sep=2pt,
    nomath/.style={content format=\forestoption{content}},
    dot/.style={
      tikz+={
        \draw[##1](.child anchor)circle[radius=2pt];
      }
    },
    dot={fill={white}},
    for descendants={dot={fill}},
    decision/.style={
      if n=1{
        decision edge label={left}{east}{##1}
      }{
        decision edge label={right}{west}{##1}
      }
    },
    decision edge label/.style n args=3{
      edge label/.expanded={node[midway,auto=##1,anchor=##2,\forestoption{elo}]{\strut$##3$}}
    },
    delay={
      for descendants={
        decision/.expanded/.wrap pgfmath arg={\getsecond##1\endget}{content},
        content/.expanded/.wrap pgfmath arg={\getfirst##1\endget}{content},
      }
    },
  }
}
\begin{document}
\color{red}
\begin{forest}
  decision tree,
  [Name,nomath
    [Name;{+\epsilon},nomath
      [;{H}]
      [;{T}]
    ]
    [Name;{-\epsilon},nomath
      [;{H}]
      [;{T}]
    ]
  ]
\end{forest}
\color{blue}
\begin{forest}
  decision tree,
  [Name,nomath
    [Name;{+\epsilon},nomath
      [;{H}]
      [;{T}]
    ]
    [Name;{-\epsilon},nomath
      [;{H}]
      [;{T}]
    ]
  ]
\end{forest}
\end{document}

two tree

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .