6

This question already has an answer here:

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:

enter image description here

marked as duplicate by Stefan Pinnow, Bobyandbob, siracusa, TeXnician, Zarko tikz-pgf Sep 19 '18 at 21:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6

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}

enter image description here

  • 1
    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. ;-) – marmot Sep 19 '18 at 16:13

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