[Tex/LaTex] How to add caption for a TikZ picture

captionstikz-pgf

I'm drawing a automaton figure, and I want to name it says 'M1' below the figure? A minimal example would be greatly appreciated. Thank you.

\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto] 
    \node[state,initial]    (q_0)                   {$q_0$}; 
    \node[state,accepting]  (q_1)   [right=of q_0]  {$q_1$}; 
    \node[state]            (q_2)   [right=of q_1]  {$q_2$}; 

    \path[->]
    (q_0) edge                  node {a}            (q_1)
    (q_1) edge                  node {$\lambda$}    (q_2)
    (q_2) edge  [bend right]    node {$\lambda$}    (q_0)
    ; %end path 
\end{tikzpicture}

Best Answer

Just include your code in a figure environment. In that way you'll be able to add a caption to it as a normal figure:

\begin{figure}
\centering
\begin{tikzpicture}
<code>
\end{tikzpicture}
\caption{M1} \label{fig:M1}
\end{figure}
Related Question