120

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?

Timofey
  • 1,947

3 Answers3

96

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

  • 2
    +1 I like the idea: constraint text area with "text width" option and that is how the sentence is split. Thanks! – Timofey 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
81

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}
Matten
  • 5,687
  • 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? – Timofey 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 09:52
  • 9
    Let me note --since I just got bitten by this-- that if you leave out the "align", you don't get the line breaking. (This is counter-intuitive to me but is what I saw.) – Hugh Thomas Jun 29 '18 at 21:44
74

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}};
Eric
  • 1,545