12

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

enter image description here

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

enter image description here

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}
orome
  • 10,459

1 Answers1

13

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}

enter image description here

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
  • @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 http://tex.stackexchange.com/questions/4239/which-measurement-units-should-one-use-in-latex ? 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
  • @PolGab Haha, I do exactly the same. – percusse Jun 26 '12 at 23:47
  • @percusse: another justification to prefer em instead of ex was given by Will himself: "I usually consider 1em to be about the same size as the font size in points." ;-) – Paul Gaborit Jun 27 '12 at 00:01
  • 1
    Perhaps better is \matrix[every node/.style =... instead of \begin{tikzpicture}[every node/.style =.... – Alain Matthes Jun 27 '12 at 07:53
  • Can't be this done somehow automatically, i.e. not guessing the values and instead use minimal needed for the particular content? – user87690 Jun 30 '15 at 15:30
  • @user87690 Which particular content? All matrix nodes? IMHO, appropriate values depend on the semantic of node's content. – Paul Gaborit Jun 30 '15 at 16:20
  • @PaulGaborit: It could be possible to specify the set of nodes. It would be nice to be able to say "make baselines and heights, possibly widths of these nodes the same" without explicitely specifying the sizes. – user87690 Jun 30 '15 at 16:52
  • @user87690 Ok. Ask a new question... – Paul Gaborit Jun 30 '15 at 21:09