为整个表格涂色

为整个表格涂色

如何让整个表格获得背景颜色。我不是指单个单元格,而是整个表格获得灰色阴影。像这样:

http://img818.imageshack.us/img818/8684/38613229.png

答案1

我只是把它放在tabular里面\colorbox

\documentclass{article}
\usepackage{booktabs,xcolor,siunitx}
\definecolor{lightgray}{gray}{0.9}

\begin{document}
\begingroup\setlength{\fboxsep}{0pt}
\colorbox{lightgray}{%
  \begin{tabular}{l*{4}{S[table-format=3.2]}@{}}
  \toprule
  Method & \multicolumn{4}{c}{Recognition rate (\%) vs.\ illumination} \\
  \cmidrule{2-5}
  & \multicolumn{1}{c}{Subset 2} &
    \multicolumn{1}{c}{Subset 3} &
    \multicolumn{1}{c}{Subset 4} &
    \multicolumn{1}{c}{Subset 5} \\
  \midrule
  Linear subspace [9]  & 100.00 & 100.00 &  85.00 &  {n/a} \\
  Cones-attached [9]   & 100.00 & 100.00 &  91.40 &  {n/a} \\
  Cones-cast [9]       & 100.00 & 100.00 & 100.00 &  {n/a} \\
  PCA                  &  98.33 &  79.17 &  30.00 &  15.79 \\
  LTV${}+{}$PCA        & 100.00 &  99.17 &  96.43 &  92.11 \\
  Our method${}+{}$PCA & 100.00 & 100.00 & 100.00 & 100.00 \\
  \bottomrule
  \end{tabular}%
}\endgroup
\end{document}

\fboxsep需要的本地设置以避免填充。

在此处输入图片描述

答案2

我想你可以xcolor按照说明使用包这里,但不要设置备用表格行颜色,而是强制使用相同的颜色为奇数行和偶数行着色:

\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\rowcolors{1}{gray}{gray}

这是一个完整的工作示例:

\documentclass[11pt]{article}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\begin{document}
\begin{table}[ht]
\caption{default}
\begin{center}
\rowcolors{1}{lightgray}{lightgray}
\begin{tabular}{r|rrrrr}
  \hline
 & 1 & 2 & 3 & 4 & 5 \\
  \hline
1 & 2.36 & 1.08 & -0.49 & -0.82 & -0.65 \\
  2 & -0.68 & -1.13 & -0.42 & -0.72 & 1.51 \\
  3 & -1.00 & 0.02 & -0.54 & 0.31 & 1.28 \\
  4 & -0.99 & -0.54 & 0.97 & -1.12 & 0.59 \\
  5 & -2.35 & -0.29 & -0.53 & 0.30 & -0.30 \\
  6 & -0.10 & 0.06 & -0.85 & 0.10 & -0.60 \\
  7 & 1.28 & -0.46 & 1.33 & -0.66 & -1.80 \\
  8 & 0.80 & 0.46 & 1.37 & 1.73 & 1.93 \\
  9 & -0.75 & 0.28 & 0.51 & 0.19 & 0.58 \\
  10 & -1.64 & -0.12 & -1.17 & -0.10 & -0.04 \\
   \hline
\end{tabular}
\end{center}
\end{table}
\end{document}

结果如下:

在此处输入图片描述


编辑:下列的Mico 的评论,有两个限制:

  1. booktabs包的\toprule\midrule\cmidrule\bottomrule命令创建的行周围的空间以及
  2. @{\extracolsep{\fill}}任何列间空白。例如,如果在环境的第二个参数中指定 tabular*-- 强制表格的整体宽度等于环境的宽度参数(通常但不一定 \textwidth) -- 额外的列间空白

不会受到 \rowcolor 语句的影响。

答案3

{NiceTabular}的环境nicematrix具有为整个数组着色的工具(以及为行、列、单元格和块着色的工具)。我们保留在表格中逐字使用的能力。

\documentclass{article}
\usepackage{booktabs,xcolor,siunitx}
\usepackage{nicematrix}

\begin{document}


\begin{NiceTabular}{l*{4}{S[table-format=3.2]}@{}}
\CodeBefore
  \arraycolor[gray]{0.9}
\Body
\toprule
Method & \multicolumn{4}{c}{Recognition rate (\%) vs.\ illumination} \\
\cmidrule{2-5}
& \multicolumn{1}{c}{Subset 2} &
  \multicolumn{1}{c}{Subset 3} &
  \multicolumn{1}{c}{Subset 4} &
  \multicolumn{1}{c}{Subset 5} \\
\midrule
Linear subspace [9]  & 100.00 & 100.00 &  85.00 &  {n/a} \\
Cones-attached [9]   & 100.00 & 100.00 &  91.40 &  {n/a} \\
Cones-cast [9]       & 100.00 & 100.00 & 100.00 &  {n/a} \\
PCA                  & \verb|&#&&| &  79.17 &  30.00 &  15.79 \\
LTV${}+{}$PCA        & 100.00 &  99.17 &  96.43 &  92.11 \\
Our method${}+{}$PCA & 100.00 & 100.00 & 100.00 & 100.00 \\
\bottomrule
\end{NiceTabular}

\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

相关内容