# TikZ Matrix Alignment Issue

The following MWE

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

\begin{document}
\begin{tikzpicture}
\matrix (A) [matrix of nodes, column sep=-\pgflinewidth, row sep=-\pgflinewidth, nodes={minimum height=3em, draw, anchor=center}] {
foo & fooy\\
};
\end{tikzpicture}
\end{document}


produces the following result: I would like to align the cell boxes properly. The only solution I know for this is to add anchor=center to the node style, i.e.

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

\begin{document}
\begin{tikzpicture}
\matrix (A) [matrix of nodes, column sep=-\pgflinewidth, row sep=-\pgflinewidth, nodes={minimum height=3em, draw, anchor=center}] {
foo & fooy\\
};
\end{tikzpicture}
\end{document}


However, this results in misaligned text (red guideline is for visualization purposes and is not part of the MWE):

How can I align both the text and the cell boxes in the intended way?

• Add \strut before or after foo: foo\strut. – Ignasi Dec 15 '17 at 11:58

According to the TikZ manual, in the "Tutorial: Diagrams as Simple Graphs", the key is to directly fix the height and depth of the nodes. That way, you force them to be aligned. The key words are text height and text depth

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

\begin{document}
\begin{tikzpicture}
\matrix (A)%
[matrix of nodes,
column sep=-\pgflinewidth,
row sep=-\pgflinewidth,
nodes=
{minimum height=3em,
draw,
text height=1.5ex,
text depth=.25ex,
anchor=center}
] {
foo & fooy\\
};

\draw[red, very thin] (A-1-1.base) -- +(1.5,0);
\draw[blue, very thin] (A-1-2.base) -- +(-1.5,0);
\end{tikzpicture}
\end{document}


On the left side of the picture, you can see the result when those keys are not specified, I place them side by side and draw the lines on the base for comparison

Just add key text depth=.25ex inside nodes options

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

\begin{document}

\begin{tikzpicture}
\matrix (A) [%
matrix of nodes,
column sep=-\pgflinewidth,
row sep=-\pgflinewidth,
nodes={minimum height=3em, draw, text depth=.25ex, anchor=center}
]
{
foo & fooy\\
};
\end{tikzpicture}

\end{document}


UPDATE

If you need the matrix cells squared, you can use the code below

\begin{tikzpicture}

\tikzset{square matrix/.style={
matrix of nodes,
column sep=-\pgflinewidth,
row sep=-\pgflinewidth,
nodes={draw,
minimum height=3em,
anchor=center,
text width=3em,
text depth=.25ex,
align=center,
inner sep=0pt
},
},
square matrix/.default=1cm
}

\matrix[square matrix]
{
foo & fooy\\
};
\end{tikzpicture}


If you're ever dealing with text that may have varying height/depth, you can use \strut to ensure similar depth/height. Here's an example with \fbox:

\documentclass{article}

\begin{document}

\fbox{foo}\ \fbox{fooy}

\fbox{\strut foo}\ \fbox{\strut fooy}

\end{document}


In a similar vein, issue \strut with the components you want to have similar height/depth (no guessing required):

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix (A)
[matrix of nodes,
column sep = -\pgflinewidth,
row sep = -\pgflinewidth,
nodes = {
minimum height=3em,
draw,
anchor = center
}] {
\strut foo & \strut fooy \\
};
\end{tikzpicture}

\end{document}