# Tikz Automata Figure

I'm trying to recreate this figure using Tikz. Since it is quite large, I've decided to put inside a sidewaysfigure environment (from the 'rotating' package).

Following several examples on this page and the TikZ & PGF manual, I have the following code:

\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}


This code generates this figure, which has the following problems:

• Some nodes are overlapping
• The text above some paths is too long, going into the nodes
• Some nodes are too far apart (causing the mentioned overlap problems)

I'm certain there is some way to fix these problems easily for someone who knows Tikz better, but I'm unsure how to do so. Is there some way to manually adjust the angles between the nodes, so they're closer to each other (so the overlap problem can be fixed on the right side). Any help is appreciated!

• Note that one part of the figure is not yet added (the dotted arc lines connecting some of the nodes), but that's just because I didn't have time for it yet. – Olorun Dec 21 '15 at 7:19
• You can set the distance between invididual nodes with e.g. above=1cm of .. and below right=2cm and 1cm of .... Negative lengths also work – Torbjørn T. Dec 21 '15 at 7:32
• You can also try drawing an horizontal tree with TiKZ or forest. – Ignasi Dec 21 '15 at 7:47
• @Ignasi Didn't know about horizontal trees before, seems similar to what I'm doing - but in my figure there are earlier "end-nodes", i.e. the $(\theta_1,0)$ parts (node_5 and node_6 in my code). – Olorun Dec 21 '15 at 9:24
• I thought I answered that already, though I was perhaps not very explicit: e.g. below right=2cm and 1cm of ... One of those lengths define the horizontal distance, the other the vertical distance. (I can never remember which is which.) – Torbjørn T. Dec 21 '15 at 10:54

I little clean up your MWE of (to my opinion) unnecessary stuff (like positioning nodes on edges, now is determined by option auto), and added option align=center to node where a text to be in two lines. Also I manually adjust some distances, so the overlapping is avoided. See, If this can serve to your need:

\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}


• Nice, didn't know it was actually possible to use start a new line inside the text of the path. That certainly saves a lot of space! – Olorun Dec 21 '15 at 12:07
• @Olorun Note that with the default article the diagram is quite a bit larger than the text area (add \usepackage{showframe} to see this), so ideally one would perhaps want to compress it further. Don't think that's a big problem though, there's a lot of room still. – Torbjørn T. Dec 21 '15 at 12:51
• Yes, I've noticed that. But scaling it down is not a big issue so far, but I'll see which option my professor prefers. I was most interested in learning some useful ideas (like the manual changes you posted). – Olorun Dec 21 '15 at 12:54
• For reducing of image size is better than scaling decrease node distances determined by node distance as well manually set ones. As mentioned @TorbjørnT. in his comment, there is still a lot of room for this. – Zarko Dec 21 '15 at 13:02

Based on the comments of Torbjørn T., I've manually changed the positions of the nodes to get rid of the overlap.

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


I've included one line of how it looks in general, since I assume the reader is not really interested in all the manual edits I've done. The first part after the "=" sign is for the vertical location (so it's 0.5 cm to the above in the example code), whereas the second part is for the horizontal location (so it's 2cm to the right in our example code).

To add the dotted arced lines, I've used the following code (easily found on tex.SE):

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