3

This question already has an answer here:

Please consider:

\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:

output of the above snippet

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?)

marked as duplicate by Ignasi tikz-pgf Feb 19 '17 at 17:14

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.

4

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}

centred anchoring

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