连续移动森林博弈树的改进

连续移动森林博弈树的改进

我需要有关 Forest 环境中应用程序的帮助。我想在 Forest 中创建一棵游戏树,其中有连续的移动。这些移动用弧线标记,如图上部所示(字母 B 上方)。我已经能够通过查看此处另一个问题的答案成功实现这一点。但是,在这个特定情况下,我想在字母 D 上方以相同的方式再画一条弧线。我认为我需要事先定义 Forest 不仅在级别 0 上这样做,而且在级别 2 上也这样做。但是,我已经尝试了不同的方法,但遗憾的是没有得到令人满意的结果。如果您继续使用现有代码(如果可能),并且不建议任何其他可能合适的环境,我将不胜感激。

\documentclass[tikz, border=10pt, multi]{standalone}
\usepackage{amsmath}
\usepackage{forest} 

\usetikzlibrary{angles}
\usetikzlibrary{decorations.pathreplacing,calligraphy,backgrounds, through, calc}
\usetikzlibrary{through,calc}

%---------------------------------------------------------

\forestset{%
  auto edge label/.style={%
    before typesetting nodes={%
      if level=0{}{
        if={n()>(n_children("!u")/2)}{
          if={n()==((n_children("!u")+1)/2)}{
            edge label/.wrap value={
              node[midway, right] {$##1$}
            },
          }{
            edge label/.wrap value={
              node[midway, outer sep=1.5mm, right] {$##1$}
            },
          },
        }{
          edge label/.wrap value={
            node[midway, outer sep=1.5mm, left] {$##1$}
          },
        }
      },
    },
  },
  nice empty nodes/.style={% page 65 of the manual - this is from the linguistics library
    for tree={
      calign=fixed edge angles
    },
    delay={
      where content={}{
        shape=coordinate,
        for parent={
          for children={anchor=north}
        }
      }{}
    }
  },
  arc below/.style={
    tikz+={%
      \clip (.center) coordinate (o) -- (!1.north) coordinate (a) |- (!2.north) coordinate (b) -| (!3.north) coordinate (c) -- cycle;
      \node [draw, circle through={(b)}] at (o) {};
      \draw [\forestoption{edge}] () -- ($(o)!1!-35:(b)$) ($(o)!1!35:(b)$) -- ();
    },
    for children={
      if n=2{}{no edge},
    }
  }
}
\tikzset{%
  my circle/.style={draw, circle}
}



\begin{document}

\begin{forest}
  for tree={
    l sep=2em,
    s sep=4em,
    auto edge label,
    nice empty nodes,
    math content
    }
  [A, my circle, arc below
    [, coordinate, edge label={0}]
    [B, my circle, edge label={}
        [{C}, my circle, edge label=0,
            [, coordinate , edge label={0}]
            [D, my circle, edge label={}
                [E, my circle, edge label={0}
                    [{0}, edge label={0}]
                    [{0}, edge label={0}]
                ]
                    [{0}, edge label={0}]
            ]
            [ , coordinate, edge label={0}]
       ]
    [{0}, edge label={0}]
    ]
    [, coordinate, edge label={0}]
  ]
\end{forest}

\end{document}

这是我当前代码的输出。

答案1

为了在节点“C”下方绘制弧,我们只需arc below在其规范中添加即可。但是:

tikz+定义中的的内容arc below应包含在scope环境中,否则“A”弧的剪切将应用于图片的其余部分,包括“C”下方的预期弧。(请注意,“图片的其余部分”不包括节点“A”之后的节点(及其边),因为 Forest 首先绘制所有节点,然后绘制所有边,然后才绘制附加tikz代码。)

\documentclass[tikz, border=10pt, multi]{standalone}
\usepackage{amsmath}
\usepackage{forest} 

\usetikzlibrary{angles}
\usetikzlibrary{decorations.pathreplacing,calligraphy,backgrounds, through, calc}
\usetikzlibrary{through,calc}

%---------------------------------------------------------

\forestset{%
  auto edge label/.style={%
    before typesetting nodes={%
      if level=0{}{
        if={n()>(n_children("!u")/2)}{
          if={n()==((n_children("!u")+1)/2)}{
            edge label/.wrap value={
              node[midway, right] {$##1$}
            },
          }{
            edge label/.wrap value={
              node[midway, outer sep=1.5mm, right] {$##1$}
            },
          },
        }{
          edge label/.wrap value={
            node[midway, outer sep=1.5mm, left] {$##1$}
          },
        }
      },
    },
  },
  nice empty nodes/.style={% page 65 of the manual - this is from the linguistics library
    for tree={
      calign=fixed edge angles
    },
    delay={
      where content={}{
        shape=coordinate,
        for parent={
          for children={anchor=north}
        }
      }{}
    }
  },
  arc below/.style={
    tikz+={%
      \begin{scope}
        \clip (.center) coordinate (o) -- (!1.north) coordinate (a) |- (!2.north) coordinate (b) -| (!3.north) coordinate (c) -- cycle;
        \node [draw, circle through={(b)}] at (o) {};
        \draw [\forestoption{edge}] () -- ($(o)!1!-35:(b)$) ($(o)!1!35:(b)$) -- ();
      \end{scope}
    },
    for children={
      if n=2{}{no edge},
    }
  }
}
\tikzset{%
  my circle/.style={draw, circle}
}



\begin{document}

\begin{forest}
  for tree={
    l sep=2em,
    s sep=4em,
    auto edge label,
    nice empty nodes,
    math content
    }
  [A, my circle, arc below
    [, coordinate, edge label={0}]
    [B, my circle, edge label={}
        [{C}, my circle, edge label=0, arc below,
            [, coordinate , edge label={0}]
            [D, my circle, edge label={}
                [E, my circle, edge label={0}
                    [{0}, edge label={0}]
                    [{0}, edge label={0}]
                ]
                    [{0}, edge label={0}]
            ]
            [ , coordinate, edge label={0}]
       ]
    [{0}, edge label={0}]
    ]
    [, coordinate, edge label={0}]
  ]
\end{forest}

\end{document}

相关内容