\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
,我正尝试为表格中的行着色。但是,由于使用了我的\aboverulesep
和\belowrulesep
,现在我的表格行上方和下方有未着色的间隙。
@{}
此外,由于在表格两侧使用“我”来“移除”表格两侧多余的填充,因此行颜色也会应用于这些填充。
问题 1:我怎样才能为上述间隙(由于而产生\aboverulesep
)着色,同时保持规则分离?
问题2:如何去除表格两侧的颜色?
编辑
这是对 Zarko 的回答的回应。我想保留@{}
两边的 以删除填充。如果我要将其改编成您的答案,如下所示:
% @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}
我的第二个问题是(问题) 仍然未解决..对于问题 1,我更喜欢使用@Skillmon 的解决方案,\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}
可以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 节点)。