37

I need to introduce line breaks into the label of a node. This is what I've tried so far, but none of it worked:

1:

\node[node, label={[split parts = 4]alpha \nodepart{two} bravo \nodepart{three} charlie nodepart{four} delta }, below of = 5] (8) {$8$};

2:

\node[node, label={alpha\\bravo\\charlie\\delta}] below of = 5] (8) {$8$};

3:

\node[node, label={alpha \newline bravo \newline charlie \newline delta}] below of = 5] (8) {$8$};

All of those approaches are fairly similar. I found solutions on line breaks inside nodes, and on labels on edges, but not in labels of nodes.

In fact, I've tried the suggestions I found on labels of nodes, but it didn't work as well.

polemon
  • 3,555
  • 4
  • 32
  • 42
  • 1
    Could you please compose a minimal compilable example? The code snippets are more confusing than helpful, because they've got quite a few syntax errors and depend on things not commonly defined (what's the node style? Why do you have below of=5 in there?) – Jake Feb 01 '13 at 17:54
  • 1
    \node[align=center]{line1\\line2\\...}; (also align=left and align=right). – Sašo Živanović Feb 01 '13 at 17:55
  • @SašoŽivanović Yep, figured it out with the answer by Jake. – polemon Feb 01 '13 at 18:20

1 Answers1

48

A label is just like any other node, so you have to specify the align of the label before the manual line breaks work, using \node [label={[align=left]<label text\\with line breaks>}] {<node text>}. Alternatively (again, like with any other node), you can specify the text width of the label, that way manual line breaks will work and the text will wrap automatically after the specified text width.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\node [draw, label={[align=left]First\\Second}] {Node};
\end{tikzpicture}
\end{document}
Jake
  • 232,450
  • Thanks! For some reason it only worked when I have the [align=left] setting in it. If I leave that one out, it's not working :S – polemon Feb 01 '13 at 18:19
  • @polemon: Yes, that's what I meant with "you have to specify the align of the label before the manual line breaks work" – Jake Feb 01 '13 at 18:22
  • 1
    @Jake, you could also add that specifying a specific node text width enables the linebreak as well: \node[text width=<x>cm] {Text\\is\\here!};. However, this also does "automatic" line breaks. – nickpapior Feb 01 '13 at 22:39
  • @zeroth: Yup, I'll put it in. We really need a "canonical" question and answer for this, it pops up so many times and there are quite a few with really misleading answers. – Jake Feb 01 '13 at 22:41
  • @Jake, I completely agree. This question has a pretty good title, however, many have rather unintuitive titles (of course it is difficult if you dont know what it is called). But this calls for an "Dublicate the dublicated" which tries to track down all dublicates. ;) – nickpapior Feb 01 '13 at 22:51
  • Is there a way to make the label appear to the left or right of the node? – soandos Apr 08 '13 at 02:12
  • @soandos: Not with the main node text (the text that is given in \node {<text>}) because that's by definition always within the node. You can use the label key, though: \node [label=left:<label text>] {<node text>};. – Jake Apr 08 '13 at 10:04
  • I found that while \node [label=left:1\\2] {T1}; compiles it does not have a line break in it, it just displays 12 (unlike label=left:{\begin{tabular}{l}1\\2\end{tabular}}] {T1};) – soandos Apr 08 '13 at 15:48
  • Yes, that's because in a label, like in nodes in general, line breaks will only work if you provide either an align option (using something like \node [label={[align=center]left:1\\2}] {T1};) or a text width option (\node [label={[text width=1em]left:1\\2}] {T1};). – Jake Apr 08 '13 at 15:55
  • 5
    Along with[remember picture,overlay,shift={(current page.center)}] and label=below:{[align=left]...} I find that "[align=left]" actually appears alongside the rest of the text. – user4417 Jan 22 '16 at 13:21
  • @user4417 https://tex.stackexchange.com/questions/168441/tikzpicture-line-break-in-node-label-to-right – Simon Schiff Apr 27 '22 at 09:57