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.

How can I force LaTeX to vertically center math in a table taking into account subscripts, superscripts, etc? It appears from the output below that LaTeX vertically centered the $JV$ and added the tildes, hats, and subscripts as an afterthought. The result is extra space under the $JV$ and little space above the hats and tildes.

mytable

\documentclass[xcolor=table,svgnames]{beamer} 
\usepackage{booktabs}
\usepackage{amsmath}
\begin{document}
%==============================================================================%
  \begin{frame}{}
    \begin{table}
      \begin{center}
        \rowcolors{1}{}{RoyalBlue!20}             % alternate row coloring
        \newlength{\oldtabcolsep}                 % keep track of old \tabcolsep
        \setlength{\oldtabcolsep}{\tabcolsep}     % 6.0pt
        \setlength{\tabcolsep}{0pt}               % so coloring doesn't run off
                                                  % ends of the table
        \renewcommand{\arraystretch}{1.3}         % because math expressions
                                                  % almost run into each other
        \begin{tabular}{l<{\hspace{\oldtabcolsep}}l<{\hspace{\oldtabcolsep}}l}  
          \toprule
          Estimator & ME & MAE \\
          \specialrule{\lightrulewidth}{0pt}{0pt} % so row-coloring aligns
          \(\widehat{JV}_{\text{na\"{\i}ve}}\)          & 4.37e-07 & 4.37e-07 \\
          \(\widetilde{JV}_{\text{na\"{\i}ve}}\)        & 3.88e-07 & 3.88e-07 \\
          \(\widehat{JV}\)                              & 1.33e-06 & 1.33e-06 \\
          \(\widetilde{JV}\)                            & 1.20e-06 & 1.20e-06 \\
          \bottomrule
        \end{tabular}
      \end{center}
    \end{table}
  \end{frame}
 \end{document}
share|improve this question
1  
1: Don't use center-environment inside a floating environment; see Should I use center or centering for figures. 2. The "new" package tabu provides more flexibility to create such tables. 3. If you don't use the package tabu you can use the package cellspace. –  Marco Daniel Nov 5 '11 at 17:19
add comment

2 Answers 2

up vote 2 down vote accepted

You can insert a strut in each row, instead of acting on \arraystretch:

\rowcolors{1}{}{RoyalBlue!20}             % alternate row coloring
\newlength{\oldtabcolsep}                 % keep track of old \tabcolsep
\setlength{\oldtabcolsep}{\tabcolsep}     % 6.0pt
\setlength{\tabcolsep}{0pt}               % so coloring doesn't run off
                                          % ends of the table
\newcommand{\mystrut}{\vrule height 20pt depth 10pt width 0pt }

 \begin{tabular}
   {>{\mystrut}l<{\hspace{\oldtabcolsep}}
    l<{\hspace{\oldtabcolsep}}
    l
   }
\toprule
\multicolumn{1}{l}{Estimator\hspace*{\oldtabcolsep}} & ME & MAE \\
\specialrule{\lightrulewidth}{0pt}{0pt} % so row-coloring aligns
\(\widehat{JV}_{\text{na\"{\i}ve}}\)          & 4.37e-07 & 4.37e-07 \\
\(\widetilde{JV}_{\text{na\"{\i}ve}}\)        & 3.88e-07 & 3.88e-07 \\
\(\widehat{JV}\)                              & 1.33e-06 & 1.33e-06 \\
\(\widetilde{JV}\)                            & 1.20e-06 & 1.20e-06 \\
\bottomrule
\end{tabular}

Since the strut is automatically inserted in the first column, you have to correct it in the header line. Here the dimensions set for the strut are exaggerated; a perhaps satisfying setting may be

\newcommand{\mystrut}{\vrule height 3ex depth 1.5ex width 0pt }
share|improve this answer
    
Is there an advantage to using the strut approach over \arraystretch? Also, I ran this code and found that there's still more space below the $\widehat{JV}$ than below it. I'd like for the mathematics to be vertically centered...**including** the extra subscripts, superscripts, hats, etc. –  lowndrul Nov 10 '11 at 15:12
    
@brianjd I wouldn't include hats and subscripts: the numbers in columns 2 and 3 visually dominate the row. Just add some vertical space so that hats and subscripts don't go too near the the edge: they span different vertical space, but the eye is not led by them. –  egreg Nov 10 '11 at 15:49
    
Agreed. Once extra space is added it's not so much of an issue anymore. I think I made this more difficult than it needed to be just out of curiosity for how LaTeX works. –  lowndrul Nov 10 '11 at 17:08
add comment

Until now you haven't gave any feedback. Now I will switch my comment to an answer.

First of all don't use the environment center inside the floating environment. An explanation is given in the answer: Should I use center or centering for figures

I know inside beamer figure and co. are not floating-environment in the LaTeX meaning.


The package cellspace:

The package cellspace have a simple syntax. It provides only 2 length and a special column prefix S.

The two length are \cellspacebottomlimit and \cellspacetoplimit which can be changed by \setlength. In your example I set both length to 5pt.

\documentclass[xcolor=table,svgnames]{beamer} 
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{cellspace}
\begin{document}
%==============================================================================%
  \begin{frame}{}
    \begin{table}
    \centering
    \cellspacebottomlimit=5pt
    \cellspacetoplimit=5pt
%      \begin{center}
        \rowcolors{1}{}{RoyalBlue!20}             % alternate row coloring
        \newlength{\oldtabcolsep}                 % keep track of old \tabcolsep
        \setlength{\oldtabcolsep}{\tabcolsep}     % 6.0pt
        \setlength{\tabcolsep}{0pt}               % so coloring doesn't run off
                                                  % ends of the table
        \renewcommand{\arraystretch}{1.3}         % because math expressions
                                                  % almost run into each other
        \begin{tabular}{Sl<{\hspace{\oldtabcolsep}}l<{\hspace{\oldtabcolsep}}l}  
          \toprule
          Estimator & ME & MAE \\
          \specialrule{\lightrulewidth}{0pt}{0pt} % so row-coloring aligns
          \(\widehat{JV}_{\text{na\"{\i}ve}}\)          & 4.37e-07 & 4.37e-07 \\
          \(\widetilde{JV}_{\text{na\"{\i}ve}}\)        & 3.88e-07 & 3.88e-07 \\
          \(\widehat{JV}\)                              & 1.33e-06 & 1.33e-06 \\
          \(\widetilde{JV}\)                            & 1.20e-06 & 1.20e-06 \\
          \bottomrule
        \end{tabular}
%      \end{center}
    \end{table}
  \end{frame}
 \end{document}

The result is seen in picture below: enter image description here

Note: The package cellspace is incompatible with siunitx. If you want to use both packages you have to create your own file Mycellspace.sty and copy the contents of cellspace in the new file. Now you have to change

\newcolumntype{S}[1]{>{\bcolumn #1\@nil}#1<{\ecolumn}}

to

\newcolumntype{O}[1]{>{\bcolumn #1\@nil}#1<{\ecolumn}}

You need a unknown column type.


The package tabu:

The package tabu offers more flexibility as shown below. The package provides the lengths \tabulinesep and \extrarowsep to control the space. In the example I set \tabulinesep=1.5mm

\documentclass[xcolor=table,svgnames]{beamer} 
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{tabu}

\begin{document}
%==============================================================================%
  \begin{frame}{}
    \begin{table}
    \centering
    \tabulinesep=1.5mm
        \rowcolors{1}{}{RoyalBlue!20}             % alternate row coloring
        \newlength{\oldtabcolsep}                 % keep track of old \tabcolsep
        \setlength{\oldtabcolsep}{\tabcolsep}     % 6.0pt
        \setlength{\tabcolsep}{0pt}               % so coloring doesn't run off
                                                  % ends of the table
        \renewcommand{\arraystretch}{1.3}         % because math expressions
                                                  % almost run into each other
        \begin{tabu}{l<{\hspace{\oldtabcolsep}}l<{\hspace{\oldtabcolsep}}l}  
          \toprule
          Estimator & ME & MAE \\
          \specialrule{\lightrulewidth}{0pt}{0pt} % so row-coloring aligns
          \(\widehat{JV}_{\text{na\"{\i}ve}}\)          & 4.37e-07 & 4.37e-07 \\
          \(\widetilde{JV}_{\text{na\"{\i}ve}}\)        & 3.88e-07 & 3.88e-07 \\
          \(\widehat{JV}\)                              & 1.33e-06 & 1.33e-06 \\
          \(\widetilde{JV}\)                            & 1.20e-06 & 1.20e-06 \\
          \bottomrule
        \end{tabu}
    \end{table}
  \end{frame}
 \end{document}

The result is similar to cellspace but it works with siunitx:

enter image description here

share|improve this answer
    
You've turned me on to the tabu package. It looks great. In this case, though, is there really any advantage to using tabulinesep over \renewcommand{\arraystretch}? It's still the case that there's less space above the hat in the $JV$ than there is below. I want the vertical spacing to be the same. \renewcommand{\arraystretch} doesn't seem to do it any better/worse (sorry I didn't respond earlier) –  lowndrul Nov 10 '11 at 17:05
add comment

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.