使用 toprule 的表格背景颜色

使用 toprule 的表格背景颜色

我想制作一个带有背景颜色的表格,当我使用 \toprule、\midrule 和 \bottomrule 时,规则后面有一个空白。我想在空白下方填充规则的颜色。MWE 在下面

\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}

答案1

为了达到效果,你必须做以下事情

  • 重置控制规则上方和下方空间的长度名称booktabs
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
  • 使用 手动添加缺失的间距\rule[]{}{}

在此处输入图片描述


编辑。tabularray,代码可以简化很多(见第二个代码)

注意,在此示例中,我应用了booktabs环境而不是常规tblr(均来自tabularray)。前者保持行周围的标准间距,并保持规则周围的额外间距(类似于booktabs常规tabular)。另一方面,tblr应用其自己的布局:行的高度略大,并且定义的规则booktabs不应用额外的间距。

在此处输入图片描述

没有 MWEtabularray

\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}

MWE 与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}

答案2

与。{NiceTabular}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}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

无论您使用哪种 PDF 查看器,都不会在彩色面板中看到细白线。

相关内容