# Tikz: Multi-line text in the node description [duplicate]

This question already has an answer here:

I use tikz to create pictures in latex. My picture consists of set of nodes. Every node has its own text inside the node, for example, "This node is valuable":

  \node (mynode) [mystyle,right=of anothernode] {This node is valuable};


When I compile the document I see the description as one line.

This node is valuable

Is it possible to split the line into multiple lines? Like this:

This node

is

valuable

Which commands should one apply in order to achieve this?

-
I'm still having trouble with the same thing. Even trying the suggestions here. –  Lucas May 26 '12 at 23:21
–  Qrrbrbirlbel Oct 30 '13 at 16:59
this question is older than the question that you posted, so this should be another way around –  Tim Mar 10 at 16:50
add comment

## marked as duplicate by Adam Liter, Jubobs, Svend Tveskæg, Jesse, ThorstenMar 10 at 17:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

## 3 Answers

A short example derived from the pgf/tikZ 2.10 manual.

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

\begin{document}
\begin{tikzpicture}[%
>=stealth,
node distance=3cm,
on grid,
auto
]
\node[state] (A)              {A};
\node        (B) [right of=A,fill=blue!25,text width=3cm]{This is a demonstration text for showing how line breaking works.};;
\path[->] (A) edge (B);
\end{tikzpicture}
\end{document}


For text alignment there is also the align option.

-
+1 I like the idea: constraint text area with "text width" option and that is how the sentence is split. Thanks! –  Tim Dec 11 '10 at 14:14
You can also use the package varwidth to constrain the node dynamically. When you say text width=3cm the node will always have space for 3cm of text, which makes nodes with little text too big. The varwidth environment grows with the text up to the given width and only then constrains. –  kongo09 Aug 24 '11 at 13:33
add comment

Yes, you can have more than one node part (for more information, refer to the TikZ manual 48.6, p. 447, pgf/tikz 2.10).

A simple example would be like this:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.multipart}

\begin{document}

\begin{tikzpicture}[every text node part/.style={align=center}]
\node (test) [rectangle, draw] {this node \\ has \\ four \\lines};
\end{tikzpicture}

\end{document}

-
I like your proposal: it is simple and fast to check, however, I have got the following error: "! Package pgfkeys Error: I do not know the key '/tikz/align' and I am going to". Is it something connected to configuration? Moreover, when I delete "align=center" I have got a rectangle with all words in one line :( What could be the issue? –  Tim Dec 11 '10 at 14:09
@Tim - I think you are not using the up to date version of pgf (which is 2.10).... Simply exchange ever text node part/.style={align=center} with text centered and it should work! –  Matten Dec 12 '10 at 9:52
add comment

You can just put a simple tabular environment, if you want to control the break. For instance, use

\node (mynode) [mystyle,right=of anothernode] {\begin{tabular}{c} This node \\ is \\ valuable \end{tabular}};

-
Simple. Efficient. Now someone tell me why this is a bad idea. –  LordStryker Apr 4 at 13:16
add comment