284

How can I insert a line break in a TikZ node? Simply putting \\ where I want the break doesn't work (see MWE).

Is there a way to make lines break automatically at some specified width?

And can I control the alignment of the text (left, right, centered, justified)?

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node {First line\\second line};
\end{tikzpicture}
\end{document}

10
  • 9
    Of course you're writing this to provide a standard answer for this usual requirements. Nice idea! Jul 11, 2013 at 23:07
  • @Qrrbrbirlbel: Oh, and the first question you linked to (tex.stackexchange.com/q/50902) has a really bad accepted answer. I know that the votes show this, but I still see potential for confusion there.
    – Jake
    Jul 12, 2013 at 5:23
  • I have a very rough and short template in tex.stackexchange.com/a/63469/3235 in case anyone wants to write a canonical answer.
    – percusse
    Jul 12, 2013 at 5:53
  • 1
    @Jake The "really bad accepted answer" is not bad: it is the first solution gives by pgfmanual! ;-) Jul 15, 2013 at 8:40
  • 2
    Related (questions that do ask this in any way): [1], [2], [3], [4], [5], [6], [7] Nov 21, 2013 at 21:27

2 Answers 2

399
+500

The problem, according to the TikZ-PGF manual is that

Normally, when a node is typeset, all the text you give in the braces is put in one long line (in an \hbox, to be precise), and the node will become as wide as necessary (§17.4.3).

Now, the TikZ-PGF manual explains three ways to achieve line breaking inside of a TikZ node if one desires.

  1. Use a multi-line environment inside of the node.

One can use an environment inside the node that forces line breaking or creates a line-breaking environment to achieve line breaking inside the node. The example in the manual uses the tabular environment:

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\node [draw] (example-tabular) {
\begin{tabular}{cc}
eaxmple1 & example2 \\
example 3 & example4 \\
\end{tabular}
};
\end{tikzpicture}

\end{document}

enter image description here

  1. Use \\ and align.

If you want to insert line breaks manually, you can use \\ and the optional argument align. (If you do not specify an option for align, the line breaking will not happen, and the problem noted by the OP will occur.)

\begin{tikzpicture}
\node (example-align) [draw, align=left]{example \\ example example};
\end{tikzpicture}

enter image description here

The advantage to this option is that the node's size is automatically set to the width of the longest line inside the node, as can be seen in the accompanying image, where the width of the node is set to the width of the second line. However, the disadvantage to this solution is that you have to manually control the line breaking yourself (more on this below).

It is also worth noting that you can control the spacing of the lines via an optional argument of the \\ command:

\begin{tikzpicture}
\node (example-align) [draw, align=left]{example \\[5em] example example};
\end{tikzpicture}

enter image description here

Note, however, that the \\ cannot be nested inside of a group. So, for example, the following will not work.

\begin{tikzpicture}
\node (example-align) [draw, align=left]{\textbf{example \\ example example}};
\end{tikzpicture}

Instead, you would need to do:

\begin{tikzpicture}
\node (example-align) [draw, align=left]{\textbf{example}\\\textbf{example example}};
\end{tikzpicture}
  1. Use text width and \\ (and maybe align, too).

Finally, the third option noted in the TikZ-PGF manual is to use the text width argument, which, I believe, internally creates a minipage environment. This solution manually sets the width of the node, and it can then be used in conjunction with manual line breaking:

\begin{tikzpicture}
\node (example-textwidth-1) [draw, text width=3cm]{example \\ example};
\end{tikzpicture}

enter image description here

Additionally, it can be used with a lengthier block of text whose default width is greater than the width specified via text width. In such cases, the text will automatically wrap inside a box of the specified width:

\begin{tikzpicture}
\node (example-textwidth-2) [draw, text width=3cm]{This is a demonstration text for showing how line breaking works.};
\end{tikzpicture}

enter image description here

