我正在尝试构建一个表格,其中某些列正常显示,而其他列则显示为灰色。我booktabs
在整个文档中使用规则,需要此表格的格式与其他表格一致。
我可以轻松将相关列的内容变灰。但是,我在保持垂直对齐的同时将规则变灰时遇到了麻烦。我可以使用\cmidrule
特定宽度(例如\heavyrulewidth
或lightrulewidth
来模拟toprule
/boittomrule
或midrule
),但使用\arrayrulecolor{}
会改变垂直间距。
我怎样才能理解并解决这个问题?
最初,我以为 Werner 的回答https://tex.stackexchange.com/a/32366/掌握了关键。但是,我并不真正理解发生了什么,也无法成功地适应它。我尝试用绘制一个常规规则,\specialrule
然后用灰色在顶部绘制一个部分规则,但这并不比我的其他尝试更好。
以下是我目前实验的结果。左边的表格应该看起来和右边的一样,只是第二列应该变灰。不用说,它不应该看起来像雅各布的梯子。
代码:
\documentclass{article}
\usepackage{booktabs,array}
\usepackage[table]{xcolor}
\begin{document}
\begin{tabular}{c>{\color{gray}\arraybackslash}c}
% \toprule
\cmidrule[\heavyrulewidth]{1-1}%
\arrayrulecolor{gray}%
\cmidrule[\heavyrulewidth]{2-2}%
\arrayrulecolor{black}%
Black Header & Grey Header \\
% \midrule
\cmidrule[\lightrulewidth]{1-1}%
\arrayrulecolor{gray}%
\cmidrule[\lightrulewidth]{2-2}%
\arrayrulecolor{black}%
Black content & Grey content \\
\specialrule{\lightrulewidth}{0pt}{-\lightrulewidth}% ateb Werner: https://tex.stackexchange.com/a/32366/
\arrayrulecolor{gray}%
\cmidrule[\lightrulewidth]{2-2}%
\arrayrulecolor{black}%
Black content & Grey content \\
Black content & Grey content \\
\cmidrule(lr){1-1}
Black content & Grey content \\
\arrayrulecolor{gray}%
\cmidrule(lr){2-2}
\arrayrulecolor{black}%
Black content & Grey content \\
\cmidrule(lr){1-1}%
\arrayrulecolor{gray}%
\cmidrule(lr){2-2}
\arrayrulecolor{black}%
Black content & Grey content \\
% \bottomrule
\cmidrule[\heavyrulewidth]{1-1}%
\arrayrulecolor{gray}%
\cmidrule[\heavyrulewidth]{2-2}%
\arrayrulecolor{black}%
\end{tabular}
\begin{tabular}{cc}
\toprule
Black Header & Grey Header \\
\midrule
Black content & Grey content \\
\cmidrule[\lightrulewidth]{1-2}%
Black content & Grey content \\
Black content & Grey content \\
\cmidrule(lr){1-1}
Black content & Grey content \\
\cmidrule(lr){2-2}
Black content & Grey content \\
\cmidrule(lr){1-1}%
\cmidrule(lr){2-2}
Black content & Grey content \\
\bottomrule
\end{tabular}
\end{document}
注:出于各种原因,如果我必须重新编码其他表格,将会引起相当大的麻烦。特别是,这些表格的排版版本需要保留完全相同的在 PDF 中,以避免被错误地标记为已更改。我将使用latexdiff
来标记更改,因此如果它们的编码也保持不变,那就最简单了,但如果有必要,我可以手动更正。
编辑
需要澄清的是,问题不仅仅在于规则的宽度,还在于表格中规则上方和下方插入的间距booktabs
。
我可以这样做:
\begin{tabular}{c}
\toprule
Black Header \\
\midrule
Black content \\
\cmidrule[\lightrulewidth]{1-1}
Black content \\
Black content \\
\cmidrule(lr){1-1}
Black content \\
\arrayrulecolor{white}\cmidrule(lr){1-1}
Black content \\
\arrayrulecolor{black}
\cmidrule(lr){1-1}
Black content \\
\bottomrule
\end{tabular}%
{\color{gray}%
\begin{tabular}{c}
\toprule
Grey Header \\
\midrule
Grey content \\
\cmidrule[\lightrulewidth]{1-1}
Grey content \\
Grey content \\
\arrayrulecolor{white}\cmidrule(lr){1-1}
Grey content \\
\arrayrulecolor{gray}
\cmidrule(lr){1-1}
Grey content \\
\cmidrule(lr){1-1}
Grey content \\
\bottomrule
\end{tabular}}
\begin{tabular}{c>{\color{gray}\arraybackslash}c}
\toprule
Black Header & Grey Header \\
\midrule
Black content & Grey content \\
\cmidrule[\lightrulewidth]{1-2}%
Black content & Grey content \\
Black content & Grey content \\
\cmidrule(lr){1-1}
Black content & Grey content \\
\cmidrule(lr){2-2}
Black content & Grey content \\
\cmidrule(lr){1-1}%
\cmidrule(lr){2-2}
Black content & Grey content \\
\bottomrule
\end{tabular}
无论如何,这给出了正确的输出 - 或者几乎正确的输出。但是,它破坏了表的结构,不易维护或调整,并且极易出错。所以,如果可以的话,我宁愿不这样做。
我一直在尝试破译和的代码colortbl.sty
,booktabs.sty
以便弄清楚究竟是什么增加了空间,以及我可以做些什么(如果有的话)。然而,我感觉自己就像被一卷巨大的透明胶带包裹着,在季节性礼品包装的压力下,胶带已经变得疯狂。
答案1
您可以做这样的事情(使用红色而不是灰色以便于调试)
\documentclass{article}
\usepackage{booktabs,array}
\usepackage[table]{xcolor}
\begin{document}
\makeatletter
\def\CT@arc@{\ifnum\@cmidla=1 \color{red}\fi}%
\begin{tabular}{c>{\color{red}\arraybackslash}c}
% \toprule
\cmidrule[\heavyrulewidth]{1-1}%
\cmidrule[\heavyrulewidth]{2-2}%
Black Header & Grey Header \\
% \midrule
\cmidrule[\lightrulewidth]{1-1}%
\cmidrule[\lightrulewidth]{2-2}%
Black content & Grey content \\
\cmidrule[\lightrulewidth]{1-1}%
\cmidrule[\lightrulewidth]{2-2}%
Black content & Grey content \\
Black content & Grey content \\
\cmidrule(lr){1-1}
Black content & Grey content \\
\cmidrule(lr){2-2}
Black content & Grey content \\
\cmidrule(lr){1-1}%
\cmidrule(lr){2-2}
Black content & Grey content \\
% \bottomrule
\cmidrule[\heavyrulewidth]{1-1}%
\cmidrule[\heavyrulewidth]{2-2}%
\end{tabular}
\def\CT@arc@{}%
\begin{tabular}{cc}
\toprule
Black Header & Grey Header \\
\midrule
Black content & Grey content \\
\cmidrule[\lightrulewidth]{1-2}%
Black content & Grey content \\
Black content & Grey content \\
\cmidrule(lr){1-1}
Black content & Grey content \\
\cmidrule(lr){2-2}
Black content & Grey content \\
\cmidrule(lr){1-1}%
\cmidrule(lr){2-2}
Black content & Grey content \\
\bottomrule
\end{tabular}
\end{document}
答案2
这里有一个解决方案\hhline
,我们可以创建一个用黑色和灰色两种颜色\Toprule
模拟的\toprule
\def\Toprule{\noalign{\global\arrayrulewidth=\heavyrulewidth}\hhline{>\arrayrulecolor{black}}%
->{\arrayrulecolor{gray}}-}\noalign{\global\arrayrulewidth=\lightrulewidth}}
在这里,我们用 改变规则的粗细,\noalign{\global\arrayrulewidth=\heavyrulewidth}
然后用 恢复规则的粗细\noalign{\global\arrayrulewidth=\lightrulewidth}
。
\Midrule
并模拟命令 \midrule
\def\Midrule{\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{gray}}-}}
完整代码
\documentclass{article}
\usepackage{booktabs,array,hhline,etoolbox}
\usepackage[table]{xcolor}
\def\Toprule{\noalign{\global\arrayrulewidth=\heavyrulewidth}\hhline{>{\arrayrulecolor{black}}%
->{\arrayrulecolor{gray}}-}\noalign{\global\arrayrulewidth=\lightrulewidth}}
\def\Midrule{\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{gray}}-}}
\let\Bottomrule\Toprule
\begin{document}
\begin{tabular}{c>{\color{gray}\arraybackslash}c}
\Toprule
Black Header & Grey Header \\
\Midrule
Black content & Grey content \\
\Midrule
Black content & Grey content \\
Black content & Grey content \\
\arrayrulecolor{black}
\cmidrule(lr){1-1}
Black content & Grey content \\
\arrayrulecolor{gray}
\cmidrule(lr){2-2}
Black content & Grey content \\
\arrayrulecolor{black}
\cmidrule(lr){1-1}%
\arrayrulecolor{gray}
\cmidrule(lr){2-2}
Black content & Grey content \\
\Bottomrule
\end{tabular}
\end{document}
答案3
tblr
环境的替代解决方案tabularray
软件包版本2021M
:
\documentclass{article}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\begin{tblr}{
colspec = {cc},
column{2} = {fg=gray},
hline{1,Z} = {\heavyrulewidth},
hline{2,3} = {\lightrulewidth},
hline{5} = {1}{\cmidrulewidth},
hline{6} = {2}{\cmidrulewidth},
hline{7} = {\cmidrulewidth},
hline{1-3,6-8} = {1}{2}{gray},
}
Black Header & Grey Header \\
Black content & Grey content \\
Black content & Grey content \\
Black content & Grey content \\
Black content & Grey content \\
Black content & Grey content \\
Black content & Grey content \\
\end{tblr}
\end{document}
使用下一版本2021N
的 tabularray 包,结果会更好:
\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\begin{tblr}{
colspec = {cc},
column{2} = {fg=gray},
hline{1,Z} = {\heavyrulewidth},
hline{2,3} = {\lightrulewidth},
hline{5} = {1}{\cmidrulewidth, lr},
hline{6} = {2}{\cmidrulewidth, lr},
hline{7} = {\cmidrulewidth, lr},
hline{1-3,6-8} = {1}{2}{gray},
}
Black Header & Grey Header \\
Black content & Grey content \\
Black content & Grey content \\
Black content & Grey content \\
Black content & Grey content \\
Black content & Grey content \\
Black content & Grey content \\
\end{tblr}
\end{document}
答案4
如果您接受使用透明度,您可以在想要灰色的列上添加一个部分透明的白色矩形。
使用{NiceTabular}
,nicematrix
可以轻松使用 创建的 PGF/Tikz 节点添加这样的矩形nicematrix
。
\documentclass{article}
\usepackage{nicematrix,booktabs,tikz}
\begin{document}
\begin{NiceTabular}{cccc}
\toprule
Blak Header & Grey Header & Black Header & Grey Header \\
Blak content & Grey content & Black content & Grey content \\
\midrule
Blak content & Grey content & Black content & Grey content \\
\midrule
Blak content & Grey content & Black content & Grey content \\
\cmidrule(rl){2-2}
Blak content & Grey content & Black content & Grey content \\
Blak content & Grey content & Black content & Grey content \\
Blak content & Grey content & Black content & Grey content \\
\bottomrule
\CodeAfter
\tikz \fill [opacity=0.7,white]
([yshift=1mm]1-|2) rectangle ([yshift=-1mm]last-|3)
([yshift=1mm]1-|4) rectangle ([xshift=1mm,yshift=-1mm]last-|5) ;
\end{NiceTabular}
\end{document}