Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

1 Answer

up vote 9 down vote accepted

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

share|improve this answer
    
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
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.