State transition diagram (TikZ)

TikZ's automata library draws state machines to the standard expected in papers and theses, with proper accepting states and curved transitions.


#What this example shows

  • The automata library, with initial and accepting states.

  • Bent edges, so two transitions between the same pair do not overlap.

  • A self-loop with a positioned label.


#A worked example

Open it, edit it, or copy the source below.

State transition diagram (TikZ)

The source, which you can paste into a new diagram and edit:

\documentclass[border=6pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{automata,positioning,arrows.meta}
\begin{document}
\begin{tikzpicture}[
    shorten >=1pt,
    node distance=3cm,
    on grid,
    auto,
    every state/.style={minimum size=1.1cm}
  ]
  \node[state,initial] (q0) {$q_0$};
  \node[state] (q1) [right=of q0] {$q_1$};
  \node[state,accepting] (q2) [right=of q1] {$q_2$};

  \path[->]
    (q0) edge node {$a$} (q1)
    (q1) edge node {$b$} (q2)
    (q1) edge [loop above] node {$a$} ()
    (q2) edge [bend left=40] node {$\varepsilon$} (q0)
    (q0) edge [bend right=40] node[swap] {$b$} (q2);
\end{tikzpicture}
\end{document}

#Making it your own

  • bend left and bend right stop parallel transitions overlapping.

  • node[swap] flips a label to the other side of its edge.

  • on grid with node distance keeps the spacing even without manual coordinates.



Exact, or not worth drawing.