113

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?

1
  • I'm still having trouble with the same thing. Even trying the suggestions here.
    – Lucas
    May 26, 2012 at 23:21

3 Answers 3

88

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
    +1 I like the idea: constraint text area with "text width" option and that is how the sentence is split. Thanks!
    – Timofey
    Dec 11, 2010 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, 2011 at 13:33
72

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}
3
  • 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, 2010 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, 2010 at 9:52
  • 7
    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.) Jun 29, 2018 at 21:44
68

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}};
1
  • +1 Because I can still do stuff like \textbf{..} and so on.. Jan 11, 2021 at 5:49

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