代码

代码

我已经画好了我想要的结果成为:

在此处输入图片描述

当前的游戏树如下所示:

在此处输入图片描述

随着当前代码

\documentclass[12pt,twoside]{book}%{book}
\usepackage[a4paper, hmargin={2.5cm, 2.5cm}, vmargin={2.5cm, 2.5cm},bindingoffset=6mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage[dvipsnames]{xcolor}
\usetikzlibrary{fit}% <-- new

\begin{document}

\begin{figure}
\begin{tikzpicture}[
                   font = \footnotesize,
edge from parent/.style = {draw ,thin},
      SN/.style = {%solid node
                   circle, inner sep=1.2 ,fill=blue},
      HN/.style = {%hollow node
                   circle, inner sep=1.2, draw=blue,
                   },
 level distance = 25mm,
 level 1/.style = {sibling distance=50mm},
 level 2/.style = {sibling distance=40mm}
                    ]
%---
\linespread{0.8}
%-------
    \node (n0) [HN] {}
        child{ node (n1) [SN]  {}         
                edge from parent node[left]{$n_c$ buy}
            }
        child{ node (n2) [SN]  {} 
            child{node[HN]  {} 
                edge from parent node[left]{$1-n_c$ buy}}            
            child{node[HN]  {}
                edge from parent node[right]{not buy}}
                edge from parent node[right]{not buy}
            };
\draw[<-,shorten <=1pt]   
        (n0) -- + (2,1) node[right,align=left] {Entrepreneur\\ 
                                                sets $P_c$ };
\draw[<-,shorten <=1pt]   
        (n2) -- + (2,1) node[right,align=left] {Entrepreneur\\ 
                                                sets $P_r$ };
\end{tikzpicture}
    \caption{Timing of game}
    \label{Testing label}
    \floatfoot{\textit{Source}: Own illustration}
\end{figure}

\end{tikzpicture}
    \end{document}

我不知道如何在每个阶段添加时间线 -----t=0,1,2,以及如何在每个节点后实现利润和效用函数。非常感谢指导 :)

答案1

缺失的文本已作为labels 添加到节点。我addlabel为方便起见定义了一种样式。amsmath为宏添加了包\text

对于缺失的线条,我使用库的语法calc绘制一条相对于节点的线条,并在该线条的每一端n0添加一个(和)。接下来的两条线用垂直坐标绘制,例如是 x 坐标为、y 坐标为 的点。这样可以轻松绘制相同长度的线条。coordinatel1l2(l1 |- n2)l1n2

我还删除了该xcolor包,因为它已被加载tikz,并将dvipsnames选项移到了文档类。

我不知道它\floatfoot来自哪里,所以它被注释掉了,并且\end{tikzpicture}你拥有的额外内容也被删除了。我还评论了\linespread,你真的想要吗?(我个人会删除它。)

在此处输入图片描述

