# How to add newline within node using TikZ?

I want to break the input of a path in order to draw Pushdown Automaton,so I try to use the break line symbol \\ and even , it still doesnt break the lines.
For example, the input should be
0, 1, 2
3, 4, 5

Any idea? Thank you.

Code Sample:

\documentclass[10pt,letterpaper]{article}
\usepackage[latin1]{inputenc}
\usepackage[left=1in,right=1in,top=1in,bottom=1in]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{automata,positioning}

\begin{document}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex}

\textbf{PDA:}\\
\begin{tikzpicture}[shorten >=1pt,node distance=5cm,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$};
\node[state]            (q_3)   [below=of q_1]  {$q_3$};

\path[->]
(q_0)   edge                    node {0,1}            (q_1)
edge    [loop above]    node {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
\end{tikzpicture}
\\
\end{document}


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

• Very nice! Thank you. In fact, I was playing around with text-width but haven't figured out its rule. – Chan Jul 28 '11 at 22:45
• You don't need to specify the text width, it's enough to only specify the alignment to get the same output. – Jake Jul 28 '11 at 23:06
• @Jake: Thank you. Could you explain how does the alignment take effect on the line break symbols '\\'? – Chan Jul 29 '11 at 1:40
• @Chan: The alignment options are explained starting on page 180 of the pgfmanual. align=center centers the text, align=left aligns it to the left, and so on. Is that what you meant? – Jake Jul 29 '11 at 1:45
• @Jake: I guess so ;). Thanks for the reference. – Chan Jul 29 '11 at 5:04

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

• I wonder why this is not marked as the correct answer. I used to play with different possible text widths in order to get newline in node label – Cyriac Antony Jun 5 '19 at 10:30
• This is the only correct answer. – barfoos May 12 at 11:11

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