绘制游戏树-Tikz 还是 Forest?

绘制游戏树-Tikz 还是 Forest?

我正在尝试绘制以下树: 树

以下是我目前的代码,我不知道如何准确地执行它。另外,我正在使用“forest”。使用 Tikz 会更好吗?

\documentclass{article} 
\usepackage[edges]{forest}
\usepackage[outline]{contour}
\usetikzlibrary{fit,shapes.misc,arrows.meta,positioning}

\begin{document}
\begin{forest}
for tree={s sep=0.1cm,l sep=1.2cm,font=\scriptsize,
where n children=0{align=center,draw,edge={thick,-{Circle}}}            {circle,fill,inner sep=0pt,minimum size=2mm},
where n=1{edge label={node[midway,above left=0pt,font=\scriptsize]{S}}}    {edge
label={node[midway,above right=0pt,font=\scriptsize]{E}}},
edge={thick}
}
  [,alias=T
   [
    [,alias=L
     [
      [{$B-C$ \\ $0$}]
      [{$B$ \\ $X$}]
     ]
     [
      [{$B-L$ \\ $R-L$}]
      [{$B-L$ \\ $R-L$}]
     ]
    ]
    [
     [
      [{$B-C$ \\ $0$}]
      [{$B$ \\ $X$}]
     ]
     [
      [{$B-L$ \\ $R-L$}]
      [{$B-L$ \\ $R-L$}]
     ]
    ]
   ]
   [
    [
     [
      [{$B-C$ \\ $\bar{R}$}]
      [{$B$ \\ $X$}]
     ]
     [
      [{$B-L$ \\ $R-L$}]
      [{$B-L$ \\ $R-L$}]
     ]
    ]
    [,alias=R
     [
      [{$B-C$ \\ $\bar{R}$}]
      [{$B$ \\ $X$}]
     ]
     [
      [{$B-L$ \\ $R-L$}]
      [{$B-L$ \\ $R-L$}]
     ]
    ]
   ]
  ]
\node[draw,dashed,rounded rectangle,fit=(L) (R)]{};  
\node[left=1mm of very left,align=right,font=\scriptsize]{Nature};
\end{forest}

\end{document}

答案1

另一种解决方案是使用游戏包裹:

在此处输入图片描述

\documentclass{standalone}

\usepackage{amsmath}
\usepackage{makecell}

\usepackage{istgame}

