我想为booktabs
表格中的某些行着色。我尝试了以下方法
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{colortbl}% http://ctan.org/pkg/colortbl
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{booktabs}
\colorlet{tableheadcolor}{gray!25} % Table header colour = 25% gray
\newcommand{\headcol}{\rowcolor{tableheadcolor}} %
\colorlet{tablerowcolor}{gray!10} % Table row separator colour = 10% gray
\newcommand{\rowcol}{\rowcolor{tablerowcolor}} %
\begin{document}
begin{table}[ht]\centering
\begin{tabular}{@{}rrrr@{}}\toprule
\headcol & \multicolumn{3}{c}{\textbf{SBR:} FC vs HC} \\
\rowcol & BA: mean $\pm 2 \sigma$ & $r$ & $p$ \\
\midrule
Operator 1 & $0.2^{+2.35}_{-1.96}$ & 0.95 & $<0.001$ \\
Operator 2 & $0.06^{+1.7}_{-1.6}$ & 0.97 & $<0.001$ \\
\midrule
\headcol & \multicolumn{3}{c}{\textbf{SBR:} Operator 1 vs Operator 2} \\
\midrule
FC & $0.37^{+1.92}_{-1.18}$ & 0.97 & $<0.001$ \\
HC & $0.26^{+1.73}_{-1.22}$ & 0.97 & $<0.001$ \\
\midrule
\headcol & \multicolumn{3}{c}{\textbf{SBR:} FC vs HC (Operator 2 reconstruction)} \\
& $0.01^{+0.06}_{-0.59}$ & 0.99 & $<0.001$ \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
但是,顶行的着色失败了。只有单元格 1 具有所需的单元格着色,而单元格 2-4 则\multicolumn
失败了。我的错误在哪里?
另外,有没有关于如何使表格(特别是第 3 列)更美观的建议
编辑
我还没有完全搞定,因为第二行的颜色比其他行延伸得更远。这让表格变得凌乱。这是我的新代码
\renewcommand{\arraystretch}{1.2}
\begin{table}[ht]\centering
\begin{tabular}{@{}rccc@{}}
\headcol & \multicolumn{3}{c}{\textbf{SBR:} FC vs HC} \\
\rowcol & BA: mean $\pm 2 \sigma$ & $r$ & $p$ \\
Operator 1 & $0.2^{+2.35}_{-1.96}$ & 0.95 & $<0.001$ \\
Operator 2 & $0.06^{+1.7}_{-1.6}$ & 0.97 & $<0.001$ \\
\headcol & \multicolumn{3}{c}{\textbf{SBR:} Operator 1 vs Operator 2} \\
FC & $0.37^{+1.92}_{-1.18}$ & 0.97 & $<0.001$ \\
HC & $0.26^{+1.73}_{-1.22}$ & 0.97 & $<0.001$ \\
\headcol & \multicolumn{3}{c}{\textbf{SBR:} FC vs HC (Op 2 reconstruction)} \\
& $0.01^{+0.06}_{-0.59}$ & 0.99 & $<0.001$ \\
\end{tabular}
\label{tab:QuantResults}
\end{table}
下表显示
答案1
即使不使用颜色,您也应该始终使用@{}
(和|
),否则跨越单元格的填充会与它们跨越的单元格不同。颜色只会使错误更加明显。
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{colortbl}% http://ctan.org/pkg/colortbl
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{booktabs}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
\colorlet{tableheadcolor}{gray!25} % Table header colour = 25% gray
\newcommand{\headcol}{\rowcolor{tableheadcolor}} %
\colorlet{tablerowcolor}{gray!10} % Table row separator colour = 10% gray
\newcommand{\rowcol}{\rowcolor{tablerowcolor}} %
\begin{document}
\renewcommand{\arraystretch}{1.2}
\begin{table}[ht]\centering
\begin{tabular}{@{}rccc@{}}
\headcol & \multicolumn{3}{c@{}}{\textbf{SBR:} FC vs HC} \\
\rowcol & BA: mean $\pm 2 \sigma$ & $r$ & $p$ \\
Operator 1 & $0.2^{+2.35}_{-1.96}$ & 0.95 & $<0.001$ \\
Operator 2 & $0.06^{+1.7}_{-1.6}$ & 0.97 & $<0.001$ \\
\headcol & \multicolumn{3}{c@{}}{\textbf{SBR:} Operator 1 vs Operator 2} \\
FC & $0.37^{+1.92}_{-1.18}$ & 0.97 & $<0.001$ \\
HC & $0.26^{+1.73}_{-1.22}$ & 0.97 & $<0.001$ \\
\headcol & \multicolumn{3}{c@{}}{\textbf{SBR:} FC vs HC (Op 2 reconstruction)} \\
& $0.01^{+0.06}_{-0.59}$ & 0.99 & $<0.001$ \\
\end{tabular}
\label{tab:QuantResults}
\end{table}
\end{document}