TikZ Matrix Alignment Issue
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
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}