# Tables with math, equal column width, and right alignment

Q: Is there an approach, less hacky than my \MyLen approach, that would generate roughly the second table below? I'd like to maintain professional features (but I welcome criticisms wholeheartedly!) like (1) booktabs effects, (2) right alignment of data, (3) no vertical rules, (4) horizontal rule only to separate estimators and column titles, (5) equal column widths, (6) and tight spacing of columns.

I generated the following table and thought it didn't look terribly professional because of the different column widths (Q: Am I wrong? Should this table be left alone?)

\documentclass{article}
\usepackage{booktabs}
\usepackage{diagbox}
\usepackage{tabularx}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{@{}lrrrrrrrr@{}}
\toprule
& \multicolumn{4}{l}{MPE} & \multicolumn{4}{l}{MAPE}\\
\cmidrule(r){1-1}  \cmidrule(lr){2-5} \cmidrule(l){6-9}
\diagbox[trim=lr]{Est.}{Jumps} & 0 & 3 & 10 & 30 & 0 & 3 & 10 & 30 \\
\midrule
$\widetilde{JV}$                & 2.14 & 2.42    & 1.35     & $-$1.69  & 2.14 & 4.57 & 4.92  & 4.74 \\
$\widehat{JV}$                  & 1.99 & 2.16    & 1.12     & $-$1.83  & 1.99 & 4.47 & 4.91  & 4.81 \\
$\widehat{JV}_{\mathrm{BNS04}}$ & 0.35 & $-$4.35 & $-$12.91 & $-$26.35 & 3.11 & 5.48 & 12.95 & 26.35 \\
\midrule
$\widetilde{IV}$                & $-$2.03 & $-$2.52 & $-$1.33 & 1.73  & 6.54 & 6.37 & 5.24  & 3.71 \\
$\widehat{IV}$                  & $-$1.87 & $-$2.26 & $-$1.10 & 1.87  & 6.48 & 6.31 & 5.23  & 3.80 \\
$\widehat{IV}_{\mathrm{BNS04}}$ & $-$0.24 & 4.25    & 12.93   & 26.39 & 6.61 & 7.06 & 13.11 & 26.39 \\
\bottomrule
\end{tabular}
\caption{my caption}
\end{table}


\newlength{\MyLen}
\settowidth{\MyLen}{$-$26.35}
\newcolumntype{R}{>{\raggedleft\arraybackslash}p{\the\MyLen}}
\begin{table}[ht]
\centering
\begin{tabular}{@{}lRRRRRRRR@{}}
\toprule
& \multicolumn{4}{l}{MPE} & \multicolumn{4}{l}{MAPE}\\
\cmidrule(r){1-1}  \cmidrule(lr){2-5} \cmidrule(l){6-9}
\diagbox[trim=lr]{Est.}{Jumps} & 0 & 3 & 10 & 30 & 0 & 3 & 10 & 30 \\
\midrule
$\widetilde{JV}$                & 2.14 & 2.42    & 1.35     & $-$1.69  & 2.14 & 4.57 & 4.92  & 4.74 \\
$\widehat{JV}$                  & 1.99 & 2.16    & 1.12     & $-$1.83  & 1.99 & 4.47 & 4.91  & 4.81 \\
$\widehat{JV}_{\mathrm{BNS04}}$ & 0.35 & $-$4.35 & $-$12.91 & $-$26.35 & 3.11 & 5.48 & 12.95 & 26.35 \\
\midrule
$\widetilde{IV}$                & $-$2.03 & $-$2.52 & $-$1.33 & 1.73  & 6.54 & 6.37 & 5.24  & 3.71 \\
$\widehat{IV}$                  & $-$1.87 & $-$2.26 & $-$1.10 & 1.87  & 6.48 & 6.31 & 5.23  & 3.80 \\
$\widehat{IV}_{\mathrm{BNS04}}$ & $-$0.24 & 4.25    & 12.93   & 26.39 & 6.61 & 7.06 & 13.11 & 26.39 \\
\bottomrule
\end{tabular}
\caption{my caption}
\end{table}


I like this table. More professional (But again, any comments? On second look, the columns do look a little wide). Although, I don't like having to use the \MyLen hack. I thought there might be a less hacky approach using the tabularx package:

\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
\begin{table}[ht]
\centering
\begin{tabularx}{\linewidth}{@{}lRRRRRRRR@{}}
\toprule
& \multicolumn{4}{l}{MPE} & \multicolumn{4}{l}{MAPE}\\
\cmidrule(r){1-1}  \cmidrule(lr){2-5} \cmidrule(l){6-9}
\diagbox[trim=lr]{Est.}{Jumps} & 0 & 3 & 10 & 30 & 0 & 3 & 10 & 30 \\
\midrule
$\widetilde{JV}$                & 2.14 & 2.42    & 1.35     & $-$1.69  & 2.14 & 4.57 & 4.92  & 4.74 \\
$\widehat{JV}$                  & 1.99 & 2.16    & 1.12     & $-$1.83  & 1.99 & 4.47 & 4.91  & 4.81 \\
$\widehat{JV}_{\mathrm{BNS04}}$ & 0.35 & $-$4.35 & $-$12.91 & $-$26.35 & 3.11 & 5.48 & 12.95 & 26.35 \\
\midrule
$\widetilde{IV}$                & $-$2.03 & $-$2.52 & $-$1.33 & 1.73  & 6.54 & 6.37 & 5.24  & 3.71 \\
$\widehat{IV}$                  & $-$1.87 & $-$2.26 & $-$1.10 & 1.87  & 6.48 & 6.31 & 5.23  & 3.80 \\
$\widehat{IV}_{\mathrm{BNS04}}$ & $-$0.24 & 4.25    & 12.93   & 26.39 & 6.61 & 7.06 & 13.11 & 26.39 \\
\bottomrule
\end{tabularx}
\caption{my caption}
\end{table}


