Manual/automatic line breaks and text alignment in TikZ nodes
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. 224).
Now, the TikZ-PGF manual explains three ways in which one can achieve line breaking inside of a TikZ node if one desires (pp. 224-227).
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}
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}
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}
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}
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}
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. 225-227 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.
(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:
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}
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}
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}
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}
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}