# Slight vertical mis-alignment of cells when using TikZ matrix of nodes

While laying out a bunch of nodes with a TikZ matrix, I noticed slight vertical mis-alignment of nodes. It seems that words with “tall” letters (p, b, l) shift the box vertically up or down. This does not happen if the node contains only a word without those letters. E.g. test will appear shifted when placed beside a node with uiae as text.

Here's a minimum example:

\usetikzlibrary{matrix}
\begin{tikzpicture}

\matrix[
matrix of nodes,
column sep=1ex, row sep=1ex,
nodes={
draw,
minimum height=2em,
text width=30mm,
},
] (matrix-optimization) {
Encodings &
Problems &
Algorithms \\
%
Selection &
uiae &
pppp \\
};
\end{tikzpicture}


The nodes Problems and pppp will appear shifted. How can I fix that? I'd like all nodes to have the same height (text vertically centered) and to be perfectly aligned.

-
Looks like I can get the desired effect with text height=1em, text depth=1ex}. Not sure about the implications though – knittl Jul 20 '12 at 12:20

The problem is what you said, nodes contain letters with different height and depth and they adjust and align according to it. TiKZ manual explains it in tutorial "Diagrams as simple graphs" and "Cell Pictures".

If you want all nodes aligned but without aligning words you can change nodes anchor with anchor=south.

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[
matrix of nodes,
column sep=1ex, row sep=1ex,
nodes={
draw,
minimum height=2em,
text width=30mm,
anchor=south
},
] (matrix-optimization) {
Encodings &
Problems &
Algorithms \\
%
Selection &
uiae &
pppp \\
};
\end{tikzpicture}
\end{document}


But if you want nodes and text aligned you need to fix a certain depth and height to nodes.

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[text height=1.5ex,text depth=.25ex,
matrix of nodes,
column sep=1ex, row sep=1ex,
nodes={
draw,
minimum height=2em,
text width=30mm
},
] (matrix-optimization) {
Encodings &
Problems &
Algorithms \\
%
Selection &
uiae &
pppp \\
};
\end{tikzpicture}
\end{document}


After posting my answer I've found this previous question which suggest some other solutions: Problem with TikZ and vertical alignment of text inside nodes

-