\documentclass[12pt,twoside,dvipsnames]{book}%{book}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}   
\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[
                   font = \footnotesize,
edge from parent/.style = {draw ,thin},
      SN/.style = {%solid node
                   circle, inner sep=1.2 ,fill=blue,
                   },
      HN/.style = {%hollow node
                   circle, inner sep=1.2, draw=blue,
                   },
      addlabel/.style={label={[align=center]below:#1}},
 level distance = 25mm,
 level 1/.style = {sibling distance=50mm},
 level 2/.style = {sibling distance=40mm}
                    ]
%---
%\linespread{0.8}
%-------
    \node (n0) [HN] {}
        child{ node (n1) [SN,addlabel={$\pi_1=\text{equation 1}$\\$u_1=\text{utility 1}$}]  {}         
                edge from parent node[left]{$n_c$ buy}
            }
        child{ node (n2) [SN]  {} 
            child{node[HN,addlabel={$\pi_2=\text{equation 2}$\\$u_2=\text{utility 2}$}]  {} 
                edge from parent node[left]{$1-n_c$ buy}}            
            child{node[HN,addlabel={0\\0}] (n3) {}
                edge from parent node[right]{not buy}}
                edge from parent node[right]{not buy}
            };
\draw[<-,shorten <=1pt]   
        (n0) -- + (2,1) node[right,align=left] {Entrepreneur\\ 
                                                sets $P_c$ };
\draw[<-,shorten <=1pt]   
        (n2) -- + (2,1) node[right,align=left] {Entrepreneur\\ 
                                                sets $P_r$ };

\draw [dotted] ($(n0.center)+(-4cm,0)$) coordinate (l1) -- ($(n0.center) + (6cm,0)$) coordinate (l2) node[right] {$t=0$};
\draw [dotted] (l1 |- n2) -- (l2 |- n2) node[right] {$t=1$};
\draw [dotted] (l1 |- n3) -- (l2 |- n3) node[right] {$t=3$};
\end{tikzpicture}
    \caption{Timing of game}
    \label{Testing label}
%    \floatfoot{\textit{Source}: Own illustration}
\end{figure}
\end{document}

答案2

解决方案forest

代码

\documentclass[12pt,twoside]{book}%{book}
\usepackage[a4paper, hmargin={2.5cm, 2.5cm}, vmargin={2.5cm, 2.5cm},bindingoffset=6mm]{geometry}
\usepackage{forest,mathtools}
\forestset{
  declare toks={elo}{}, % Edge Label Options
  anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
  dot/.style={tikz={\draw[blue,#1] (.child anchor) circle[radius=1.5pt];}},
  decision edge label/.style n args=3{
    edge label/.expanded={node[midway,auto=#1,anchor=#2,\forestoption{elo}]{\strut\unexpanded{#3}}}
  },
  decision/.style={if n=1
    {decision edge label={left}{east}{#1}}
    {decision edge label={right}{west}{#1}}
  },
  game tree/.style={
    for tree={
      s sep=30mm,l=25mm,
      if n children=0{anchors=north}{
        if n=1{anchors=south east}{anchors=south west}},
      math content,
    },
    anchors=south, 
    dot={fill=white},for descendants={dot={fill=blue}},
    delay={for descendants={split option={content}{;}{decision,content}}},
  },
}
\newcommand\payoff[1]{\begin{matrix}#1\end{matrix}}

\begin{document}

\begin{figure}
\centering
\begin{forest}
  game tree,
  [
    [$n_c$ buy;\payoff{\pi_1=\text{equation 1} \\ u_1=\text{utility 1}}]
    [not buy;
      [$1-n_c$ buy;\payoff{\pi_2=\text{equation 2} \\ u_2=\text{utility 2}},dot={fill=white}]
      [not buy;\payoff{0 \\ 0},dot={fill=white}]
    ]
  ]
  \draw[<-,shorten <=3pt]   
        (!r.anchor) -- + (2,1) node[right,align=left] {Entrepreneur\\ 
                                                sets $P_c$ };
  \draw[<-,shorten <=3pt]   
        (!2.anchor) -- + (2,1) node[right,align=left] {Entrepreneur\\ 
                                                sets $P_r$ };
  \draw[dotted](current bounding box.west|-!r.anchor)--(current bounding box.east|-!r.anchor)node[right,overlay]{$t=0$};
  \draw[dotted](current bounding box.west|-!2.anchor)--(current bounding box.east|-!2.anchor)node[right,overlay]{$t=1$};
  \draw[dotted](current bounding box.west|-!21.anchor)--(current bounding box.east|-!22.anchor)node[right,overlay]{$t=2$};
\end{forest}
    \caption{Timing of game}
    \label{Testing label}
    % \floatfoot{\textit{Source}: Own illustration}
\end{figure}

\end{document}

输出

在此处输入图片描述

答案3

一个较短但不灵活的forest实现:

代码

\documentclass[tikz]{standalone}
\usepackage{forest}\usetikzlibrary{arrows.meta}
\begin{document}
\begin{forest}
  Pin/.style={pin={[pin edge={thin,shorten <=4\pgflinewidth,black,{Latex[round]}-,
    to path={-- (\tikztotarget.west)}}, pin distance=1.5cm, align=left]
    10:Entrepreneur\\ sets $P_#1$}},
  buy/.style={edge label={node[inner sep=.1667em, /forest/if level=2{near end}{midway},
    /forest/if n=1{above left}{above right}] {#1 buy}}},
  c/.style={node options={circle,fill,outer sep=0pt,inner sep=.4mm},anchor=center},
  foo/.style={anchor=parent, child anchor=parent, node options={align=left,
    append after command={(.parent) edge[fill,to path={circle[radius=.4mm]}] ()}},
    content={$\pi_{\pgfmathprint{level}} = {}$equation \pgfmathprint{level}\\
            $u_{\pgfmathprint{level}} = {}$utility \pgfmathprint{level}}},
  for tree={l sep=2.5cm, s sep*=5,if n=1{alias/.wrap pgfmath arg={level-#1}{level()}}{}}
[, c, Pin=c, alias=level-0
 [, foo, buy=$n_c$]
 [, c, buy=not, Pin=r
   [, foo, buy=$1-n_c$]
   [, foo, content={0\\0}, buy=not] ] ]
\path(current bounding box.west)coordinate(cbbw)(current bounding box.east)coordinate(cbbe);
\foreach \l[evaluate={\a={\l==0?"center":"north"};}] in {0,...,2}
  \draw[dashed] (cbbw |- level-\l.\a) -- (cbbe |- level-\l.\a) node[right] {$t=\l$};
\end{forest}
\end{document}

答案4

istgame包(版本 2)提供了用于时间线的宏:\xtTimeLineH\xtTimeLineV。它还提供了用于评论的宏:\xtCommentTo\xtCommentFrom

\xtTimeLineH以下是使用和的示例\xtCommentTo

在此处输入图片描述

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{makecell}
\usepackage{istgame}

\begin{document}

\begin{istgame}[font=\footnotesize]
% tree
\setistmathTF001
\xtShowEndPoints
\xtdistance{25mm}{50mm}
\istroot(0)
  \istb{$n_c$ buy}[al]{\makecell[l]{\pi_1=\text{equation 1}\\u_1=\text{utility 1}}}
  \istb{not buy}[ar] 
  \endist
\istroot(1)(0-2)
  \istb{$1-n_c$ buy}[al]{\makecell[l]{\pi_2=\text{equation 2}\\u_2=\text{utility 2}}}
  \istb{not buy}[ar]{\makecell{0\\0}}
  \endist
% time-lines and comments
\xtTimeLineH(0){-4cm}{6cm}{$t=0$}
\xtTimeLineH(1){-4cm}{6cm}{$t=1$}
\xtTimeLineH(1-1){-4cm}{6cm}{$t=2$}
\xtCommentTo[solid](0)(2,1){\makecell[l]{Entrepreneur\\sets $P_c$}}[r]
\xtCommentTo[solid](1)(2,1){\makecell[l]{Entrepreneur\\sets $P_r$}}[r]
\end{istgame}
\end{document}

相关内容