[Tex/LaTex] Position an automata below another, both centered

automatatikz-pgf

I want to draw two automata in a tikz picture. The second automaton should be below the first, and both should be centered horizontally.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{automata, positioning}

\begin{document}

\begin{tikzpicture}[node distance=15mm, auto]
  % first automaton
  \node[state,initial]   (sA)               {$A$};
  \node[state,accepting] (sB) [right of=sA] {$B$};
  \path[->] (sA) edge[loop below] node {$0$} ()
            (sA) edge[bend left]  node {$1$} (sB)
            (sB) edge[bend left]  node {$0$} (sA);

  % second automaton
  \node[state,initial]   (s0) [below=15mm of sA] {$s_0$};
  \node[state]           (s1) [right of=s0]      {$s_1$};
  \node[state,accepting] (s2) [right of=s1]      {$s_2$};
  \path[->] (s0) edge node {$a$} (s1)
            (s1) edge node {$b$} (s2);
\end{tikzpicture}

\end{document}

two automata in the same tikz picture

How is the best way of getting the automata centered?

EDIT: The standalone class is used here only for illustration purposes. I really intend to use the beamer class.

Best Answer

An easy fix that will work in most classes (other than standalone) is to dedicate a tikzpicture to each automaton (if you can afford it) and use the \centering declaration.

Make sure to leave a blank line between the two tikzpicture environments. You can also add some vertical space between the two.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata, positioning}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[node distance=15mm, auto]
  % first automaton
  \node[state,initial]   (sA)               {$A$};
  \node[state,accepting] (sB) [right of=sA] {$B$};
  \path[->] (sA) edge[loop below] node {$0$} ()
            (sA) edge[bend left]  node {$1$} (sB)
            (sB) edge[bend left]  node {$0$} (sA);
\end{tikzpicture}

\vspace{1em}
\begin{tikzpicture}[node distance=15mm, auto]
  % second automaton
  \node[state,initial]   (s0) [below=15mm of sA] {$s_0$};
  \node[state]           (s1) [right of=s0]      {$s_1$};
  \node[state,accepting] (s2) [right of=s1]      {$s_2$};
  \path[->] (s0) edge node {$a$} (s1)
            (s1) edge node {$b$} (s2);
\end{tikzpicture}
\caption{One automaton, two automata\ldots}
\end{figure}

\end{document}

EDIT: as Gonzalo Medina points out, there is no point in using a float environment, since the class used is beamer. In that case, combining minipage and \centering will do the trick. A center environment can also be used, but takes more vertical space than the latter.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata, positioning}

\begin{document}

\begin{minipage}[b]{\textwidth}
\centering
\begin{tikzpicture}[node distance=15mm, auto]
  % first automaton
  \node[state,initial]   (sA)               {$A$};
  \node[state,accepting] (sB) [right of=sA] {$B$};
  \path[->] (sA) edge[loop below] node {$0$} ()
            (sA) edge[bend left]  node {$1$} (sB)
            (sB) edge[bend left]  node {$0$} (sA);
\end{tikzpicture}

\vspace{1em}
\begin{tikzpicture}[node distance=15mm, auto]
  % second automaton
  \node[state,initial]   (s0) [below=15mm of sA] {$s_0$};
  \node[state]           (s1) [right of=s0]      {$s_1$};
  \node[state,accepting] (s2) [right of=s1]      {$s_2$};
  \path[->] (s0) edge node {$a$} (s1)
            (s1) edge node {$b$} (s2);
\end{tikzpicture}
\end{minipage}

\end{document}