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}
Micha
Thanks for those tips! This is exactly the problem I struggled with in the last two week. Too bad the submission deadline for my documentation was last friday; but thanks to your post, I can handle this issue it in the future 🙂
tom
Thanks Micha. Too bad I was a bit too late with this. Cheers, Tom
Christophe Trefois
Nice post.
However, I’m not sure if Apple typography practices should be taken as LaTeX practices so soon.
I’m just gonna leave this here.
http://darkhorseanalytics.com/blog/wp-content/uploads/2014/03/ClearOffTheTableMd.gif
tom
Hi Christophe,
Thanks for your comment, I completely agree. However, when I mentioned Apple all I had in mind was alternate coloring of rows. This simple trick increases the readability of a tables which is ultimately what the post is about. I could have been more clear on that point.
Thanks for the link, really cool animation…
Tom
Arwen
great work!! Thank you very much!
ccroche
I’m definitely looking for a way to colour rows not alternatively like here above, but once every nth rows (like I can do in MS Excel…), but automatically, like for even and odd row colours. Would somebody know how this is feasible?
tom
Coloring every even and odd row with a different color can be done as shown here. For a different pattern, you would probably have to do it manually.
Cheers, Tom.
Raphael
Hi Tom, this is an excellent post and helped me already a lot. The only problem I am struggling with and on which I did not find a solution yet is the text alignment of the multirow cells. I don’t want the text to be aligned in the center of the multirow cells but at the top. Do you have any suggestions on that?
Thanks and best regards,
Raphael
tom
Hi Raphael,
Could you just place the content in the top cell then? Would that be a problem? Please provide a minimal working example.
Thanks, Tom
Alex
Hi there!
Thank you for the fantastic work!
Could you please show how this could be generalised in the case where you have more than one multirow? (i.e.http://tex.stackexchange.com/questions/167366/combining-multirow-and-multicolumn) and then wanted to color the “Numeric literals” and the “integers” column?
Many thanks again!
A.
tom
Hi Alex,
Thanks for your question and the link to the example. Given the table, there are several issues that arise when trying to color alternate rows as shown in the post above. For more complex tables, it may sometimes be easier to manually color individual rows and additional cells.
Cheers, Tom
Hani
Hi Tom,
Thanks a lot for your technique. I’ve been struggling with it lately and your post helped me a lot. Just one question, how can I color two consecutive rows and still have the solid border line between? When you color them the border line is gone (\hline is still present in the latex code but it doesn’t show up in the table!)
Thanks,
Hani
tom
Hi Hani,
Thanks for your question. In the minimal example below, the line between two colored rows is visible. Please provide a similar example that illustrates your problem.
Thanks, Tom.
Milind Sharma
This is so useful. Thanks a lot!