1

I currently have a Forest binary tree as follows:

enter image description here

This is produced by the code:


% Adapted from https://tex.stackexchange.com/questions/425238/horizontal-probability-tree-using-forest-with-edge-labels
\documentclass{standalone}

\usepackage{standalone}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\usepackage{xcolor}
\usepackage{forest}
\usepackage{amsmath}
\usepackage{MnSymbol}
\usepackage{commath}

\forestset{
  ptree/.style={
    for tree={
      grow'=0,
      parent anchor=children,
      child anchor=parent,
      l=2.4cm,
      s sep=0.5cm,
      fit=rectangle
    },
    before typesetting nodes={
      for tree={
        split option={content}{:}{content, my edge label},
      },
    },
  },
  my edge label/.style={
    if={
      > O_= {n'}{1}
    }{
      edge label={node [midway, fill=white, font=\color{red}\footnotesize] {#1} }
    }{
      edge label={node [midway, fill=white, font=\color{red}\footnotesize] {#1} }
    },
  }
}

\begin{document}
\begin{forest}
    ptree
    [1
        [2: 1
            [1: 1
                [2: 1
                    [{(1,-1)}: 1]
                ]
                [{(-1,1)}: 2]
            ]
            [1: 2
                [{(-1,1)}: 1]
            ]
        ]
        [2: 2
            [1: 1
                [{(-1,1)}: 1]
            ]
            [{(1,-1)}: 2]
        ]
    ]
\end{forest}
\end{document}

This is perfect for what I need it do, except I’m hoping to pushed dashed ellipses encompassing arbitrary ellipses, often with multiple in the same tree. I put an example of what I mean below.

enter image description here

As well, to make it easier to code rapidly (since I’m using this for lecture notes), I was hoping to simply modify the forest tree to look like:


\begin{forest}
    ptree
    [1
        [2: 1
            [1: 1: A
                [2: 1
                    [{(1,-1)}: 1]
                ]
                [{(-1,1)}: 2]
            ]
            [1: 2: A
                [{(-1,1)}: 1]
            ]
        ]
        [2: 2: B
            [1: 1: B
                [{(-1,1)}: 1]
            ]
            [{(1,-1)}: 2]
        ]
    ]
\end{forest}

where A and B will be used to designate two separate ellipses. As well, occasionally, these ellipses may encompass three or more nodes, so I am hoping they can expand as needed.

Any help changing the \forestset would be very much appreciated!

Felix Jen
  • 145
  • Please attribute code and provide a link to the source. It is rude not to and a link is often enormously helpful to potential helpers. – cfr May 05 '18 at 01:47
  • @cfr I've put a link to the original question where I got the code! Sorry about that! – Felix Jen May 05 '18 at 02:32

2 Answers2

5

A rather similar question has been answered here, but I am not sure it works with forest and, more importantly, rotates the ellipses. For the latter, I am using one proposal of this answer. I'd love to use fit but that fails when the nodes are just separated vertically. Here I draw the ellipses around a given node and another node that gets specified in the relative node syntax, see page 8 of the forest manual.

\documentclass[tikz,border=3.14pt]{standalone}
\usepackage[T1]{fontenc}
\usetikzlibrary{decorations.pathreplacing,shapes.geometric,calc}
\usepackage{xcolor}
\usepackage{forest}
\usepackage{amsmath}
\usepackage{MnSymbol}
\usepackage{commath}

\forestset{
  ptree/.style={
    for tree={
      grow'=0,
      parent anchor=children,
      child anchor=parent,
      l=2.4cm,
      s sep=0.5cm,
      fit=rectangle
    },
    before typesetting nodes={
      for tree={
        split option={content}{:}{content, my edge label},
      },
    },
  },
  my edge label/.style={
    if={
      > O_= {n'}{1}
    }{
      edge label={node [midway, fill=white, font=\color{red}\footnotesize] {#1} }
    }{
      edge label={node [midway, fill=white, font=\color{red}\footnotesize] {#1} }
    },
  },
  myellipse/.style={ tikz={% from https://tex.stackexchange.com/a/186673/121799
  \path let
         \p1 = ($()-(#1)$),
         \p2 = ($(.north east)-(.south west)$),
         \p3 = ($(#1.north east)-(#1.south west)$),
         \n2 = {veclen(\p2)},
         \n3 = {veclen(\p3)},
         \n1 = {veclen(\p1)+0.5*(\n2+\n3)}
         in %\pgfextra{\typeout{\n1,\n2,\n3}}
         () -- (#1) 
         node[midway, sloped, draw, ellipse,dashed, 
              minimum width=\n1, minimum height={max(\n2,\n3)}] {};
  }}
}

\begin{document}
\begin{forest}
    ptree
    [1
        [2: 1
            [1: 1
                [2: 1
                    [{(1,-1)}: 1]
                ]
                [{(-1,1)}: 2]
            ]
            [1: 2,myellipse=!s
                [{(-1,1)}: 1]
            ]
        ]
        [2: 2
            [1: 1,myellipse=!u
                [{(-1,1)}: 1]
            ]
            [{(1,-1)}: 2]
        ]
    ]
\end{forest}
\end{document}

enter image description here

EDIT: Replaced the hardcoded additional distance by the size of the respective nodes.

  • Thank you so much! This is exactly what I was looking for! – Felix Jen May 04 '18 at 19:40
  • @FelixJen You can get the requested syntax: you just need to split to three and collect the third bits into a register which gets turned into an ellipse in a later stage or cycle. – cfr May 05 '18 at 01:50
2

Another solution to use istgame package (version 2.0). You can simply use the macro \xtInfosetO to draw sloped oval type information sets.

enter image description here

\documentclass{standalone}
\usepackage{istgame}

\begin{document}

\begin{istgame} \setistgrowdirection'{east} \tikzset{oval node/.style={ellipse node,draw=none}} \tikzset{move/.style={red,fill=white}} \xtdistance{25mm}{25mm} \istroot(0)[null node]<180>{1}+25mm..40mm+ \istb{1}[move] \istb{2}[move] \endist \istrooto(1)(0-1){2} \istb{1}[move] \istb{2}[move] \endist \istrooto(2)(0-2){2} \istb{1}[move] \istb{2}[move]{(-1,1)} \endist \istrooto(3)(1-1){1} \istb{1}[move] \istb{2}[move]{(-1,1)} \endist \istrooto(4)(1-2){1} \istb{1}[move]{(-1,1)} \endist \istrooto(5)(2-1){1} \istb{1}[move]{(-1,1)} \endist \istrooto(6)(3-1){2} \istb{1}[move]{(-1,1)} \endist \setxtinfosetlayer{main} \xtInfosetOdashed,thick(4)(1.5em) \xtInfosetOellipse,dashed,thick(5)(2em) \end{istgame} \end{document}

I. Cho
  • 2,985