# Colored tables with booktabs, makecell and cellspace

While experimenting with different table-packages, I realised, that each package basically stands for itself and often lacks in compatibility with other great packages.

So I tried to create a table in the style of the booktabs-package (only horizontal lines), but with the functionality of the makecell-package (\thead, \makegapedcells, ...) and the possibility to color the rows (which is not very intuitive with booktabs, as discussed here.)

I made a little comparison between different table-configurations:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage[skip=10pt]{parskip}
\usepackage[table]{xcolor}
\usepackage{rotating}
\usepackage{array}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage[column=Q]{cellspace}    % changed to Q for simultaneous use of siunitx

\newcommand{\tableComment}[2]{
\begin{tabular}{m{4.5cm}}
#1\\[5pt]#2
\end{tabular}}

\setcellgapes{1pt}
\renewcommand{\cellrotangle}{90}

\newcommand{\TopRule}{\Xhline{1pt}}
\newcommand{\MidRule}{\Xhline{.5pt}}
\newcommand{\BottomRule}{\Xhline{1pt}}

\begin{document}

{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{ll}\hline
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \hline
\end{tabular}
}
\tableComment{2: Standard Lines, Head with \texttt{makecell}, spacing with \texttt{cellspace}}
{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{QlQl}\hline
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \hline
\end{tabular}
}
\tableComment{3: Lines with \texttt{Xhline}, Head with \texttt{makecell}, spacing with \texttt{cellspace}}
{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{QlQl}\TopRule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \BottomRule
\end{tabular}
}\\

{
\rowcolors{1}{gray!20}{white}
\makegapedcells
\begin{tabular}{ll}\TopRule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \BottomRule
\end{tabular}
}
\tableComment{5: Lines with \texttt{booktabs}, Head with \texttt{makecell}, spacing with \texttt{booktabs}}
{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{ll}\toprule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \bottomrule
\end{tabular}
}
{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{QlQl}\TopRule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \BottomRule
\end{tabular}
}

{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{QlQl}\TopRule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \BottomRule
\end{tabular}
}
{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{ll}\toprule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \bottomrule
\end{tabular}
}
{
\rowcolors{1}{gray!20}{white}
\makegapedcells
\begin{tabular}{ll}\TopRule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \BottomRule
\end{tabular}
}
\end{document}


With the following result:

As you can see, in almost every combination of packages there are some difficulties (or it simply doesn't look good). My favourites are Versions 3 and 6.

But I found no solutions for rotated cells, or by using \makegapedcells

How would you create colored tables in booktabs-style? (And what does the booktabs-package actually do except introducing new horizontal lines and changing the vertical space?). And why does \makegapedcells delete all colors except inside the \thead?

• What about \renewcommand{\theadfont}{\itshape\cellcolor{gray!40}} \tableComment{10: Lines with \texttt{Xhline}, Head with \texttt{rothead}, spacing with \texttt{cellspace}, color with \texttt{cellcolor}} { \rowcolors{3}{gray!20}{white} \begin{tabular}{QlQl}\TopRule %\rowcolor{gray!40} \rothead{My\\Head} & \rothead{My\\second\\ Head} \\ \MidRule some content & $\dfrac{1}{2}$ \\ foo & bar \\ \BottomRule \end{tabular} } as an alternative for colored cells with rotated text? Aug 17 '19 at 12:07

As to vertical spacing in cells, the winner is … (drum roll) … cellspace, provided you use it the correct way – you simply forgot to specify the values of the minimal spacing at the top and bottom of cells.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage[skip=10pt]{parskip}
\usepackage[table]{xcolor}
\usepackage{rotating}
\usepackage{array}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage[column=Q]{cellspace} % changed to Q for simultaneous use of siunitx
\setlength{\cellspacetoplimit}{5pt}
\setlength{\cellspacebottomlimit}{5pt}

\newcommand{\tableComment}[2]{
\begin{tabular}{m{4.5cm}}
#1\\[5pt]#2
\end{tabular}}

\setcellgapes{1pt}
\renewcommand{\cellrotangle}{90}

\newcommand{\TopRule}{\Xhline{1pt}}
\newcommand{\MidRule}{\Xhline{.5pt}}
\newcommand{\BottomRule}{\Xhline{1pt}}

\begin{document}

{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{ll}\hline
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \hline
\end{tabular}
}
\tableComment{2: Standard Lines, Head with \texttt{makecell}, spacing with \texttt{\color{red}cellspace}}
{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{QlQl}\hline
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \hline
\end{tabular}
}
\tableComment{3: Lines with \texttt{Xhline}, Head with \texttt{makecell}, spacing with \texttt{\color{red}cellspace}}
{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{QlQl}\TopRule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \BottomRule
\end{tabular}
}\\

{
\rowcolors{1}{gray!20}{white}
\makegapedcells
\begin{tabular}{ll}\TopRule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \BottomRule
\end{tabular}
}
\tableComment{5: Lines with \texttt{booktabs}, Head with \texttt{makecell}, spacing with \texttt{booktabs}}
{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{ll}\toprule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \bottomrule
\end{tabular}
}
{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{QlQl}\TopRule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \BottomRule
\end{tabular}
}

{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{QlQl}\TopRule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \BottomRule
\end{tabular}
}
{
\rowcolors{1}{gray!20}{white}
\begin{tabular}{ll}\toprule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \bottomrule
\end{tabular}
}
{
\rowcolors{1}{gray!20}{white}
\makegapedcells
\begin{tabular}{ll}\TopRule
\rowcolor{gray!40}
some content & $\dfrac{1}{2}$ \\
foo & bar \\ \BottomRule
\end{tabular}
}

\end{document}


Unfortunately, cellspace may give uneven height of the rows, depending on cell content. With all the packages, you will experience the problem that the rules disappear in browser, because colour overwrites them.

The package cals is actually the only package that I have found that overcomes most of the problems.

Another possibility is to cancel all space between cells in the table (\tabcolsep{0pt} arraystretch > 1 ) and then add spacing between columns with narrow columns and between rows with a combination of heavy, horizontal rules with the same colour as the row and (eventually) a narrow, black arrayrulesep.

You then imitate the old way of typesetting tabulars when the typesetter spaced out columns and rows using struts of lead.

[I plan to give some examples, but I ran into some side effects of a command and need first to ask a question!]

With nicematrix (≥ 5.6) and booktabs, you have directly the following result.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}

\begin{document}

\begin{NiceTabular}{ll}[cell-space-limits=1pt]
\CodeBefore
\rowcolor{gray!40}{1}
\rowcolors{2}{gray!20}{}
\Body
\toprule
some content & $\dfrac{1}{2}$ \\

The commands \toprule, midrule and \bottomrule are provided by booktabs; \Block and \rotate are provided by nicematrix. The keywords \CodeBefore and \Body and the commands within (\rowcolor and \rowcolors) are provided by nicematrix. The package colortbl is not loaded. It could have been loaded with the same result.