Table Row Color Covers Text
The white space comes from booktabs
adding some vertical padding above and below table rules. A workaround consists in setting this padding to $0$ pt and replacing it with vertical spacing with the cellspace
package: it defines a minimal such vertical spacing and is compatible with \rowcolors
(the column specifier must be preceded by the letter S
).
As for the issue with @{}
, I replace it with adding negative horizontal spacing on leaving cells in the second column and on entering the third, with >{}
and <{}
.
\documentclass[preview]{article}
\usepackage{mathtools}
\usepackage{tabularx, booktabs, caption, array}
\usepackage{colortbl}
\usepackage[table]{xcolor}
\usepackage{cellspace}
\setlength\cellspacetoplimit{6pt}
\setlength\cellspacebottomlimit{6pt}
\begin{document}
\begin{table}
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\rowcolors{2}{gray!15}{white}% <{\hskip-\arraycolsep}>{\hskip-\arraycolsep{}}
$ \begin{array}{Slr <{{}\hskip-\arraycolsep}>{\hskip-\arraycolsep\mkern-.5mu}lcc}
&& & \text{Col 1} & \text{Col 2} \\
\toprule
\text{Row 1} & (100 \times{} & 100) & 1 & 2 \\
\text{Row 2} & (100 \times{} &1000) & 3 & 4 \\
\bottomrule
\end{array} $
\end{table}
\end{document}
For the first question, you can remove the \tabcolsep
separately for those two columns like
r<{\hspace{-\tabcolsep}}>{\hspace{-\tabcolsep}\,}c
<{\hspace{-\tabcolsep}\,}>{\hspace{-\tabcolsep}}lcc}
and for second, you can define a \bottomrulec
like
\newcommand{\bottomrulec}{%
\arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{0pt}
\arrayrulecolor{black}
}
and use it instead of \bottomrule
. Here are the coloured versions for \toprule
and \midrule
in case if you need them.
\newcommand{\toprulec}{%
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\aboverulesep}{0pt}
\arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
\arrayrulecolor{black}
}
\newcommand{\midrulec}{%
\arrayrulecolor{gray!15}\specialrule{\aboverulesep}{0pt}{0pt}
\arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{\belowrulesep}
}
Your code modified:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\newcommand{\bottomrulec}{% Coloured \toprule
\arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{0pt}
\arrayrulecolor{black}
}
\begin{document}
\begin{table}
\footnotesize
\centering
\rowcolors{2}{gray!15}{white}
\begin{tabular}{lr<{\hspace{-\tabcolsep}}>{\hspace{-\tabcolsep}\,}c
<{\hspace{-\tabcolsep}\,}>{\hspace{-\tabcolsep}}lcc}
&&&& Col 1 & Col 2 \\ \toprule
Row 1 & $(100$ & $\times$ & $100)$ & $1$ & $2$ \\
Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
\bottomrulec
\end{tabular}
\end{table}
\end{document}