Tikz 自动机模型

Tikz 自动机模型

我正在尝试重新创造使用 Tikz 绘制图形。由于它相当大,我决定将其放入 sidewaysfigure 环境中(来自“旋转”包)。

按照本页和 TikZ 和 PGF 手册上的几个示例,我有以下代码:

\documentclass{article}
\usepackage{subcaption}
\usepackage{pgfplots}
\usepackage{rotating}
\usetikzlibrary{automata, chains}

\begin{sidewaysfigure}  
    \begin{tikzpicture}[shorten >=1pt,node distance=2.5cm] 
    \node        (node_start)   {chance $0$}; 
    \node[state] (node_1) [above right=of node_start] {$2,S$}; 
    \node[state] (node_2) [below right=of node_start] {$2,W$};
    \node[state] (node_3) [right=of node_1]  {$1$};
    \node[state] (node_4) [right=of node_2]  {$1$};
    \node        (node_5) [above=of node_1] {$(\theta_1,0)$};
    \node        (node_6) [below=of node_2] {$(\theta_1,0)$};
    \node        (node_7) [above left=of node_3] {$(0,\theta_2)$};
    \node        (node_8) [above right=of node_3] {$(-\psi_1, \theta_2 - \psi_2)$};
    \node        (node_9) [below left=of node_4] {$(0,\theta_2)$};
    \node        (node_10) [below right=of node_4] {$(\theta_1 - \psi_1, -\psi_2)$};

    \node       (node_01_start) [right= of node_3] {O chance};
    \node       (node_02_start) [right= of node_4] {O chance};
    \node[state] (node_03) [above right=of node_01_start] {$1$};
    \node[state] (node_04) [below right=of node_01_start] {$1$};
    \node[state] (node_05) [above right=of node_02_start] {$1$};
    \node[state] (node_06) [below right=of node_02_start] {$1$};

    \node       (node_07) [above right=of node_03] {$(-\kappa_1, \theta_2 - \kappa_2)$};
    \node       (node_08) [below right=of node_03] {$(-\psi_1 - \kappa_1, \theta_2 - \psi_2-\kappa_2)$};
    \node       (node_09) [above right=of node_04] {$(-\kappa_1, \theta_2 - \kappa_2)$};
    \node       (node_010) [below right=of node_04] {$(-\psi_1 - \kappa_1, \theta_2 - \psi_2-\kappa_2)$};
    \node       (node_011) [above right=of node_05] {$(-\kappa_1, \theta_2 - \kappa_2)$};
    \node       (node_012) [below right=of node_05] {$(-\theta_1 - \psi_1 - \kappa_1, -\psi_2 - \kappa_2)$};
    \node       (node_013) [above right=of node_06] {$(-\kappa_1, \theta_2 - \kappa_2)$};
    \node       (node_014) [below right=of node_06] {$(-\theta_1 - \psi_1 - \kappa_1, -\psi_2 - \kappa_2)$};


    \path[->] 
    (node_start) edge  node [above left]    {$p$} (node_1)
                 edge  node [below left]    {$1-p$} (node_2)
    (node_1)     edge  node [right]         {give-in} (node_5)
    (node_1)     edge  node [above]         {fight} (node_3)
    (node_2)     edge  node [above]         {fight} (node_4)
    (node_2)     edge  node [right]         {give-in} (node_6)
    (node_3)    edge    node [above left]   {give-in} (node_7)
    (node_3)    edge    node [below left]   {fight} (node_8)
    (node_4)    edge    node [above]    {give-in} (node_9)
    (node_4)    edge    node [below left]   {fight} (node_10)

    (node_3)    edge    node [above]        {information update} (node_01_start)
    (node_4)    edge    node [above]        {information update} (node_02_start)

    (node_01_start) edge  node [above left]     {$1-\varepsilon$} (node_03)
                    edge  node [above right]    {$\varepsilon$} (node_04)
    (node_02_start) edge  node [above left]     {$\varepsilon$} (node_05)
                    edge  node [above right]    {$1-\varepsilon$} (node_06);                    


    \draw[dashed]  (node_3) to node[left]{$I_1$}(node_4);   
    \end{tikzpicture}
    \caption*{A model with information improvement}
\end{sidewaysfigure}

此代码生成图,存在以下问题:

  • 一些节点重叠
  • 某些路径上方的文本太长,进入节点
  • 一些节点相距太远(导致提到的重叠问题)

我确信对于更了解 Tikz 的人来说,有某种方法可以轻松解决这些问题,但我不确定如何做。有没有办法手动调整节点之间的角度,使它们彼此更接近(这样就可以在右侧修复重叠问题)。任何帮助都值得感激!

答案1

我稍微清理了一下你的 MWE,去掉了(在我看来)不必要的东西(比如在边缘上定位节点,现在由选项决定auto),并align=center在节点中添加了文本分为两行的选项。我还手动调整了一些距离,这样就避免了重叠。看看,如果这能满足你的需要:

