使用 TikZ 生成的游戏树标签中的方程式换行符

使用 TikZ 生成的游戏树标签中的方程式换行符

我使用本手册来帮助使用 TikZ 绘制游戏树(http://www.sfu.ca/~haiyunc/notes/Game_Trees_with_TikZ.pdf)。

然而,我有一个似乎无法克服的挑战。我的一些终端节点需要在标签中换行。在下面的具体示例中,在第二个分支左侧的终端节点(“Not Resist”之后),我希望将两条垂直线(||)后的方程式放在第一个方程式下方。换句话说,将线分割在 X 和 U_S 之间。

我尝试指定“ align=centre”并使用\\,但没有成功。同样,我尝试在标签内指定方程式环境,但也没有成功。

非常感激任何的帮助。

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}

\tikzset{
    solid node/.style={circle,draw,inner sep=1.5,fill=black},
    hollow node/.style={circle,draw,inner sep=1.5}
}

\begin{tikzpicture}[scale=1.5,font=\footnotesize]
    \tikzstyle{level 1}=[level distance=15mm, sibling distance=20mm]
    \tikzstyle{level 2}=[level distance=15mm, sibling distance=20mm]
          \node(0)[solid node,label=above:{\textbf{Sender}}]{}
              child{node(1)[hollow node, label=below:{$U_S(SQ)$}]{}
              edge from parent node[left,xshift=-3,yshift=5]{\textit{Not Threaten}}
              }
              child{node(2)[solid node, label=above right:{\textbf{Target}}]{}
                  child{node[hollow node, label=below:{$U_S(TS)=X$ \\ $||U_T(TS)=Y$}]{} edge from parent node[left,xshift=-3,yshift=0]{\textit{Not Resist}}}
                  child{node[hollow node, label=below:{$TF$}]{} edge from parent node[right,xshift=3,yshift=0]{\textit{Resist}}}
                  edge from parent node[right,xshift=3,yshift=5]{\textit{Threaten}}
                  };
  \end{tikzpicture}

\end{document}

博弈树模型

答案1

您可以使用\makecell

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{makecell}
\begin{document}

\tikzset{
    solid node/.style={circle,draw,inner sep=1.5,fill=black},
    hollow node/.style={circle,draw,inner sep=1.5}
}

\begin{tikzpicture}[scale=1.5,font=\footnotesize]
    \tikzstyle{level 1}=[level distance=15mm, sibling distance=20mm]
    \tikzstyle{level 2}=[level distance=15mm, sibling distance=20mm]
          \node(0)[solid node,label=above:{\textbf{Sender}}]{}
              child{node(1)[hollow node, label=below:{$U_S(SQ)$}]{}
              edge from parent node[left,xshift=-3,yshift=5]{\textit{Not Threaten}}
              }
              child{node(2)[solid node, label=above right:{\textbf{Target}}]{}
                  child{node[hollow node, label=below:{\makecell{$U_S(TS)=X$ \\ $||U_T(TS)=Y$}}]{} edge from parent node[left,xshift=-3,yshift=0]{\textit{Not Resist}}}
                  child{node[hollow node, label=below:{$TF$}]{} edge from parent node[right,xshift=3,yshift=0]{\textit{Resist}}}
                  edge from parent node[right,xshift=3,yshift=5]{\textit{Threaten}}
                  };
  \end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

摞起来!

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{stackengine}
\usepackage{tikz}
\begin{document}

\tikzset{
    solid node/.style={circle,draw,inner sep=1.5,fill=black},
    hollow node/.style={circle,draw,inner sep=1.5}
}

\begin{tikzpicture}[scale=1.5,font=\footnotesize]
    \tikzstyle{level 1}=[level distance=15mm, sibling distance=20mm]
    \tikzstyle{level 2}=[level distance=15mm, sibling distance=20mm]
          \node(0)[solid node,label=above:{\textbf{Sender}}]{}
              child{node(1)[hollow node, label=below:{$U_S(SQ)$}]{}
              edge from parent node[left,xshift=-3,yshift=5]{\textit{Not Threaten}}
              }
              child{node(2)[solid node, label=above right:{\textbf{Target}}]{}
                  child{node[hollow node, label=below:{\stackunder{$U_S(TS)=X ||$}{%
                     $U_T(TS)=Y$}}]{} edge from parent node[left,xshift=-3,yshift=0]{\textit{Not Resist}}}
                  child{node[hollow node, label=below:{$TF$}]{} edge from parent node[right,xshift=3,yshift=0]{\textit{Resist}}}
                  edge from parent node[right,xshift=3,yshift=5]{\textit{Threaten}}
                  };
  \end{tikzpicture}

