texblog

Coloring multi-row tables in LaTeX

Since Apple introduced alternately colored rows in their lists in Mac OS X, it has become more and more popular to color tables in LaTeX in a similar fashion. Usually, this can be achieved fairly easily, see my post on the topic here. However, when using multirow, things become slightly more complicated. In this post, I will discuss a few cases and give appropriate examples.

Let’s first define a colour in the document preamble that will be of use later on:

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

An alternative would be to directly use the color gray!25 instead of defining a new color:

\rowcolors{1}{}{gray!25}

 
Next, it is very important that multirow cells are always defined in the last cell, rather than the first. Why is that? Let’s assume we combine 2 cells using \multirow. To do that, we place the \multirow in the first of the 2 cells. The text is now written as soon as the engine encounters the \multirow command, in the first cell. Since we combine 2 cells, however, the text gets placed between them. If we now add color to the second row, any text in that cell will be overlaid and becomes invisible. To prevent this, we combine the two cells by placing the \multirow command inside the second and inverting the sign of row numbers.

...
\multirow{-2}{*}{Multi-row}&Single-row\\
...

\documentclass{article}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\usepackage{multirow}
\begin{document}
\begin{minipage}{0.48\linewidth}
Multirow w/ hidden text
\centering
\rowcolors{1}{}{lightgray}
\begin{tabular}{cc}
\hline
\multirow{2}{*}{Multi-row}&Single-row\\
&Single-row\\
\hline
\end{tabular}
\end{minipage}
\qquad
\begin{minipage}{0.48\linewidth}
Multirow w/o hidden text
\centering
\rowcolors{1}{}{lightgray}
\begin{tabular}{cc}
\hline
&Single-row\\
\multirow{-2}{*}{Multi-row}&Single-row\\
\hline
\end{tabular}
\end{minipage}
\end{document}

 
With that, we are now ready to look at a few examples.

 

Coloring multirow cell with the same pattern

Nothing special to do here, except for what I mentioned earlier that \multirow always goes into the last cell.

\documentclass{article}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\usepackage{multirow} 
\begin{document}
\begin{table}[ht]
\rowcolors{1}{}{lightgray}
\centering
\caption{Multirow table with continuous coloring.}\label{tab:multi row}
\begin{tabular}{p{5cm}p{5cm}}
\hline
Column 1 & Column 2\\
\hline
-&-\\
-&-\\
&Single-row\\
&Single-row\\
\multirow{-3}{*}{Multi-row (3)}&Single-row\\
-&-\\
-&-\\
\hline
\end{tabular}
\end{table}%
\end{document}

 

Coloring all multirow cells

Coloring all cells combined by \multirow has to be done manually. It’s good practice to explicitly color all cells individually in case we add a row above, possibly shifting the pattern.

\documentclass{article}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\usepackage{multirow} 
\begin{document}
\begin{table}[ht]
\rowcolors{1}{}{lightgray}
\centering
\caption{Multirow table with all cells in the same color.}\label{tab:multi row}
\begin{tabular}{p{5cm}p{5cm}}
\hline
Column 1 & Column 2\\
\hline
-&-\\
-&-\\
\cellcolor{lightgray}&Single-row\\
\cellcolor{lightgray}&Single-row\\
\multirow{-3}{*}{\cellcolor{lightgray}Multi-row (3)}&Single-row\\
-&-\\
-&-\\
\hline
\end{tabular}
\end{table}%
\end{document}

 

Coloring multirow cells white

Basically, this case is the same as before using white instead of lightgray throughout all \multirow cells.

% replace
\cellcolor{lightgray}

% by 
\cellcolor{white}

 

Coloring all multirow rows

Similarly to cells, we might want to highlight the entire row of multirow cells in the same color. Again, this has to be done manually using \rowcolor{}.

\documentclass{article}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\usepackage{multirow} 
\begin{document}
\begin{table}[ht]
\rowcolors{1}{}{lightgray}
\centering
\caption{Multirow table with all rows in the same color.}\label{tab:multi row}
\begin{tabular}{p{5cm}p{5cm}}
\hline
Column 1 & Column 2\\
\hline
-&-\\
-&-\\
\rowcolor{lightgray}&Single-row\\
\rowcolor{lightgray}&Single-row\\
\rowcolor{lightgray}\multirow{-3}{*}{Multi-row (3)}&Single-row\\
-&-\\
-&-\\
\hline
\end{tabular}
\end{table}%
\end{document}
Exit mobile version