Drawing finite state automata with Tikz

Tikz offers a library for drawing finite state automata (FSA) for embedding in LaTeX. As I mostly use simple FSAs and for making the corresponding Tikz code more expressive, I created two new commands:

  • newState
    Each state has an internal name which may be referred to when drawing edges and a visible label. In contrast toother graph drawing tools such as GraphViz we need to provide positioning information for each node (above of/below of/right of/left of). Each state may bear additional properties such as whether it is an accepting state or  an initial state.
  • newTransition
    A transition connects a source state with a target state, given that some condition is fulfilled guard. An edge may be drawn as a straight line (default) or as an arc (bend left/bend right). For loops you need to use loop above/loop below etc.

Example FSA

Finite State Automaton drawn with Tikz

Finite State Automaton drawn with Tikz

The following example code defines an FSA which accepts words conforming to the regular expression b(an)+e:



% newState{}{}{}{}
% #1: internal name,
% #2: (visible) label,
% #3: node properties (e.g. accepting, initial)
% #4: relative position (e.g. right of=/left of=/above of=/below of=)


% newTransition{}{}{}{}
% #1: source state (internal name),
% #2: target state (internal name)
% #3: guard/edge label
% #4: edge direction (loop below/above, bend right/left)
newcommand{newTransition}[4]{path[->] (#1) edge [#4] node {#3} (#2); } 

    % Default arrow tip
    ->,>=stealth',shorten >=1pt,auto,
    % Default node distance
    node distance=2cm,
    % Edge stroke thickness: semithick, thick, thin

    newState{q1}{$q_1$}{right of=S}{}
    newState{q2}{$q_2$}{below of=q1}{}
    newState{q3}{$q_3$}{right of=q1}{accepting} 

    newTransition{q1}{q2}{a}{bend left}
    newTransition{q2}{q1}{n}{bend left}
  caption{Finite State Automaton, accepting the pattern emph{b(an)+e}}

GraphViz as an Alternative

Another candidate for drawing graphs is GraphViz, which I indeed tested first. GraphViz has some important drawbacks:

  • The subscript tag <SUB> and some others do not work for GraphViz before the version of October 14, 2011, and even then, only for PNG and SVG rendering.[1]
  • The font used for rendering by GraphViz is not synchronized with the font in the final LaTeX document which may be annoying.


  • [1] Martin Thoma’s site with another nice example
  • [2] texamples.net

Leave a Reply