Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How do I get an entire table to get a background color. I dont mean the indivual cells but the entire table to get a gray shade. Something like this:

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

share|improve this question
add comment

2 Answers 2

I'd simply put the tabular inside a \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}

The local setting of \fboxsep is needed to avoid padding.

enter image description here

share|improve this answer
    
would it be doable to embed the background color into a new tabular environment? –  pluton Apr 2 '13 at 2:27
    
@pluton You need to know the precise dimensions of the foreground. –  egreg Apr 2 '13 at 8:23
add comment

I guess you can use the xcolor package as explained here, but instead of set up alternate table row colors, force to color odd and even rows using the same color:

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

Here is you have a full working example:

\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}

And the result:

enter image description here


EDIT: Following Mico's comment, there are two limitations:

  1. The space around lines created by the booktabs package's \toprule, \midrule, \cmidrule, and \bottomrule commands and
  2. Any intercolumn whitespace. E.g., if one specifies @{\extracolsep{\fill}} in the tabular* environment's second argument -- to force the overall width of the table to be equal to the environment's width argument (usually, but not necessarily, \textwidth) -- the extra intercolumn whitespace

won't be affected by the \rowcolor statement.

share|improve this answer
    
You may want to add a note mentioning two limitations: coloring won't be made for (i) the space around lines created by the booktabs package's \toprule, \midrule, \cmidrule, and \bottomrule commands or (ii) any intercolumn whitespace. E.g., if one specifies @{\extracolsep{\fill}} in the tabular* environment's second argument -- to force the overall width of the table to be equal to the environment's width argument (usually, but not necessarily, \textwidth) -- the extra intercolumn whitespace won't be affected by the \rowcolor statement. :-( –  Mico Jul 19 '12 at 11:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.