假设你有这张表:
我想仅突出显示某些行的部分内容。例如,我想突出显示第四行,但是仅有的对于前六列,突出显示此行。然后对于后六列,我想突出显示最后一行。
我知道您可以使用\cellcolor{}
,但对每一列都这样做似乎很繁琐。有没有更优雅的方法来只为一行中的特定单元格着色?
PS,我知道表格末尾有多余的空间,我必须修复它:)
以下是代码:
\documentclass{article}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{pdflscape}
\usepackage{booktabs}
\usepackage[margin = 1in]{geometry}
\begin{document}
\begin{table}[ht]
\centering
\def\arraystretch{1.5}
\begin{tabularx}{\linewidth}{lccccclccccc} \toprule
& \multicolumn{5}{c}{$K$-means Clustering} & & \multicolumn{5}{c}{Hierarchical Clustering} \\ \cmidrule(r){2-6} \cmidrule(lr){8-12}
& $\lambda$ & Misclass. & Deviance & Sig. Coef. & Sig. Groups & & $\lambda$ & Misclass. & Deviance & Sig. Coef. & Sig. Groups\\ \midrule
$\alpha = 1$ & $0.05873092$ & $11/31$ & $1.730157$ & $1$ & $0$ & $\alpha = 1$ & $0.05873092$ & $11/31$ & $1.741681$ & $1$ & $0$ \\
$\alpha = 0.8$ & $0.0281825$ & $8/31$ & $1.735783$ & $7$ & $1$ & $\alpha = 0.8$ & $0.02822086$ & $11/31$ & $1.732505$ & $108$ & $1$ \\
$\alpha = 0.6$ & $0.02425659$ & $8/31$ & $1.711518$ & $26$ & $2$ & $\alpha = 0.6$ & $0.02380529$ & $11/31$ & $1.727994$ & $226$ & $1$ \\
$\alpha = 0.4$ & $0.021494$ & $7/31$ & $1.706627$ & $29$ & $2$ & $\alpha = 0.4$ & $0.0214516$ & $11/31$ & $1.726991$ & $319$ & $1$ \\
$\alpha = 0.2$ & $0.02001998$ & $7/31$ & $1.70708$ & $32$ & $2$ & $\alpha = 0.2$ & $0.02007049$ & $11/31$ & $1.72622$ & $393$ & $1$ \\
$\alpha = 0$ & $0.01946719$ & $7/31$ & $1.707758$ & $36$ & $2$ & $\alpha = 0$ & $0.01921006$ & $11/31$ & $1.725663$ & $472$ & $1$ \\ \bottomrule
\end{tabularx}
\end{table}
\end{document}
编辑对 MWE 进行了编辑。
答案1
据我所知,该colortbl
包仅提供轻松为整行或整列着色的方法。如果您只想为部分颜色着色,可以使用命令\cellcolor
。如果您不想一遍又一遍地重复相同的颜色定义,可以使用缩写。(如果您以后决定要更改颜色,也可能会派上用场)。还请注意,我已删除了$
原始示例代码中每个数字周围的多余部分。此外,我猜测了包的设置,geometry
因为它们未在您的示例中给出。
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{tabularx}
\usepackage{pdflscape}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\newcommand{\mycc}{\cellcolor{lightgray}}
\begin{document}
\begin{landscape}
\begin{table}[ht]
\centering
\def\arraystretch{1.5}
\begin{tabularx}{\linewidth}{lccccclccccc} \toprule
& \multicolumn{5}{c}{$K$-means Clustering} & & \multicolumn{5}{c}{Hierarchical Clustering} \\ \cmidrule(r){2-6} \cmidrule(lr){8-12}
& $\lambda$ & Misclass. & Deviance & Sig. Coef. & Sig. Groups & & $\lambda$ & Misclass. & Deviance & Sig. Coef. & Sig. Groups\\ \midrule
$\alpha = 1$ & 0.05873092 & 11/31 & 1.730157 & 1 & 0 & $\alpha = 1$ & 0.05873092 & 11/31 & 1.741681 & 1 & 0 \\
$\alpha = 0.8$ & 0.0281825 & 8/31 & 1.735783 & 7 & 1 & $\alpha = 0.8$ & 0.02822086 & 11/31 & 1.732505 & 108 & 1 \\
$\alpha = 0.6$ & 0.02425659 & 8/31 & 1.711518 & 26 & 2 & $\alpha = 0.6$ & 0.02380529 & 11/31 & 1.727994 & 226 & 1 \\
\mycc$\alpha = 0.4$ & \mycc 0.021494 & \mycc 7/31 & \mycc 1.706627 & \mycc 29 & \mycc 2 & $\alpha = 0.4$ & 0.0214516 & 11/31 & 1.726991 & 319 & 1 \\
$\alpha = 0.2$ & 0.02001998 & 7/31 & 1.70708 & 32 & 2 & $\alpha = 0.2$ & 0.02007049 & 11/31 & 1.72622 & 393 & 1 \\
$\alpha = 0$ & 0.01946719 & 7/31 & 1.707758 & 36 & 2 & \mycc$\alpha = 0$ & \mycc 0.01921006 & \mycc 11/31 & \mycc 1.725663 & \mycc 472 & \mycc 1 \\ \bottomrule
\end{tabularx}
\end{table}
\end{landscape}
\end{document}
您似乎已经注意到,即使表格内容没有那么宽,水平线也会一直延伸到右边距。这是由于您使用了tabularx
我在上面的 MWE 中未更正的那个。如果您希望表格与文本宽度一样宽,并且愿意对表格进行轻微的重新设计,您可能会对以下 MWE 感兴趣(它也只需要一个\rowcolor
命令来为上述单元格着色):
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{pdflscape}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\begin{document}
\begin{landscape}
\begin{table}[ht]
\centering
\def\arraystretch{1.5}
\begin{tabular}{lccccc} \toprule
& \multicolumn{5}{c}{Hierarchical Clustering} \\ \cmidrule(lr){2-6}
& $\lambda$ & Misclass. & Deviance & Sig. Coef. & Sig. Groups\\ \midrule
$\alpha = 1$ & 0.05873092 & 11/31 & 1.741681 & 1 & 0 \\
$\alpha = 0.8$ & 0.02822086 & 11/31 & 1.732505 & 108 & 1 \\
$\alpha = 0.6$ & 0.02380529 & 11/31 & 1.727994 & 226 & 1 \\
\rowcolor{lightgray}$\alpha = 0.4$ & 0.0214516 & 11/31 & 1.726991 & 319 & 1 \\
$\alpha = 0.2$ & 0.02007049 & 11/31 & 1.72622 & 393 & 1 \\
$\alpha = 0$ & 0.01921006 & 11/31 & 1.725663 & 472 & 1 \\ \bottomrule
\end{tabular}
\hfill
\begin{tabular}{lccccc} \toprule
& \multicolumn{5}{c}{$K$-means Clustering} \\ \cmidrule(r){2-6}
& $\lambda$ & Misclass. & Deviance & Sig. Coef. & Sig. Groups \\ \midrule
$\alpha = 1$ & 0.05873092 & 11/31 & 1.730157 & 1 & 0 \\
$\alpha = 0.8$ & 0.0281825 & 8/31 & 1.735783 & 7 & 1 \\
$\alpha = 0.6$ & 0.02425659 & 8/31 & 1.711518 & 26 & 2 \\
$\alpha = 0.4$ & 0.021494 & 7/31 & 1.706627 & 29 & 2 \\
$\alpha = 0.2$ & 0.02001998 & 7/31 & 1.70708 & 32 & 2 \\
\rowcolor{lightgray} $\alpha = 0$ & 0.01946719 & 7/31 & 1.707758 & 36 & 2 \\ \bottomrule
\end{tabular}
\end{table}
\end{landscape}
\end{document}
从这次重新设计开始,您可能还需要重新考虑是否真的需要横向布局,因为您的表格可以完美地适应纵向页面的文本块(即使具有标准边距):
\documentclass{article}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}[ht]
\centering
\def\arraystretch{1.5}
\begin{tabular}{lccccc} \toprule
& \multicolumn{5}{c}{Hierarchical Clustering} \\ \cmidrule(lr){2-6}
& $\lambda$ & Misclass. & Deviance & Sig. Coef. & Sig. Groups\\ \midrule
$\alpha = 1$ & 0.05873092 & 11/31 & 1.741681 & 1 & 0 \\
$\alpha = 0.8$ & 0.02822086 & 11/31 & 1.732505 & 108 & 1 \\
$\alpha = 0.6$ & 0.02380529 & 11/31 & 1.727994 & 226 & 1 \\
\rowcolor{lightgray}$\alpha = 0.4$ & 0.0214516 & 11/31 & 1.726991 & 319 & 1 \\
$\alpha = 0.2$ & 0.02007049 & 11/31 & 1.72622 & 393 & 1 \\
$\alpha = 0$ & 0.01921006 & 11/31 & 1.725663 & 472 & 1 \\ \midrule
& \multicolumn{5}{c}{ $K$-means Clustering} \\ \cmidrule(r){2-6}
& $\lambda$ & Misclass. & Deviance & Sig. Coef. & Sig. Groups \\ \midrule
$\alpha = 1$ & 0.05873092 & 11/31 & 1.730157 & 1 & 0 \\
$\alpha = 0.8$ & 0.0281825 & 8/31 & 1.735783 & 7 & 1 \\
$\alpha = 0.6$ & 0.02425659 & 8/31 & 1.711518 & 26 & 2 \\
$\alpha = 0.4$ & 0.021494 & 7/31 & 1.706627 & 29 & 2 \\
$\alpha = 0.2$ & 0.02001998 & 7/31 & 1.70708 & 32 & 2 \\
\rowcolor{lightgray}$\alpha = 0$ & 0.01946719 & 7/31 & 1.707758 & 36 & 2 \\ \bottomrule
\end{tabular}
\end{table}
\end{document}
最后,这是我个人想要的布局。在这里,我使用了包S
中的类型列siunitx
,以便将数字与小数点对齐,并使用它>{\(}l<{\)}
自动设置数学模式下第一列的注释。
\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{@{\quad}>{\(}l<{\)}S[table-format=1.8]cS[table-format=1.6]S[table-format=3]c} \toprule
& $\lambda$ & Misclass. & {Deviance} & {Sig. Coef.} & Sig. Groups\\ \midrule
\multicolumn{6}{@{}l}{Hierarchical Clustering} \\
\alpha = 1 & 0.05873092 & 11/31 & 1.741681 & 1 & 0 \\
\alpha = 0.8 & 0.02822086 & 11/31 & 1.732505 & 108 & 1 \\
\alpha = 0.6 & 0.02380529 & 11/31 & 1.727994 & 226 & 1 \\
\rowcolor{lightgray}\alpha = 0.4 & 0.0214516 & 11/31 & 1.726991 & 319 & 1 \\
\alpha = 0.2 & 0.02007049 & 11/31 & 1.72622 & 393 & 1 \\
\alpha = 0 & 0.01921006 & 11/31 & 1.725663 & 472 & 1 \\ \midrule
\multicolumn{6}{@{}l}{$K$-means Clustering} \\
\alpha = 1 & 0.05873092 & 11/31 & 1.730157 & 1 & 0 \\
\alpha = 0.8 & 0.0281825 & 8/31 & 1.735783 & 7 & 1 \\
\alpha = 0.6 & 0.02425659 & 8/31 & 1.711518 & 26 & 2 \\
\alpha = 0.4 & 0.021494 & 7/31 & 1.706627 & 29 & 2 \\
\alpha = 0.2 & 0.02001998 & 7/31 & 1.70708 & 32 & 2 \\
\rowcolor{lightgray}\alpha = 0 & 0.01946719 & 7/31 & 1.707758 & 36 & 2 \\ \bottomrule
\end{tabular}
\end{table}
\end{document}
答案2
@leandriis 的一堆例子回答让我再举两个例子:
\documentclass{article}
\usepackage{booktabs,tabularx}
\usepackage{siunitx}
\usepackage[table]{xcolor}
\newcommand{\mycc}{\cellcolor{lightgray}}
\newcommand{\myccs}[2]{\cellcolor{lightgray}{\tablenum[table-format=#1]{#2}}}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
\begin{document}
\begin{table}[ht]
\small
\caption{With use of \texttt{siunitx} and its option \texttt{tablenum} and \texttt{tabularx}}
\setlength\tabcolsep{2.4pt}
\centering
\begin{tabularx}{\linewidth}{@{}
>{\columncolor{white}[0pt][\tabcolsep]}l
S[table-format=1.8]
r
S[table-format=1.6]
S[table-format=3]
>{\centering}X
@{\quad}
l
S[table-format=1.8]
r
S[table-format=1.6]
S[table-format=3]
>{\columncolor{white}[\tabcolsep][0pt]\centering\arraybackslash}X
@{}}
\toprule
& \multicolumn{5}{c}{$K$-means Clustering} &
& \multicolumn{5}{c}{Hierarchical Clustering} \\
\cmidrule(r){2-6} \cmidrule(r){8-12}
$\alpha$ & $\lambda$ & MC & {Deviance} & {SC} & SG &
$\alpha$ & $\lambda$ & MC & {Deviance} & {SC} & SG \\
\midrule
1 & 0.05873092 & 11/31 & 1.730157 & 1 & 0 &
1 & 0.05873092 & 11/31 & 1.741681 & 1 & 0 \\
0.8 & 0.0281825 & 8/31 & 1.735783 & 7 & 1 &
0.8 & 0.02822086 & 11/31 & 1.732505 & 108 & 1 \\
0.6 & 0.02425659 & 8/31 & 1.711518 & 26 & 2 &
0.6 & 0.02380529 & 11/31 & 1.727994 & 226 & 1 \\
\mycc{0.4}
& \myccs{1.8}{0.021494}
& \mycc{7/31}
& \myccs{1.6}{1.706627}
& \myccs{3}{29}
& \mycc{2} &
0.4 & 0.0214516 & 11/31 & 1.726991 & 319 & 1 \\
0.2 & 0.02001998 & 7/31 & 1.70708 & 32 & 2 &
0.2 & 0.02007049 & 11/31 & 1.72622 & 393 & 1 \\
0 & 0.01946719 & 7/31 & 1.707758 & 36 & 2 &
\mycc{0}
& \myccs{1.8}{0.01921006}
& \mycc{11/31}
& \myccs{1.6}{1.725663}
& \myccs{3}{472}
& \mycc{1} \\
\midrule[\heavyrulewidth]
\multicolumn{12}{p{\linewidth}}{
MC: Missclass.,
SG: Sig. Coef.,
SG: Sig. Groups}
\end{tabularx}
\end{table}
\begin{table}[ht]
\caption{With use of \texttt{siunitx} and \texttt{tabular}}
\centering
\begin{tabular}{S[table-format=1.1]
S[table-format=1.8]
c
S[table-format=1.6]
S[table-format=3]
c}
\toprule
$\alpha$ & $\lambda$ & Misclass. & {Deviance} & {Sig. Coef.} & Sig. Groups\\
\midrule
\multicolumn{6}{@{}l}{Hierarchical Clustering} \\
1 & 0.05873092 & 11/31 & 1.741681 & 1 & 0 \\
0.8 & 0.02822086 & 11/31 & 1.732505 & 108 & 1 \\
0.6 & 0.02380529 & 11/31 & 1.727994 & 226 & 1 \\
\rowcolor{lightgray}
0.4 & 0.0214516 & 11/31 & 1.726991 & 319 & 1 \\
0.2 & 0.02007049 & 11/31 & 1.72622 & 393 & 1 \\
0 & 0.01921006 & 11/31 & 1.725663 & 472 & 1 \\
\midrule
\multicolumn{6}{@{}l}{$K$-means Clustering} \\
1 & 0.05873092 & 11/31 & 1.730157 & 1 & 0 \\
0.8 & 0.0281825 & 8/31 & 1.735783 & 7 & 1 \\
0.6 & 0.02425659 & 8/31 & 1.711518 & 26 & 2 \\
0.4 & 0.021494 & 7/31 & 1.706627 & 29 & 2 \\
0.2 & 0.02001998 & 7/31 & 1.70708 & 32 & 2 \\
\rowcolor{lightgray}
0 & 0.01946719 & 7/31 & 1.707758 & 36 & 2 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
(红线表示文本边框)
答案3
该软件包提供了与传统环境类似的nicematrix
环境,但具有更多功能。特别是,您可以使用命令来为数组的矩形区域着色。此外,无论您使用哪种 PDF 查看器,都不会看到细白线。{NiceTabular}
{tabular}
\rectanglecolor
\documentclass[landscape]{article}
\usepackage{nicematrix}
\usepackage{pdflscape}
\usepackage{booktabs}
\usepackage[margin = 1in]{geometry}
\begin{document}
\begin{table}[ht]
\centering
\def\arraystretch{1.5}
\begin{NiceTabular}{lccccclccccc}
\CodeBefore
\rectanglecolor{lightgray}{6-1}{6-6}
\rectanglecolor{lightgray}{8-7}{8-12}
\Body
\toprule
& \multicolumn{5}{c}{$K$-means Clustering} & & \multicolumn{5}{c}{Hierarchical Clustering} \\ \cmidrule(r){2-6} \cmidrule(lr){8-12}
& $\lambda$ & Misclass. & Deviance & Sig. Coef. & Sig. Groups & & $\lambda$ & Misclass. & Deviance & Sig. Coef. & Sig. Groups\\ \midrule
$\alpha = 1$ & $0.05873092$ & $11/31$ & $1.730157$ & $1$ & $0$ & $\alpha = 1$ & $0.05873092$ & $11/31$ & $1.741681$ & $1$ & $0$ \\
$\alpha = 0.8$ & $0.0281825$ & $8/31$ & $1.735783$ & $7$ & $1$ & $\alpha = 0.8$ & $0.02822086$ & $11/31$ & $1.732505$ & $108$ & $1$ \\
$\alpha = 0.6$ & $0.02425659$ & $8/31$ & $1.711518$ & $26$ & $2$ & $\alpha = 0.6$ & $0.02380529$ & $11/31$ & $1.727994$ & $226$ & $1$ \\
$\alpha = 0.4$ & $0.021494$ & $7/31$ & $1.706627$ & $29$ & $2$ & $\alpha = 0.4$ & $0.0214516$ & $11/31$ & $1.726991$ & $319$ & $1$ \\
$\alpha = 0.2$ & $0.02001998$ & $7/31$ & $1.70708$ & $32$ & $2$ & $\alpha = 0.2$ & $0.02007049$ & $11/31$ & $1.72622$ & $393$ & $1$ \\
$\alpha = 0$ & $0.01946719$ & $7/31$ & $1.707758$ & $36$ & $2$ & $\alpha = 0$ & $0.01921006$ & $11/31$ & $1.725663$ & $472$ & $1$ \\ \bottomrule
\end{NiceTabular}
\end{table}
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。