\NewDocumentCommand\mact{m}
{\makecell[c]{#1}}
\NewDocumentCommand\mpay{m}
{\begin{matrix}#1\end{matrix}}

\begin{document}

\begin{istgame}[font=\footnotesize]
\setistmathTF*001
\xtShowEndPoints
% top part
\xtdistance{25mm}{120mm}
\istroot(0)[chance node]{Nature}
  \istb{\mact{$R=O$\\cooperative\\partner}}[al]
  \istb{\mact{$R=\bar R$\\noncooperative\\partner}}[ar]  \endist
\xtdistance{25mm}{50mm}
\istroot(A)(0-1)%<180>{Minister}
  \istb{\mact{Introduce\\first proposal}}[l]  \endist
\istroot(B)(0-2)%<0>{Minister}
  \istb{\mact{Introduce\\first proposal}}[r]  \endist
% left part
\xtdistance{25mm}{60mm}
\istroot(A0)(A-1)<180>{Partner}
  \istb{Scrutinize}[l]  \istb{Endorse}[r]  \endist
\xtdistance{25mm}{30mm}
\istroot(A1)(A0-1)<0>{$B-C,O$}
  \istb{\mact{Introduce\\second\\proposal}}[al]  \istb{Delay}[ar]  \endist
\istroot(A2)(A0-2)<180>{$B,X$}
  \istb{\mact{Introduce\\second\\proposal}}[al]  \istb{Delay}[ar]  \endist
\xtdistance{25mm}{15mm}
\istroot(A3)(A1-1)<180>{Partner}
  \istb{Scrut.}[l]{B-C,O}        \istb{Endorse}[r]{B,X}         \endist
\istroot(A4)(A1-2)<0>{Partner}
  \istb{S}[l]{\mpay{B-L,\\R-L}}  \istb{E}[r]{\mpay{B-L,\\R-L}}  \endist
\istroot(A5)(A2-1)<0>{Partner}
  \istb{S}[l]{B-C,O}             \istb{E}[r]{B,X}               \endist
\istroot(A6)(A2-2)<0>{Partner}
  \istb{S}[l]{\mpay{B-L,\\R-L}}  \istb{E}[r]{\mpay{B-L,\\R-L}}  \endist
% right part
\xtdistance{25mm}{60mm}
\istroot(B0)(B-1)<180>{Partner}
  \istb{Scrutinize}[l]  \istb{Endorse}[r]  \endist
\xtdistance{25mm}{30mm}
\istroot(B1)(B0-1)<0>{$B-C,\bar R$}
  \istb{\mact{Introduce\\second\\proposal}}[al]  \istb{Delay}[ar]  \endist
\istroot(B2)(B0-2)<180>{$B,X$}
  \istb{\mact{Introduce\\second\\proposal}}[al]  \istb{Delay}[ar]  \endist
\xtdistance{25mm}{15mm}
\istroot(B3)(B1-1)<180>{Partner}
  \istb{Scrut.}[l]{B-C,\bar R}   \istb{Endorse}[r]{B,X}         \endist
\istroot(B4)(B1-2)<0>{Partner}
  \istb{S}[l]{\mpay{B-L,\\R-L}}  \istb{E}[r]{\mpay{B-L,\\R-L}}  \endist
\istroot(B5)(B2-1)<0>{Partner}
  \istb{S}[l]{B-C,\bar R}        \istb{E}[r]{B,X}               \endist
\istroot(B6)(B2-2)<0>{Partner}
  \istb{S}[l]{\mpay{B-L,\\R-L}}  \istb{E}[r]{\mpay{B-L,\\R-L}}  \endist
% information sets
\xtInfosetO(A)(B){Minister}
\xtCInfosetO(A1)!.35!(B1)<1.15>{Minister}
\xtCInfosetO(A2)!.65!(B2)<1.15>{Minister}
\end{istgame}

\end{document}

答案2

我无法回答这个问题Z 或森林,因为正如 Alan 指出的那样,森林基于 TiZ. 我唯一能做的就是在你的两个节点周围添加弯曲的虚线。这是用样式完成的rconnect

\draw[dashed] let \p1=($(F1.north)-(F1.center)$) in
 [/tikz/rconnect/radius=\y1] [rconnect=LL and RR];

其中是let ... in为了确保尺寸与fit节点的尺寸相匹配。(您可以Nature使用添加节点label,这也可以修复我在运行代码时遇到的小错误。)

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage[edges]{forest}
\usepackage[outline]{contour}
\usetikzlibrary{fit,shapes.misc,arrows.meta,positioning}
\tikzset{rconnect/.style args={#1 and #2}{insert path={
($(#1)+(270+\pgfkeysvalueof{/tikz/rconnect/angle}:\pgfkeysvalueof{/tikz/rconnect/radius})$) 
arc(270+\pgfkeysvalueof{/tikz/rconnect/angle}:90+\pgfkeysvalueof{/tikz/rconnect/angle}:\pgfkeysvalueof{/tikz/rconnect/radius})
to[out=\pgfkeysvalueof{/tikz/rconnect/angle},in=180-\pgfkeysvalueof{/tikz/rconnect/angle}] 
($(#2)+(90-\pgfkeysvalueof{/tikz/rconnect/angle}:\pgfkeysvalueof{/tikz/rconnect/radius})$) 
arc(90-\pgfkeysvalueof{/tikz/rconnect/angle}:-90-\pgfkeysvalueof{/tikz/rconnect/angle}:\pgfkeysvalueof{/tikz/rconnect/radius})
to[in=\pgfkeysvalueof{/tikz/rconnect/angle},out=180-\pgfkeysvalueof{/tikz/rconnect/angle}] 
cycle}},rconnect/.cd,radius/.initial=6.5pt,angle/.initial=-12}
\begin{document}
\begin{forest}
for tree={s sep=0.1cm,l sep=1.2cm,font=\scriptsize,
where n children=0{align=center,draw,edge={thick,-{Circle}}}            {circle,fill,inner sep=0pt,minimum size=2mm},
where n=1{edge label={node[midway,above left=0pt,font=\scriptsize]{S}}}    {edge
label={node[midway,above right=0pt,font=\scriptsize]{E}}},
edge={thick}
}
  [,label=above:Nature,alias=T
   [,alias=L
   [,alias=LL
     [
      [{$B-C$ \\ $0$}]
      [{$B$ \\ $X$}]
     ]
     [
      [{$B-L$ \\ $R-L$}]
      [{$B-L$ \\ $R-L$}]
     ]
    ]
    [,alias=LR
     [
      [{$B-C$ \\ $0$}]
      [{$B$ \\ $X$}]
     ]
     [
      [{$B-L$ \\ $R-L$}]
      [{$B-L$ \\ $R-L$}]
     ]
    ]
   ]
   [,alias=R
    [,alias=RL
     [
      [{$B-C$ \\ $\bar{R}$}]
      [{$B$ \\ $X$}]
     ]
     [
      [{$B-L$ \\ $R-L$}]
      [{$B-L$ \\ $R-L$}]
     ]
    ]
    [,alias=RR
     [
      [{$B-C$ \\ $\bar{R}$}]
      [{$B$ \\ $X$}]
     ]
     [
      [{$B-L$ \\ $R-L$}]
      [{$B-L$ \\ $R-L$}]
     ]
    ]
   ]
  ]
\node[draw,dashed,rounded rectangle,fit=(L) (R)] (F1){};  
\node[draw,dashed,rounded rectangle,fit=(LR) (RL)]{};  
\draw[dashed] let \p1=($(F1.north)-(F1.center)$) in
 [/tikz/rconnect/radius=\y1] [rconnect=LL and RR];
\end{forest}
\end{document}

在此处输入图片描述

相关内容