How to insert a linebreak in a TikZ matrix node?
Just like in any node, you need to specify the alignment in order to break the line.
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [
matrix of nodes,
nodes={align=left, text width=3cm} % New!
] {
{some text} &
{text with \\ linebreak} \\
{other text} &
{more text} \\
};
\end{tikzpicture}
\end{document}
For some reason, for a node in a matrix, you also need to specify the width of the node text.
Any value larger than the actual width should work, but when finalizing your document, you should try to get the value as close as possible to the actual width of the text, so that the node anchors stand where they are expected to.
--- Edit ---
If one wants to avoid the text width
specification, it is possible to draw the node manually:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [
matrix of nodes%,
%nodes={align=left, text width=3cm} % Not needed now
] {
{some text} &
\node[align=left] {text with \\ linebreak}; \\
{other text} &
{more text} \\
};
\end{tikzpicture}
\end{document}
Here's an alternative which wraps the contents of each of the nodes within the matrix in the variable-width \pbox
defined by the pbox
package using the execute at begin cell
option. You could selectively apply the \pbox
to the offending material, but this approach allows the syntax of the question to be used. The \linewidth
is the maximum width for the node. As the nodes are sized individually, the default left-alignment of the \pbox
looks strange in my opinion, hence the regexpatch
to center the contents.
Edit: As pointed out by @cfr in the comments, \usetikzlibrary{chains}
from the MWE in the original question is not necessary.
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{pbox}
\usepackage{regexpatch}
\makeatletter
\xpatchcmd{\pb@xiii}{{\parbox[\pb@xargi]{\pb@xlen}{#2}}}{{\parbox[\pb@xargi]{\pb@xlen}{\centering#2}}}{}{fail}%
\makeatother
\begin{document}
\begin{tikzpicture}
\matrix (m) [
matrix of nodes,
execute at begin cell = {\pbox[t]{\linewidth}},
] {
{some text to test} &
{really long text with\\ linebreak}\\
{other text} &
{more text} \\
};
\end{tikzpicture}
\end{document}
I do not know why. But this works.
\makeatletter
\def\tikz@lib@matrix@smuggle{%
\bgroup%
\def\\{\egroup\tikz@lib@matrix@saved@eol\bgroup}%
}
\tikz{
\matrix(m)[matrix of nodes,nodes={anchor=center,align=center}]{
some text & {this node \\ now works}& \\
other text & more text \\
};
}
P.S. the original definition is in /tikzlibrarymatrix.code.tex
line 60:
\def\tikz@lib@matrix@smuggle{% \bgroup% \let\\=\tikz@lib@matrix@saved@eol% }
This suggests that the TikZ stopped because it reached something like
\node{{this node \\ does not work}};
And the new definition leads to
\node{{this node }\\{ now works}};
P.S.2. This is not widely tested.