Fontsize – Changing Font Size in Forest Decision Tree

fontsizeforest

I want to change the font size of the following forest decision tree to \footnotesize. When I add font=\footnotesize to for tree, it only the size of the "Name"s get changed, but not the edge labels ("H" and "T"). If I fiddle with the decision edge label/.style by adding \footnotesize in it, an error would occur:

! TeX capacity exceeded, sorry [input stack size=5000].
\@setfontsize #1#2#3->\@nomath #1
\ifx \protect \@typeset@protect \let \@curr…
l.53 \end{forest}

MWE

\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}
        [;{H}]
        [;{T}]
      ]
      [;{T}
        [;{H}]
        [;{T}]
      ]
    ]
    [Name;{-\epsilon},nomath
      [;{H}
        [;{H}]
        [;{T}]
      ]
      [;{T}
        [;{H}]
        [;{T}]
      ]
    ]
  ]
\end{forest}
\end{document}

Output

enter image description here

Best Answer

The easiest way to set the entire tree this way is to simply put \footnotesize in a group with the tree. For example:

{\footnotesize
  \begin{forest}
    ...
  \end{forest}%
}

Alternatively, you can set the font for every node in the tree's preamble using

 /tikz/every node/.append style={font=\footnotesize},

smaller nodes

\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}{}} % edge label options
\begin{document}
\color{red}
\begin{forest}
  anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
  /tikz/every node/.append style={font=\footnotesize},
  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}
        [;{H}]
        [;{T}]
      ]
      [;{T}
        [;{H}]
        [;{T}]
      ]
    ]
    [Name;{-\epsilon},nomath
      [;{H}
        [;{H}]
        [;{T}]
      ]
      [;{T}
        [;{H}]
        [;{T}]
      ]
    ]
  ]
\end{forest}
\end{document}
Related Question