1

I want make a table with background color, when I am using \toprule,\midrule and \bottomrule I have a white space after the rule. I want to fill the color of rule below space. MWE is below

\documentclass{book}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage[table]{colortbl}
\usepackage{array}

\abovetopsep=2pt

\makeatletter

\def\toprule{\noalign{\ifnum0=`}\fi @aboverulesep=\abovetopsep \global@belowrulesep=4pt%%%I should need 4pt in below of midrule \global@thisruleclass=@ne @ifnextchar[{@BTrule}{@BTrule[\heavyrulewidth]}% }

\def\midrule{\noalign{\ifnum0=`}\fi @aboverulesep=\abovetopsep \global@belowrulesep=4pt%%%I should need 4pt in below of midrule \global@thisruleclass=@ne @ifnextchar[{@BTrule}{@BTrule[\lightrulewidth]}}

\makeatother

\newcommand{\mc}[2]{\multicolumn{#1}{c}{#2}} \definecolor{Gray}{gray}{0.85} \definecolor{LightCyan}{rgb}{0.88,1,1}

\newcolumntype{b}{>{\columncolor{Gray}}c}

\begin{document}

\begin{table} \begin{tabular}{b | b | b | b | b} \toprule \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}

\end{document}

Bernard
  • 271,350

2 Answers2

2

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[]{}{}.

enter image description here


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.

enter image description here

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}

F. Pantigny
  • 40,250
Celdor
  • 9,058
0

With {NiceTabular} of nicematrix.

\documentclass{book}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{nicematrix}

\begin{document}

\begin{table} \begin{NiceTabular}{c|c|c|c|c} \toprule \Block[fill=[rgb]{0.88,1,1}]{2-}{} % the \Block avoids the vertical rules & x & y & w & z \ e & d & c & b & a \ \cline{1-2} \RowStyle[nb-rows=,rowcolor=[gray]{0.85}]{} 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{NiceTabular} \end{table}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You won't see thin white lines in the colored panels, whatever PDF viewer you use.

F. Pantigny
  • 40,250