texblog

Highlight table rows/columns with color

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 :-).

Exit mobile version