How to add newline within node using TikZ?
One simple method is to specify text characteristics within the node : text width, etc. This will let you do exactly what you want, without any extra package. For example,
\documentclass[]{minimal}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{tikz}
\usetikzlibrary{automata,positioning}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=5cm,on grid,auto]
\node[state,initial] (q_0) {$q_0$};
\path[->] (q_0) edge[loop above] node[text width=1cm,align=center] {0,1,2\\3,4,5} (q_0);
\end{tikzpicture}
\end{document}
The result is
Based on Frédérics answer and Chans comment you can just do:
\documentclass[]{minimal}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{tikz}
\usetikzlibrary{automata,positioning}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=5cm,on grid,auto]
\node[state,initial] (q_0) {$q_0$};
\path[->] (q_0) edge[loop above] node[align=center] {0,1,2\\3,4,5} (q_0);
\end{tikzpicture}
\end{document}
You don't need the text width
option, just the align
You could use the makecell
package. As stated in the package documentation, it provides
\makecell[<vertical or/and horizontal alignment>]{<cell text>}
that aids in the creation of (small-scale) multi-lined tabular cell. In that regard, consider the following alteration to your code:
...
\usepackage{makecell}%
...
\path[->]
(q_0) edge node {0,1} (q_1)
edge [loop above] node {\makecell[l]{0,1,2,\\3,4,5}} (q_0)
(q_1) edge node {0,1} (q_2)
(q_2) edge [loop right] node {1} (q_2)
; %end path