# tikz matrix alignment of empty cells [duplicate]

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}[
]
\matrix (m) [nodes in empty cells,
matrix of nodes,
execute at begin cell=\strut,
execute at empty cell=\strut,
nodes = {
red,
draw,
align=right,
minimum height=1cm,
minimum width=2cm,
inner sep=0pt,
},
]
{
A  & B    & y  \\
1  &      &    \\
2  &      &    \\
3  &      &    \\
};

\end{tikzpicture}

\end{document}


which gives:

Why the empty cells are misaligned? How can I solve this? (The \strut is necessary to avoid the same problem with descendant in "filled" cells, but why it is not working in the empty ones?)

I don't think it is the content so much as the anchoring of the cells which is problematic. That is, you could fix it by fiddling with the content, no doubt, but setting the anchor for each cell is probably the most straightforward solution.

Try using anchor=center:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}[
]
\matrix (m) [nodes in empty cells,
matrix of nodes,
nodes = {
red,
draw,
align=right,
minimum height=1cm,
anchor=center,
minimum width=2cm,
inner sep=0pt,
},
]
{
A  & B    & y  \\
1  &      &    \\
2  &      &    \\
3  &      &    \\
};

\end{tikzpicture}

\end{document}