# How can I anchor TikZ matrix cell contents to 'baseline' without shifting cell location?

The cells in my tables often contain expressions with superscripts. This results in a text baseline that appears to "wander" between adjacent cells. For example

\begin{tikzpicture}[every node/.style={minimum size=7mm}]
\matrix{
\node[fill=gray!10]{$a$};  &  \node[fill=gray!20]{$a^2$};  &  \node[fill=gray!30]{$b$}; &  \node[fill=gray!40]{$b^2$}; \\};
\end{tikzpicture}


produces

I understand that the way to align the text of all cells to a common baseline is to use anchor=base, but this has the side effect of jumbling the cells. For example

\begin{tikzpicture}[every node/.style={anchor=base,minimum size=7mm}]
\matrix{
\node[fill=gray!10]{$a$};  &  \node[fill=gray!20]{$a^2$};  &  \node[fill=gray!30]{$b$}; &  \node[fill=gray!40]{$b^2$}; \\};
\end{tikzpicture}


produces

Is there a way to align all text to a common baseline, without shifting the cells as well?

\documentclass[]{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[every node/.style={minimum size=7mm}]
\matrix{
\node[fill=gray!10]{$a$};  &  \node[fill=gray!20]{$a^2$};  &  \node[fill=gray!30]{$b$}; &  \node[fill=gray!40]{$b^2$}; \\};
\end{tikzpicture}

\begin{tikzpicture}[every node/.style={anchor=base,minimum size=7mm}]
\matrix{
\node[fill=gray!10]{$a$};  &  \node[fill=gray!20]{$a^2$};  &  \node[fill=gray!30]{$b$}; &  \node[fill=gray!40]{$b^2$}; \\};
\end{tikzpicture}

\end{document}

-

You may fix height and depth of text:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[every node/.style={anchor=base,
text height=.8em,text depth=.2em,minimum size=7mm}]
\matrix{
\node[fill=gray!10]{$a$};&
\node[fill=gray!20]{$a^2$};&
\node[fill=gray!30]{$b$}; &
\node[fill=gray!40]{$b^2$};\\
};
\end{tikzpicture}
\end{document}


-
Perfect! Thanks! –  raxacoricofallapatorius Jun 26 '12 at 22:36
@raxacoricofallapatorius Fixing depth of text is necessary for letters like 'g' or 'y'... –  Paul Gaborit Jun 26 '12 at 22:40
What do you say about Will's answer in tex.stackexchange.com/questions/4239/… ? Should we change it to 2ex ? ;) –  percusse Jun 26 '12 at 23:15
@percusse I usually use only em mainly because ratio between ex and em are often the same regardless of the chosen font... –  Paul Gaborit Jun 26 '12 at 23:39
Perhaps better is  \matrix[every node/.style =... instead of \begin{tikzpicture}[every node/.style =.... –  Alain Matthes Jun 27 '12 at 7:53
show 2 more comments