How to insert a linebreak in a TikZ matrix node?

How can I get a linebreak inside a matrix node? The following gives the error:

Package tikz Error: Giving up on this path. Did you forget a semicolon?.

unless I remove the linebreak.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [
matrix of nodes
] {
{some text} &
{this node \\ does not work} \\
{other text} &
{more text} \\
};
\end{tikzpicture}
\end{document}

• @alfred-m I just tested it (using Sharelatex online service, which uses pdflatex and a recent tex distribution), and the answer by T.Verron works without problem. – JLDiaz Nov 18 '15 at 11:42
• @JLDiaz: I can also compile the example (I updated pgf in the mean time). However, it requires the specification of the text width. I would like to break lines only using explicit line breaks and let the dimensions of the nodes be automatically fixed otherwise. – Alfred M. Nov 18 '15 at 14:47
• @AlfredM. If you wanted to get my attention on this problem, posting a comment to the answer would have been safer. ;) As far as I know, there is no easy way to get rid of the text width option. If I remember correctly, the reason for this option is that a node with a line break is implemented as a minipage, and this requires setting the width explicitely. However, if you aren't interested in doing that in a matrix cell, you can use two nodes aligned in a matrix to emulate the linebreak behavior. – T. Verron Nov 18 '15 at 15:16
• or nodes={align=center} – percusse Nov 19 '15 at 0:32
• @percusse I guess then the anchors (m-3-5) break, would they? – Symbol 1 Nov 19 '15 at 1:00

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}

• I still get ! Package tikz Error: Giving up on this path. Did you forget a semicolon?. when compiling your example, without any modification. What version of tikz or other is required for this to work? – Georges Dupéron Sep 16 '13 at 11:55
• Hmf. It doesn't work here either. I'm pretty sure I tested it back then, and I think the OP wouldn't have accepted it if it didn't work, but I also don't think I updated any tikz package since then. So no idea what's happening. Anyway, I'm adding a fix to the answer. Thanks for the notification! – T. Verron Sep 16 '13 at 14:38

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.