考虑以下 MWE
\documentclass{article}
\usepackage{xcolor}
\usepackage{array,booktabs,nicematrix}
\definecolor{yesgreen}{HTML}{AFFA6A}
\definecolor{nored}{HTML}{E04B14}
\def \cagewidthW {0.2pt}
\newcommand{\clrCell}[1]{\Block[fill=#1,draw=black,line-width=\cagewidthW]{1-1}{}}
\begin{document}
{
\renewcommand{\arraystretch}{1.6}
\setlength\extrarowheight{-0.8pt}
\begin{table}
\centering
\begin{NiceTabular}{c@{\hskip 2em}c@{\hskip 1.8em}ccc}
\toprule
& & Case a) & Case b) & Case c) \\
\toprule
\Block{2-1}{$U-s$} & Present & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{nored} \\
& Future & \clrCell{yesgreen} & \clrCell{nored} & \clrCell{nored} \\
\Block{2-1}{$U-Y$} & Present & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{yesgreen} \\
& Future & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{yesgreen} \\
\Block{2-1}{$Y-s$} & Present & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{yesgreen} \\
& Future & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{yesgreen} \\ \bottomrule
\end{NiceTabular}
\end{table}
}
\end{document}
然而,手动调整会导致两个问题:
(我)前导指令@{\hskip <length>}
用于增加第一列与第二列、第二列与第三列之间的距离,这导致(我相信)第一个彩色列比其他两个彩色列窄很多。
(二)\toprule
限制表格内容的和booktabs命令\bottomrule
导致(我相信)第一个和最后一个彩色行比中间的行高很多。
我该如何修复这个问题(即,使每个彩色单元格大小相同)同时仍保持良好的对齐(单词垂直居中)?请注意,我需要在文档中的其他表格上使用 booktabs 包。
答案1
您的表格代码非常符合您的要求。因此,任何改进都是值得怀疑的。您只能通过使用nicematrix
包(我不太熟悉)或使用新的基于 LaTeX-3 的tabularray
包来简化代码。对于后者,代码可以是:
\documentclass[12pt]{article}
\usepackage{xcolor}
\definecolor{yesgreen}{HTML}{AFFA6A}
\definecolor{nored}{HTML}{E04B14}
\usepackage{tabularray}
\begin{document}
\begin{table}[htb]
\centering
\begin{tblr}{
colspec = {Q[l,wd=4em] Q[l,wd=5em] *{3}{Q[c,wd=3.4em]}},
column{3-5}={bg=yesgreen},
row{1} = {bg=white},
hline{1,2,Z} = 1pt, hline{3-Y} = {3-Z}{solid},
vline{3-Z}=solid,
stretch=1.5
}
& & Case a) & Case b) & Case c) \\
\SetCell[r=2]{c} $U-s$
& Present & & & \SetCell{nored} \\
& Future & & \SetCell{nored} & \SetCell{nored} \\
\SetCell[r=2]{c} $U-Y$
& Present & & & \\
& Future & & & \\
\SetCell[r=2]{c} $Y-s$
& Present & & & \\
& Future & & & \\
\end{tblr}
\end{table}
\end{document}
答案2
我对您的代码做了些许修改,但显示信息显示booktabs
不正常。通过使用(更宽)\hline
,单元格的高度相同。
\documentclass{article}
\usepackage[table]{xcolor}
\definecolor{yesgreen}{HTML}{AFFA6A}
\definecolor{nored}{HTML}{E04B14}
\usepackage{booktabs,nicematrix}
\usepackage{tikz}% added <<<<
\newlength{\cagewidthW}
\setlength{\cagewidthW}{0.2pt}
\newcommand{\clrCell}[1]{\Block[tikz={fill=#1, draw=black, line width=\cagewidthW}]{1-1}{}}
\begin{document}
\textbf{\large MWE}
{
\renewcommand{\arraystretch}{1.6}
\setlength\extrarowheight{-0.8pt}
\begin{table}[h!]
\centering
\begin{NiceTabular}{c>{\hspace*{2em}}c<{\hspace*{2em}}ccc}
\toprule
& & Case a) & Case b) & Case c) \\
\toprule
\Block{2-1}{$U-s$} & Present & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{nored} \\
& Future & \clrCell{yesgreen} & \clrCell{nored} & \clrCell{nored} \\
\Block{2-1}{$U-Y$} & Present & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{yesgreen} \\
& Future & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{yesgreen} \\
\Block{2-1}{$Y-s$} & Present & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{yesgreen} \\
& Future & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{yesgreen} \\ \bottomrule
\end{NiceTabular}
\end{table}
}
\textbf{\large Modified}
\newcolumntype{C}[1]{>{\raggedright\arraybackslash}m{#1}}
\begin{table}[h!]
\centering
\begin{NiceTabular}{c>{\hspace*{1.5em}}C{7em}ccc}[rules/color=black,rules/width=0.7pt, cell-space-limits=3pt]
\hline
& & Case a) & Case b) & Case c) \\
\hline
\Block{2-1}{$U-s$} & Present & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{nored} \\
& Future & \clrCell{yesgreen} & \clrCell{nored} & \clrCell{nored} \\
\Block{2-1}{$U-Y$} & Present & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{yesgreen} \\
& Future & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{yesgreen} \\
\Block{2-1}{$Y-s$} & Present & \clrCell{yesgreen} & \clrCell{yesgreen} & \clrCell{yesgreen} \\
& Future & \clrCell{yesgreen} & \clrCell{nored} & \clrCell{yesgreen} \\
\hline
\end{NiceTabular}
\end{table}
\end{document}