The text width argument can also be used in conjunction with the align argument to produce different effects. The options for align are left, flush left, right, flush right, center, flush center, justify, and none. See §17.4.3 for the details of the different effects of these align options in conjunction with the text width argument.

In brief, however, the flush variants do not try to balance left and right borders via hyphenation. In my opinion, the result often does not look good (see picture), but it can be used if, for whatever reason, you do wish to avoid hyphenation.

enter image description here

(The top node in the picture immediately above uses align=left, and the bottom node uses align=flush left.)

A fourth option

A fourth, and I think the preferred option not discussed in the TikZ-PGF manual, is to use the varwidth package. This package essentially creates a minipage environment but automatically sets the horizontal size of the environment to the widest thing inside of it. Above, you will notice that the text width option often made the node bigger than necessary. For example, in the picture reproduced immediately below, you can see that there is extra space at the right margin:

enter image description here

However, if we use the varwidth package, this extra space is eliminated, even though both are set to 3cm:

\documentclass{article}

\usepackage{tikz}
\usepackage{varwidth}

\begin{document}

\begin{tikzpicture}
\node (example-textwidth-3) [draw, text width=3cm, align=left]{This is a demonstration text for showing how line breaking works.};
\end{tikzpicture}

\begin{tikzpicture}
\node (example-varwidth) [draw, align=left] {\begin{varwidth}{3cm}This is a demonstration text for showing how line breaking works.\end{varwidth}};
\end{tikzpicture}

\end{document}

enter image description here

Discussion

Overall, I think the fourth option is the most preferred, as it makes the node as compact as possible (unless, of course, you do not want the node to be compact).

Nonetheless, there are at least two cons to the fourth option that I can think of that you may want to consider before determining how you wish to implement multi-line text inside of a TikZ node, though I think the first con to this option has a workaround.

The first con is the following: as @percusse pointed out in the comments, the varwidth package effectively does the same thing as what align does to the node, which presumably causes varwidth to effectively moot any visible differences between the possible options for align. So, if you wish to have a right-aligned margin in your node and you specify it via align=right, there will be no visible difference between that and, say, align=left. This can be overcome, though, by making use of the ragged2e package. If you do want your node to have a right-aligned margin yet you also want it to be as compact as possible (and do not want to take the time to manually manipulate the setting of text width via a 'guess and check' method):

\documentclass{article}

\usepackage{tikz}
\usepackage{varwidth}
\usepackage{ragged2e}

\begin{document}

\begin{tikzpicture}
\node (example-varwidth-left) [draw, align=left]{\begin{varwidth}{3cm}This is a demonstration text for showing how line breaking works.\end{varwidth}};
\end{tikzpicture}

\begin{tikzpicture}
\node (example-varwidth-right) [draw, align=right]{\begin{varwidth}{3cm}This is a demonstration text for showing how line breaking works.\end{varwidth}};
\end{tikzpicture}

\begin{tikzpicture}
\node (example-varwidth-ragged) [draw, align=flush right] {\begin{varwidth}{3cm}\RaggedLeft This is a demonstration text for showing how line breaking works.\end{varwidth}};
\end{tikzpicture}

\end{document} 

enter image description here

In the image, you can see that there is no difference between the first two nodes, but using the commands made available by ragged2e does produce a visible effect in the third node.

The second con to the fourth option arises if you really do wish to control the line breaking manually. The varwidth environment still attempts to balance lines via hyphenation, which can lead to, in my opinion, ugly results:

\begin{tikzpicture}
\node (example-varwidth-linebreak) [draw, align=left]{\begin{varwidth}{3cm}This is a demonstration \\ text for showing how line breaking works.\end{varwidth}};
\end{tikzpicture}

enter image description here

If you wish to manually control the line breaking, I would suggest the second option, specifying the align argument and inserting \\'s as you please.

