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.

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}

share|improve this question
5  
Of course you're writing this to provide a standard answer for this usual requirements. Nice idea! –  Gonzalo Medina Jul 11 '13 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 '13 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 '13 at 5:53
1  
@Jake The "really bad accepted answer" is not bad: it is the first solution gives by pgfmanual! ;-) –  Paul Gaborit Jul 15 '13 at 8:40
1  
Related (questions that do ask this in any way): [1], [2], [3], [4], [5], [6], [7] –  Qrrbrbirlbel Nov 21 '13 at 21:27

1 Answer 1

up vote 77 down vote accepted
+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 (p. 179).

Now, the TikZ-PGF manual explains three ways in which one can achieve line breaking inside of a TikZ node if one desires (pp. 179-182).

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

One can use an environment inside of the node that forces line breaking or creates a line-breaking environment in order to achieve line breaking inside of 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

2. Use \\ and align.

If you want to manually insert line breaks, 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 size of the node 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. 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

3. 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 pp. 181-182 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, preferred option that is not discussed in the TikZ-PGF manual is to make use of 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. 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 option, 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, making use of 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 do 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):

The reason that, in the case of wanting to manually control line breaks, the second option is preferable to the fourth option is because the second option simply 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

share|improve this answer
2  
That's the stuff. Thank you, that's an excellent answer! –  Jake Jul 15 '13 at 16:58
    
@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 '13 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 '13 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 '13 at 17:48
    
@percusse I'm still relatively new to (La)TeX, so I'm still learning my way around things. Your last comment is a bit beyond me, but if you do find anything further to nitpick in the updated answer, let me know. Thanks again! :-) –  Adam Liter Jul 15 '13 at 20:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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