In order to achieve the effect, you have to do the following
- reset length names that control space above and below rules in
booktabs
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
- manually add missing spacing using
\rule[]{}{}
.
EDIT. With tabularray
, the code can be much simplified (see the second code)
Note, in this example, I applied booktabs
environment instead of regular tblr
(both from tabularray
). The former keep standard spacing around rows and maintains additional spacing around rules (similarly to booktabs
in regular tabular
). On the other hand, tblr
applies its own layout: rows have slightly larger height as well as rules defined in booktabs
do not apply extra spacing.
The MWE without tabularray
\documentclass{book}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage[table]{colortbl}
\usepackage{array}
\newcommand{\mc}[2]{\multicolumn{#1}{c}{#2}}
\definecolor{Gray}{gray}{0.85}
\definecolor{LightCyan}{rgb}{0.88,1,1}
\newcolumntype{B}{>{\columncolor{Gray}}c} % don't use existing names
\begin{document}
\begin{table}[tbh]
\begin{tabular}{B | B | B | B | B}
\specialrule{\heavyrulewidth}{0pt}{0pt}
\rowcolor{LightCyan}
\mc{1}{} & \mc{1}{x} & \mc{1}{y} & \mc{1}{w} & \mc{1}{z} \\
\rowcolor{LightCyan}
\mc{1}{e} & \mc{1}{d} & \mc{1}{c} & \mc{1}{b} & \mc{1}{a} \\
\cmidrule{1-2}
variable 1 & a & b & c & d \\
variable 1 & a & b & c & d \\
\midrule
variable 1 & a & b & c & d \\
variable 1 & a & b & c & d \\
\hline
variable 2 & a & b & c & d \\ \bottomrule
\end{tabular}
\end{table}
\begin{table}[tbh]
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\newcommand\astrut{\rule[8pt]{0pt}{2pt}}
\newcommand\bstrut{\rule[-5pt]{0pt}{5pt}}
\begin{tabular}{B *4{|B}}
\specialrule{\heavyrulewidth}{0pt}{0pt}
\rowcolor{LightCyan}
\mc{1}{} & \mc{1}{x} & \mc{1}{y} & \mc{1}{w} & \mc{1}{z} \\
\rowcolor{LightCyan}
\mc{1}{e} & \mc{1}{d} & \mc{1}{c} & \mc{1}{b} & \mc{1}{a\bstrut} \\
\cmidrule{1-2}\astrut
variable 1 & a & b & c & d \\
variable 1 & a & b & c & d \bstrut \\
\midrule\astrut
variable 1 & a & b & c & d \\
variable 1 & a & b & c & d \bstrut \\
\hline \astrut
variable 2 & a & b & c & d \bstrut\\ \bottomrule
\end{tabular}
\end{table}
\end{document}
The MWE with tabularray
\documentclass{book}
% \usepackage{amsmath,amsfonts,amssymb}
\usepackage{xcolor}
\definecolor{Gray}{gray}{0.85}
\definecolor{LightCyan}{rgb}{0.88,1,1}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\begin{table}[tbh]
\begin{booktabs}{
colspec = {*5{Q[c]}},
row{1-2} = {bg=LightCyan},
row{3-Z} = {bg=Gray},
hline{3} = {3-5}{Gray},
rows = {abovesep+=-0pt, belowsep+=-0pt},
}
\toprule
& x & y & w & z \\
e & d & c & b & a \\
\cmidrule{1-2}
variable 1 & a & b & c & d \\
variable 1 & a & b & c & d \\
\midrule
variable 1 & a & b & c & d \\
variable 1 & a & b & c & d \\
\midrule
variable 2 & a & b & c & d \\
\bottomrule
\end{booktabs}
\end{table}
\end{document}