Update (as per @percusse's comment):

In the case of wanting to manually control line breaks, the second option is preferable to the fourth option because the second option sets the node width to the length of the longest line inside of the node. No text wrapping and no hyphenation is enforced. As a result, there will be no, in my opinion, 'ugly' output from the second option precisely because no text wrapping and hyphenation is enforced.

Moreover, this is why I have included the fourth option in addition to the three noted in the TikZ-PGF manual. If you do wish to enforce text wrapping inside of the node with a certain width as well as have the node's size as compact as possible, then you will want to use the varwidth environment. Compare the following two nodes, one with align and one with varwidth set to 3cm:

\begin{tikzpicture}
\node (example) [draw, align=left]{This is a demonstration text for showing how line breaking works.};
\end{tikzpicture}

\begin{tikzpicture}
\node (example) [draw]{\begin{varwidth}{3cm}This is a demonstration text for showing how line breaking works.\end{varwidth}};
\end{tikzpicture}

enter image description here


Addendum

Strictly speaking, this addendum is outside the scope of the question; however, given the intended nature of this question as indicated in the comments on the question, I thought that I would add that you can put any of the list environments (itemize, enumerate, and description) inside of a node by embedding it inside either a minipage or varwidth environment. Again, I think the varwidth environment is preferable for reasons enumerated (no pun intended!) above.

\documentclass{article}

\usepackage{tikz}
\usepackage{varwidth}
\usepackage{enumitem}

\begin{document}

\begin{tikzpicture}
\node [draw] (example-list) {
\begin{varwidth}{3cm}
\begin{enumerate}[leftmargin=*]
\item{First item}
\item{Second item}
\end{enumerate}
\end{varwidth}
};
\end{tikzpicture}

\end{document}

enter image description here

8
  • @percusse Ah, that would explain why the effects of align are mooted in conjunction with the varwidth environment. A question, though: how does TikZ determine the width of the node when you only specify align. varwidth gives the user the option of setting at least an approximate width, and then any extra space is effectively chopped off. That (along with my apparent ignorance) is why I added it as a fourth option---namely, since it gives the user the ability to at least roughly specify the width of the node for herself.
    – Adam Liter
    Jul 15, 2013 at 17:03
  • @percusse Okay, so it seems align only sets the width of the node to the minimal width, whereas varwidth does this and enforces text wrapping and hyphenation. I've updated my answer a bit to reflect this, though if you have any clarifications or more nitpicks, I'd appreciate more helpful comments. Thanks! :-)
    – Adam Liter
    Jul 15, 2013 at 17:44
  • @Adam Ah, sorry. I coudn't check it yet. But as far as I remember it was all about controlling something like a \pgfutil@minipage environment when it is not a \....@line@box (!?). I need to read the code before I say something stupid.
    – percusse
    Jul 15, 2013 at 17:48
  • 1
    @Viesturs In the simplest case, I think you're right. But there's no reason you couldn't create multiple minipage or varwidth environments inside of a node, each with their own text alignment.
    – Adam Liter
    Aug 19, 2017 at 15:23
  • 1
    Now that the manual has changed a lot and the page number changes, you could quote the section number/name instead. "17.4.3 Text Parameters: Alignment and Width for Multi-Line Text" _____________________________________________________________________________________________ Also note that in option 2, the \\ must not be in any group like in a tabular environment.
    – user202729
    Oct 16, 2021 at 8:04
2

If you are just looking to break down the line at your points of reference, it suffices to break down the node itself to multiple lines.

For instance, from TikZ manual section 3.12, the code draft below

\begin{tikzpicture}
  \draw (0,0) -- (3,0) 
  node [above,align=center,midway]
  {
    First line \\
    Second Line
  };
\end{tikzpicture}

breaks the line with no further packages/tools required.

NOTE: this method did not work for me when the node is alone; just when it is part of a line. No extra packages are needed though.

The script output:

Output of the TikZ script with no extra packages

2
  • Don't know why this don't work for me.
    – Ying
    Mar 23, 2019 at 0:32
  • it only works when align is provided. Apr 10, 2021 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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