I like that the columns are tighter but there is some weird stuff going on with that 3rd data row (Q: Any clue what's going on there?)

-
I wouldn't say your length approach is a hack. The alternative (defining some environment that automatically identifies the largest item in all the columns) is doable, but would have to do a lot of leg-work in the background. You'd have to read in the entire table and parse the column entries, and then (re)typeset the entire table. For example, the kbordermatrix does this (I think) to maintain a balanced look. Foregoing this, \MyLen is the easy (and convenient) way out if you know your table entries. –  Werner Jan 21 '12 at 21:42

I really dislike how you are doing the minus signs. Since all your data is two digits a simpler solution in this case for the last table would be to use

\newcolumntype{R}{>{\begin{math}}r<{\end{math}}}


and eliminate the $-$ hack. Also, you could consider centering the titles: MPE and MAPE.

Alternatively, you could use the S column type from the siunitx package to align on the decimal point. This also ends up shifting the "Jumps" row so if that is not desired you could manually adjust that row:

## Note:

• I am not sure why, but using the S column type necessitated replacing the \midrule and \bottomrule with \cmidrule{1-9}.

## Code: Modified R Column Type:

\documentclass{article}

\usepackage{booktabs}
\usepackage{diagbox}
\usepackage{tabularx}

\begin{document}
\newcolumntype{R}{>{\begin{math}}r<{\end{math}}}
\begin{table}[ht]
\centering
\begin{tabularx}{\linewidth}{@{}lRRRRRRRR@{}}
\toprule
& \multicolumn{4}{l}{MPE} & \multicolumn{4}{l}{MAPE}\\
\cmidrule(r){1-1}  \cmidrule(lr){2-5} \cmidrule(l){6-9}
\diagbox[trim=lr]{Est.}{Jumps} & 0 & 3 & 10 & 30 & 0 & 3 & 10 & 30 \\
\midrule
$\widetilde{JV}$                &  2.14 &  2.42 &   1.35 & -1.69  & 2.14 & 4.57 & 4.92  & 4.74 \\
$\widehat{JV}$                  &  1.99 &  2.16 &   1.12 & -1.83  & 1.99 & 4.47 & 4.91  & 4.81 \\
$\widehat{JV}_{\mathrm{BNS04}}$ &  0.35 & -4.35 & -12.91 & -26.35 & 3.11 & 5.48 & 12.95 & 26.35 \\
\midrule
$\widetilde{IV}$                & -2.03 & -2.52 &  -1.33 &   1.73 & 6.54 & 6.37 & 5.24  & 3.71 \\
$\widehat{IV}$                  & -1.87 & -2.26 &  -1.10 &   1.87 & 6.48 & 6.31 & 5.23  & 3.80 \\
$\widehat{IV}_{\mathrm{BNS04}}$ & -0.24 &  4.25 &  12.93 &  26.39 & 6.61 & 7.06 & 13.11 & 26.39 \\
\bottomrule
\end{tabularx}
\caption{my caption}
\end{table}
\end{document}


## Code: siunitx version:

\documentclass{article}

\usepackage{booktabs}
\usepackage{diagbox}
\usepackage{tabularx}
\usepackage{siunitx}

\begin{document}
\begin{table}[ht]
\centering
\begin{tabularx}{\linewidth}{@{}lSSSSSSSS@{}}
%\begin{tabularx}{\linewidth}{@{}lrrrrrrrr@{}}
\toprule
& \multicolumn{4}{l}{MPE} & \multicolumn{4}{l}{MAPE}\\
\cmidrule(r){1-1}  \cmidrule(lr){2-5} \cmidrule(l){6-9}
\diagbox[trim=lr]{Est.}{Jumps} & 0 & 3 & 10 & 30 & 0 & 3 & 10 & 30 \\
\cmidrule{1-9}
$\widetilde{JV}$                &  2.14 &  2.42 &   1.35 & -1.69  & 2.14 & 4.57 & 4.92  & 4.74 \\
$\widehat{JV}$                  &  1.99 &  2.16 &   1.12 & -1.83  & 1.99 & 4.47 & 4.91  & 4.81 \\
$\widehat{JV}_{\mathrm{BNS04}}$ &  0.35 & -4.35 & -12.91 & -26.35 & 3.11 & 5.48 & 12.95 & 26.35 \\
\cmidrule{1-9}
$\widetilde{IV}$                & -2.03 & -2.52 &  -1.33 &   1.73 & 6.54 & 6.37 & 5.24  & 3.71 \\
$\widehat{IV}$                  & -1.87 & -2.26 &  -1.10 &   1.87 & 6.48 & 6.31 & 5.23  & 3.80 \\
$\widehat{IV}_{\mathrm{BNS04}}$ & -0.24 &  4.25 &  12.93 &  26.39 & 6.61 & 7.06 & 13.11 & 26.39 \\
\cmidrule{1-9}
\end{tabularx}
\caption{my caption}
\end{table}
\end{document}

-
Yeah, I used the xtable package in R to automatically output this table. It gives the funny $-$ hack, which is nice in a way because I can just use the regular r columns rather than specifying a \newcolumntype. But I think I'm going to use your approach now. Also, your solution is a good one to replace the first table where the column widths are not uniform. But it doesn't really work as a replacement for the 3rd table since it doesn't use equal column widths or the X columntype from tabularx –  lowndrul Jan 21 '12 at 22:04
Have updated to include an alternate solution using the siunitx package that provides the S column type to align on the decimal point. –  Peter Grill Jan 22 '12 at 1:12