# Tikz Matrix: Nodes are not 'in-a-row'? [duplicate]

I am using a matrix for node-positioning with a fixed node witdh. However, as some nodes contaning more than one line of text some nodes are shifted in y-axis and I don't know why or how to fix this. There seems to be enough space inside the node in y-axis. I also connected some nodes which result in an ugly picture.

MWE which shows this behaviour:

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,fit}
\begin{document}
\begin{tikzpicture}
\matrix (m) [%
matrix of nodes,%
nodes in empty cells,%
nodes={rectangle, draw ,align=center,minimum height=1.5cm, text width=3.5cm,font=\scriptsize, inner sep= 0cm, outer sep= 0cm, fill=white},%
column sep=.5cm,%
row sep=.5cm,%
anchor=center
]%
{
&{Two\\line\\breaks}&{One line}&& \\
{One\\linebreak}&&&& \\
&&&{Two\\line\\break}& \\
&&One&& \\
};
\foreach \row/\rownext in {1/2,2/3,3/4,4/4}
{
\foreach \col/\colnext in {1/2,2/3,3/4,4/5,5/5}
{
\draw(m-\row-\col) to (m-\row-\colnext);
\draw(m-\row-\col) to (m-\rownext-\col);
}
}
\end{tikzpicture}
\end{document}


Picture or result:

## marked as duplicate by Stefan Pinnow, Bobyandbob, siracusa, TeXnician, Zarko tikz-pgf StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Sep 19 '18 at 21:15

You only need to add anchor=center to the nodes. You only added this option to the matrix (node), not to the nodes within the matrix. NOTE: This question has already been asked here. At the time of writing my answer I didn't know that.

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,fit}
\begin{document}
\begin{tikzpicture}
\matrix (m) [%
matrix of nodes,%
nodes in empty cells,%
nodes={rectangle, draw ,align=center,minimum height=1.5cm, text
width=3.5cm,font=\scriptsize, inner sep= 0cm, outer sep= 0cm,
fill=white,anchor=center},%
column sep=.5cm,%
row sep=.5cm,%
anchor=center
]%
{
& {Two\\ line \\breaks} & {One line} && \\
{One\\linebreak}&&&& \\
&&&{Two\\line\\breaks}& \\
&&One&& \\
};
\foreach \row/\rownext in {1/2,2/3,3/4,4/4}
{
\foreach \col/\colnext in {1/2,2/3,3/4,4/5,5/5}
{
\draw(m-\row-\col) to (m-\row-\colnext);
\draw(m-\row-\col) to (m-\rownext-\col);
}
}
\end{tikzpicture}
\end{document}


• The problem here is different from the one you pointed. The use of matrix of nodes changes node's anchor to base which means than empty cells and nodes with text will align on their base anchor, not through the center. Loop Space explained to me also long time ago ;-). – Ignasi Sep 19 '18 at 16:01
• @Ignasi Thanks! I didn't know that. (My immediate thought when seeing this was that anchor=center is on the wrong place. So I wrote a quick answer. Then I was looking for someone to give credit for this afterwards, and apparently messed it up. ;-) – user121799 Sep 19 '18 at 16:13