\documentclass{article}
\usepackage{subcaption}
\usepackage{pgfplots}
\usepackage{rotating}
\usetikzlibrary{automata, positioning}

\begin{document}
\begin{sidewaysfigure}
    \begin{tikzpicture}[
shorten >=1pt,
node distance=22mm,
auto]
    \node        (node_start)   {chance $0$};
    \node[state] (node_1) [above right=31mm and 22mm of node_start] {$2,S$};
    \node[state] (node_2) [below right=31mm and 22mm of node_start] {$2,W$};
    \node[state] (node_3) [right=of node_1]  {$1$};
    \node[state] (node_4) [right=of node_2]  {$1$};
    \node        (node_5) [above=of node_1] {$(\theta_1,0)$};
    \node        (node_6) [below=of node_2] {$(\theta_1,0)$};
    \node        (node_7) [above  left=22mm and 7mm of node_3] {$(0,\theta_2)$};
    \node        (node_8) [above right=22mm and 7mm of node_3] {$(-\psi_1, \theta_2 - \psi_2)$};
    \node        (node_9) [below  left=22mm and 7mm of node_4] {$(0,\theta_2)$};
    \node        (node_10)[below right=22mm and 7mm of node_4] {$(\theta_1 - \psi_1, -\psi_2)$};

    \node       (node_01_start) [right= of node_3] {O chance};
    \node       (node_02_start) [right= of node_4] {O chance};
    \node[state] (node_03) [above right=of node_01_start] {$1$};
    \node[state] (node_04) [below right=of node_01_start] {$1$};
    \node[state] (node_05) [above right=of node_02_start] {$1$};
    \node[state] (node_06) [below right=of node_02_start] {$1$};
\begin{scope}[node distance=3mm and 22mm]
    \node       (node_07) [above right=of node_03] {$(-\kappa_1, \theta_2 - \kappa_2)$};
    \node       (node_08) [below right=of node_03] {$(-\psi_1 - \kappa_1, \theta_2 - \psi_2-\kappa_2)$};
    \node       (node_09) [above right=of node_04] {$(-\kappa_1, \theta_2 - \kappa_2)$};
    \node       (node_010) [below right=of node_04] {$(-\psi_1 - \kappa_1, \theta_2 - \psi_2-\kappa_2)$};
    \node       (node_011) [above right=of node_05] {$(-\kappa_1, \theta_2 - \kappa_2)$};
    \node       (node_012) [below right=of node_05] {$(-\theta_1 - \psi_1 - \kappa_1, -\psi_2 - \kappa_2)$};
    \node       (node_013) [above right=of node_06] {$(-\kappa_1, \theta_2 - \kappa_2)$};
    \node       (node_014) [below right=of node_06] {$(-\theta_1 - \psi_1 - \kappa_1, -\psi_2 - \kappa_2)$};
\end{scope}

    \path[->]
   (node_start) edge node {$p$}     (node_1)
                edge node {$1-p$}   (node_2)
    (node_1)    edge node {give-in} (node_5)
    (node_1)    edge node {fight}   (node_3)
    (node_2)    edge node {fight}   (node_4)
    (node_2)    edge node {give-in} (node_6)
    (node_3)    edge node {give-in} (node_7)
    (node_3)    edge node {fight}   (node_8)
    (node_4)    edge node {give-in} (node_9)
    (node_4)    edge node {fight}   (node_10)

    (node_3)    edge node [align=center] {information\\ update} (node_01_start)
    (node_4)    edge node [align=center] {information\\ update} (node_02_start)

    (node_01_start) edge  node {$1-\varepsilon$}    (node_03)
                    edge  node {$\varepsilon$}      (node_04)
    (node_02_start) edge  node {$\varepsilon$}      (node_05)
                    edge  node {$1-\varepsilon$}    (node_06)

    (node_03) edge (node_07.west)
    (node_03) edge (node_08.west)
    (node_04) edge (node_09.west)
    (node_04) edge (node_010.west)
    (node_05) edge (node_011.west)
    (node_05) edge (node_012.west)
    (node_06) edge (node_013.west)
    (node_06) edge (node_014.west)
                        ;


    \draw[dashed]  (node_3) to node[left]{$I_1$}(node_4);
    \end{tikzpicture}
    \caption*{A model with information improvement}
\end{sidewaysfigure}
\end{document}

在此处输入图片描述

答案2

根据 Torbjørn T. 的评论,我手动更改了节点的位置以消除重叠。

\node       (node_07)  [above right=0.5cm and 2cm of node_03] {$(0,\theta_2)$};

我添加了一行来描述它的总体外观,因为我认为读者并不真正关心我所做的所有手动编辑。“=”符号后的第一部分表示垂直位置(因此在示例代码中它位于上方 0.5 厘米处),而第二部分表示水平位置(因此在我们的示例代码中它位于右侧 2 厘米处)。

为了添加虚线弧线,我使用了以下代码(在 tex.SE 上很容易找到):

\draw[dashed]  (node_03) to[bend right=30] node[below left]{$I_2$}(node_05);

相关内容