Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by Adam Liter, Jubobs, Svend Tveskæg, Jesse, Thorsten Mar 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.

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

3 Answers 3

up vote 22 down vote accepted

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.


alt text

share|improve this answer
    
+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
1  
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

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}
share|improve this answer
1  
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

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}};
share|improve this answer
    
Simple. Efficient. Now someone tell me why this is a bad idea. –  LordStryker Apr 4 at 13:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.