101

I want to break the input of a path in order to draw pushdown automaton, so I tried to use the break line symbol \\ and even $$ $$, but it still doesn't break the lines. Example image
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}

logi-kal
  • 115
roxrook
  • 9,907

4 Answers4

104

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

enter image description here

Frédéric
  • 11,087
  • 3
  • 37
  • 43
  • Very nice! Thank you. In fact, I was playing around with text-width but haven't figured out its rule. – roxrook Jul 28 '11 at 22:45
  • 18
    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
  • 1
    @Jake: Thank you. Could you explain how does the alignment take effect on the line break symbols '\'? – roxrook Jul 29 '11 at 01:40
  • 8
    @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 01:45
  • 1
    @Jake: I guess so ;). Thanks for the reference. – roxrook Jul 29 '11 at 05:04
  • I think \\ is not necessary with text width specified. – zyy Mar 10 '22 at 11:39
68

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

Thomas
  • 931
27

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

Multi-lined node input

Werner
  • 603,163
2

You could use \shortstack[alignment]{text1 \\ text1 \\ text1 etc. }
Where alignment can take:
r - right aligned
l - left aligned
c - centered (default)

\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}

\tikzstyle{box} = [rectangle, rounded corners, minimum width=2cm, minimum height=1cm,text centered, draw=black, ]

\begin{tikzpicture}[node distance=2cm] \node (box1) [box] {\shortstack{text1 \ text2} }; \node (box2) [box, right of=box1, xshift=1cm] {Process 2b}; \end{tikzpicture}

enter image description here