In one of my previous posts I describe how to color or shade every other row in a LaTeX table. However, there is a much nicer, cleaner and simpler way to color every odd/even row, making tables much more readable.
First load the xcolor
package in the preamble:
\usepackage[table]{xcolor}
and define any color you like, e.g. light gray:
\definecolor{lightgray}{gray}{0.9}
Set the row color(s) just before opening the tabular
environment for even and odd rows:
\rowcolors{<starting row index>}{<odd row color>}{<even row color>}
To use only one color, leave either of the row color arguments blank.
Complete code example:
\documentclass[11pt]{article} \usepackage[table]{xcolor} \definecolor{lightgray}{gray}{0.9} \begin{document} \begin{table}[ht] \caption{default} \begin{center} \rowcolors{1}{}{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}
Nope, the code box above was colored in a different way :-).
And here is the result:
If you like the way Apple colors/colored tables, try using light blue:
\definecolor{lightblue}{rgb}{0.93,0.95,1.0}
I used R to generate the table:
library(xtable) xtable(replicate(5, rnorm(10)))
Thanks to TJ Ellis for his comment.
Addendum:
Use the following code in your preamble to achieve the same effect automatically throughout the document. It redefines the tabular environment by adding the rowcolors
command before \begin{tabular}
.
\usepackage[table]{xcolor} \definecolor{lightgray}{gray}{0.9} \let\oldtabular\tabular \let\endoldtabular\endtabular \renewenvironment{tabular}{\rowcolors{2}{white}{lightgray}\oldtabular}{\endoldtabular}
Hi, Can the the xcolor package be used for completely coloring the table in one color – something like this table: http://www.iom.edu/Reports/2011/Breast-Cancer-and-the-Environment-A-Life-Course-Approach/~/media/Files/Report%20Files/2011/Breast-Cancer-Environment/BC_tableforweb.jpg I am trying to replicate that table design for my report. The colortbl has a pretty limited palette. Do you have any pointers on how to replicate that table design? Thanks!
You can always define your own color and use it to color columns. Here is an example:
Have a look at this article.
Cheers, Tom.
Thanks for the Addendum, Nikos
Sometimes the last row contains totals etc. and one does not want to color that but separate it with a libne from the rest of the table: How is that done?
Hi Christiaan,
You could always overwrite the color using
\rowcolor
on the last row.Might be of some interest: http://tex.stackexchange.com/a/61748/8272
Thanks for the link, wasn’t aware of that discussion :-).
Hi tom,,,
how can we define the width of \hline ? mean bold etc,
secondly, if we to produce a dotted line (……..) or (_ _ _ _ _), etc, can this be done via \hline command???
thanks.
Hi!
Take a look at the booktabs and arydshln packages.
Cheers, Tom.
thanks tom, good article…… 🙂
cheers.
Hi, thanks for your article. I think there is a typo in the addendum, don’t you want to load the xcolor package instead of color? The color package does not have a table option. It works well with the xcolor package though.
Hi Julien,
Thanks for the correction, it is fixed now.
Best, Tom