\end{document}

在此处输入图片描述

如果希望它对齐,请将其放在标签堆栈中(注意:由于某种原因,需要||在内部进行额外的分组\slignstackunder):

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabstackengine}
\stackMath
\usepackage{tikz}
\begin{document}

\tikzset{
    solid node/.style={circle,draw,inner sep=1.5,fill=black},
    hollow node/.style={circle,draw,inner sep=1.5}
}

\begin{tikzpicture}[scale=1.5,font=\footnotesize]
    \tikzstyle{level 1}=[level distance=15mm, sibling distance=20mm]
    \tikzstyle{level 2}=[level distance=15mm, sibling distance=20mm]
          \node(0)[solid node,label=above:{\textbf{Sender}}]{}
              child{node(1)[hollow node, label=below:{$U_S(SQ)$}]{}
              edge from parent node[left,xshift=-3,yshift=5]{\textit{Not Threaten}}
              }
              child{node(2)[solid node, label=above right:{\textbf{Target}}]{}
                  child{node[hollow node, label=below:{\alignstackunder{U_S(TS)=&X {||}}{%
                     U_T(TS)=&Y}}]{} edge from parent node[left,xshift=-3,yshift=0]{\textit{Not Resist}}}
                  child{node[hollow node, label=below:{$TF$}]{} edge from parent node[right,xshift=3,yshift=0]{\textit{Resist}}}
                  edge from parent node[right,xshift=3,yshift=5]{\textit{Threaten}}
                  };
  \end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

您只需要添加标签样式align=center

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\tikzset{
    solid node/.style={circle,draw,inner sep=1.5,fill=black},
    hollow node/.style={circle,draw,inner sep=1.5},
    every label/.append style={align=center}% <--- added
}
\begin{document}

\begin{tikzpicture}[scale=1.5,font=\footnotesize]
    \tikzstyle{level 1}=[level distance=15mm, sibling distance=20mm]
    \tikzstyle{level 2}=[level distance=15mm, sibling distance=20mm]
          \node(0)[solid node,label=above:{\textbf{Sender}}]{}
              child{node(1)[hollow node, label=below:{$U_S(SQ)$}]{}
              edge from parent node[left,xshift=-3,yshift=5]{\textit{Not Threaten}}
              }
              child{node(2)[solid node, label=above right:{\textbf{Target}}]{}
                  child{node[hollow node, label=below:{$U_S(TS)=X$ \\ $||U_T(TS)=Y$}]{} edge from parent node[left,xshift=-3,yshift=0]{\textit{Not Resist}}}
                  child{node[hollow node, label=below:{$TF$}]{} edge from parent node[right,xshift=3,yshift=0]{\textit{Resist}}}
                  edge from parent node[right,xshift=3,yshift=5]{\textit{Threaten}}
                  };
  \end{tikzpicture}
\end{document}

在此处输入图片描述

答案4

您可以使用amsmathgathered;原因已\!在网站的其他地方解释。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}

\tikzset{
  solid node/.style={circle,draw,inner sep=1.5,fill=black},
  hollow node/.style={circle,draw,inner sep=1.5}
}

\begin{tikzpicture}[scale=1.5,font=\footnotesize]
\tikzset{
  level 1/.style={level distance=15mm, sibling distance=20mm},
  level 2/.style={level distance=15mm, sibling distance=20mm},
}
\node(0)[solid node,label=above:{\textbf{Sender}}]{}
  child{
    node(1)[hollow node, label=below:{$U_S(SQ)$}]{}
    edge from parent node[left,xshift=-3,yshift=5]{\textit{Not Threaten}}
  }
  child{
    node(2)[solid node, label=above right:{\textbf{Target}}]{}
    child{
      node[hollow node, label=below:{
        $\!\begin{gathered}U_S(TS)=X \\ ||U_T(TS)=Y\end{gathered}$}]{} 
      edge from parent node[left,xshift=-3,yshift=0]{\textit{Not Resist}}}
      child{
        node[hollow node, label=below:{$TF$}]{} 
        edge from parent node[right,xshift=3,yshift=0]{\textit{Resist}}}
        edge from parent node[right,xshift=3,yshift=5]{\textit{Threaten}}
  };
\end{tikzpicture}

\end{document}

请注意,\tikzstyle已被弃用。

在此处输入图片描述

相关内容