The following post will show you how to better present your data in tables through coloring rows/columns or even single cells to highlight important data.
You will need the following two packages, the first to define new colors and the latter to actually color the table:
\usepackage{color, colortbl}
Let me first show you how to define a new color. There is different ways depending on the color system. I will use a light gray and a light cyan.
\definecolor{name}{system}{definition}
Light gray:
\definecolor{Gray}{gray}{0.9}
Light cyan:
\definecolor{LightCyan}{rgb}{0.88,1,1}
For the “RGB”-system, I was using this to pick a nice color. Just divide the numbers by 255 and you’ll get what you need.
By the way, for convenience purposes, I am using the LaTeX pseudo random number generator in the examples below. Just in case you’re trying to reuse my code examples.
\usepackage[first=0,last=9]{lcg} \newcommand{\ra}{\rand0.\arabic{rand}}
Now that we have some colors, we can start to use them on our table. Lets start with rows. The following table will have rows which are colored white and light cyan alternatively, using the previously defined color “LightCyan”:
\rowcolor{LightCyan}
Complete code example:
\begin{table}[ht] \centering \begin{tabular}{c|ccccccc} \hline & col1 & col2 & col3 & col4 & col5 & col6 & col7 \\ \hline \rowcolor{LightCyan} row1& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ row2& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ \rowcolor{LightCyan} row3& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ row4& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ \rowcolor{LightCyan} row5& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ row6& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ \hline \end{tabular} \end{table}
Furthermore, coloring rows alternatively is similarly easy. This time we are using light gray (defined above) and white. Here it makes sense to define a new colored column type, so we don’t have to retype the thing every time:
\newcolumntype{g}{>{\columncolor{Gray}}c}
The name of the new column type is “g”. If you only need to color one column you may want to use >{\columncolor{Gray}}c
in the tabular environment definition directly.
Complete code example:
\newcolumntype{g}{>{\columncolor{Gray}}c} \begin{table}[ht] \centering \begin{tabular}{c|g|c|g|c|g|c|g} \hline &col1 &col2 &col3 &col4 & col5 &col6 &col7\\ \hline row1& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ row2& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ row3& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ row4& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ row5& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ row6& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ \hline \end{tabular} \end{table}
You can combine the two in case you want to alternatively color rows, but not the first column. Use the defined, colored row type to color all the data columns and recolor the title row as well as every second data-row white:
\newcolumntype{g}{>{\columncolor{Gray}}c} \begin{table}[ht] \centering \begin{tabular}{c|g|g|g|g|g|g|g} \hline \rowcolor{white} &col1 &col2 &col3 &col4 & col5 &col6 &col7\\ \hline \rowcolor{white} row1& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ row2& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ \rowcolor{white} row3& \ra & \ra & \ra & \ra & \ra & \ra & \ra \\ ... \hline \end{tabular} \end{table}
Finally, LaTeX also lets you color single cells using the following command inside a cell:
\cellcolor{Gray}
I’ll let you do the chessboard-like table yourself :-).
To do alternating row colors, you can also include the xcolor package with the table option (i.e.
\usepackage[table]{xcolor}
), and immediately preceding the tabular environment use the command— this will produce a table with colored rows, starting at the row indicated by start, with odd rows colored color1 and even rows colored color2.
A bit simpler than putting the rowcolor command before each row.
Adding color to tables « Random Determinism
[…] Texblog had an interesting post on creating tables with alternating colors. See the pdf for the final output. I thought that it will be interesting to see how to reproduce the same effect in ConTeXt. This is the table that I am going to use for my tests: […]
Terse and to the point. Helps without distracting. Excellent.
Thank you, this post was helpful! I flattr’ed your twitter account https://flattr.com/thing/1076396
Cool, thanks!! Appreciate it. Best, Tom.
Nice article, thank you very much. It works like a charm 😉
For some reason using the \rowcolors command seems to do strange things to the table’s border colours. Especially if this table moves to a different part of the page. Very frustraiting.
Hi Julia,
Could you send a small code example for illustration. I’d be interested to see that and might be able to help…
Do you know whether it’s possible to have the highlighting get turned on piecewise in a beamer presentation?
Hi Mallika,
Either you could repeat the entire table as shown in the code below or use a more sophisticated solution proposed here.
Thanks for this – really helpful. I just wanted to say that
\columncolor
doesn’t work well with\cline
– the colour of the column obscures the line – and that if you wish to colour a table that uses\cline
you should switch to\hhline
instead.Thanks much! Cheers, Tom
i can’t do for \cellcolor, can you give me illustration please?
Hi there,
Thanks for your comment. Here is a simple example:
I know there’s an issue with figures and attachement on my blog. Will fix it asap.
Cheers, Tom.
Hi Tom! I hava a question: How do you make color in your code?
If you refer to the LaTeX sourcecode on this blog, see the WordPress support page on source code here.
Hi Tom,
I have been trying to re-do your combination example, but the borders of some columns are misteriously disappearing… Here is my code :
Some vertical lines are missing and the one to the left is disappearing only for the white rows.
Thank you in advance for your help.
Vincent
Hi Vincent,
Thanks for the nice code example. Unfortunately, I’m not able to reproduce the issue. I can see all vertical lines. Make sure it’s not just the rendering on your screen by zooming in onto the table.
You may want to try the following line of code. Let me know if the vertical lines still disappear.
\begin{tabular}{|c|*{6}{>{\hspace{-\tabcolsep}\,}b<{\hspace{-\tabcolsep}\,}|}}[\sourcecode] I got the code from here.
Thank you very much. Can you also tell us about a package that colors a figure just like ‘colortbl’ colors table rows.
Hi Muhammad,
You’d have to be a bit more specific. A figure is usually loaded from a separate file. Therefore, I don’t see how you’d want to color it. See the PGF/TikZ package for how to generate figures with LaTeX.
Best, Tom
Nice, couldnt be explained any better