tabularx 中帶有 \aboverulesep 和 \{} 的行顏色間隙

tabularx 中帶有 \aboverulesep 和 \{} 的行顏色間隙

桌子顏色

\documentclass[]{article}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{multicol,booktabs,tabularx}

% Table settings
\renewcommand{\aboverulesep}{1pt}
\renewcommand{\belowrulesep}{1pt}

\begin{document}
    \begin{tabularx}{\textwidth}{@{}X l@{}}
        This is Header 1 & This is Header 2 \\
        \toprule
        \rowcolor{Apricot}
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\
        \bottomrule
    \end{tabularx}
\end{document}

我有上面的表 MWE 使用tabularx,並且我正在嘗試為表中的行著色。但是,由於使用了 my\aboverulesep\belowrulesep,現在我的表格行上方和下方有未著色的間隙。

另外,由於在表格的兩側使用 my@{}來「刪除」表格兩側多餘的填充,因此行顏色也會套用到這些填充中。

問題 1:如何\aboverulesep在保持規則分離的同時對上述間隙(由於 引起)進行著色?

問題 2:如何去除桌子兩側的顏色?


編輯

這是對扎科的回答的回應。我想保持在任一側使用@{}來移除填充物。如果我要把它改編成你的答案,就像這樣:

% @Zarko's answer
\documentclass{article}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{booktabs, cellspace, tabularx}

% Table settings
\renewcommand{\aboverulesep}{0pt}
\renewcommand{\belowrulesep}{0pt}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}

\begin{document}
    \begin{tabularx}{\textwidth}{@{}SX Sl@{}}% <-- S is append for activate additional vertical space 
        This is Header 1 & This is Header 2 \\
        \toprule
        \rowcolor{Apricot}
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\
        \bottomrule
    \end{tabularx}
\end{document}

我得到這個:在此輸入影像描述

所以我的第二個問題(Qn2\renewcommand{\arraystretch}{1.15})仍未解決。

答案1

一種可能性是使用cellspace包:

在此輸入影像描述

首先,規則周圍的垂直空間booktabs我減少到零點,然後通過宏增加到\cellspacetoplimit所需\cellspacebottomlimit的垂直(彩色)間隙:

\documentclass{article}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{booktabs, cellspace, tabularx}

% Table settings
\renewcommand{\aboverulesep}{0pt}
\renewcommand{\belowrulesep}{0pt}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}

\begin{document}
   \begin{tabularx}{\textwidth}{SX Sl}% <-- S is append for activate additional vertical space 
        This is Header 1 & This is Header 2 \\
        \toprule
    \rowcolor{Apricot}
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\
        \bottomrule
    \end{tabularx}
\end{document}

附錄: 解決問題的第二部分。它需要引入假列(或列之間的距離為零或寬度為 2 的空白\tabcolsep):

在此輸入影像描述

這次沒有額外的包,但有使用技巧\rowcolor(有關詳細信息,請參閱包的文檔colortbl):

\documentclass{article}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{booktabs, tabularx}

% Table settings
\renewcommand{\aboverulesep}{0pt}
\renewcommand{\belowrulesep}{0pt}

\begin{document}
\setlength\tabcolsep{0pt}
\renewcommand\arraystretch{1.2}
   \begin{tabularx}{\textwidth}{ X c<{\hspace{12pt}} l }
        This is Header 1 && This is Header 2 \\
        \toprule
    \rowcolor{Apricot}%[0pt]
        This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is && This is Text 2 \\\midrule
        This is Text 1 && This is Text 2 \\\midrule
        This is Text 1 && This is Text 2 \\
        \bottomrule
    \end{tabularx}
\end{document}

附錄(2): 四年後......現在我將使用新的表包tabularray。將其用於上述附錄中的 MWE 程式碼更簡單:

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
\noindent
    \begin{tblr}{colspec = {@{} X[1,l]  l @{}},
                 column{1} = {rightsep=12pt},
                 row{2} = {bg=Apricot},
                 }
This is Header 1 &  This is Header 2    \\
    \toprule
This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1
                & This is Text 2        \\
    \midrule 
This is Text 1  & This is Text 2        \\
    \midrule
This is Text 1  & This is Text 2        \\
    \bottomrule
    \end{tblr}
\end{document}

在此輸入影像描述

答案2

OP的解決方案

這是 @Zarko 針對我的第二個問題的解決方案的替代解決方案,即如何在使用 時刪除表格兩側的虛假顏色,同時仍然在環境中\rowcolor{}保留 , 的使用。這具有額外的好處(與 Zarko 的答案相比),不需要在中間添加單獨的列,並且必須在所有行中添加額外的對齊點。@{}tabularx

在下面的答案中,我還結合了@Skillmon\renewcommand\arraystretch{1.2}關於我的第一個問題的使用建議。

這是代碼:

\documentclass{article}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{booktabs, tabularx}

% Table settings
\renewcommand{\aboverulesep}{0pt}
\renewcommand{\belowrulesep}{0pt}

\begin{document}
    \renewcommand\arraystretch{1.15}
    \begin{tabularx}{\textwidth}{@{}>{\columncolor{white}[0pt][\tabcolsep]}X >{\columncolor{white}[\tabcolsep][0pt]}l @{}}
        This is Header 1 & This is Header 2 \\
        \toprule
        \rowcolor{Apricot}
        This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1  & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\
        \bottomrule
    \end{tabularx}
\end{document}

桌子



程式碼解釋

主要方法是透過套件\columncolor中的命令來利用懸垂的使用colortbl,也可以透過xcolor帶有table可選參數的套件來呼叫。

請注意,該\columncolor命令接受可選參數,如下所示:

\columncolor{colour} [left overhang][right overhang]

所以我只是將最左邊一列的左懸垂設為 0pt,從而消除整個列左側的虛假空間,並將右懸垂設置為\tabcolsep基本上是分隔列的距離。我對最右邊一列的右懸垂做了同樣的事情,將其設置為 0pt,同時將其左懸垂設置為\tabcolsep.這本質上是修復列寬,如我所願。

顯然,中兩列的顏色都\columncolor必須設定為white,最後要注意的一件事是,當\rowcolor稍後在表中呼叫時,它將覆蓋的這種white顏色\columncolor,因此仍然為我們帶來了正確設定行顏色的好處。

閱讀colortbl包裝手冊這裡

答案3

使用{NiceTabular}of nicematrix,您可以直接獲得預期的輸出。

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{booktabs, nicematrix}

\begin{document}
    \renewcommand\arraystretch{1.15}
    \begin{NiceTabularX}{\textwidth}{@{}Xl@{}}[colortbl-like]
        This is Header 1 & This is Header 2 \\
        \toprule
        \rowcolor{Apricot}
        This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1  & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\
        \bottomrule
    \end{NiceTabularX}
\end{document}

您需要多次編譯(因為nicematrix在背景使用 PGF/Tikz 節點)。

上述程式碼的輸出

相關內容