Coloring longtabus (with rowcolors)

Another desperate I-google-together-my-document person here: In my thesis I want to create light grey tables with darker grey top lines, but I encounter 2 problems:

If I use \rowcolor[RGB]{240,240,240} I get the result, but I can't figure out what I'm doing wrong when defining colors in the preamble. But that's the minor issue.

The big problem: I use a longtabu environment and can't get \rowcolors to work with it.

What I want it to look like (works):

\documentclass[11pt,a4paper, twoside]{scrbook}

\usepackage[english]{babel}
\usepackage{array}
\usepackage{longtable, tabu}
\usepackage[longtable,tabu]{xcolor}
\usepackage{colortbl}
\definecolor{grey1}{rgb}{240,240,240}
\definecolor{grey2}{rgb}{250,250,250}

\begin{document}

\begin{longtabu}[H]{XXl}
\caption[Chemicals and Reagents] {\textbf{Chemicals and Reagents}} \vspace{2mm}\\
\rowcolor[RGB]{240,240,240}
\textbf{Product} & \textbf{Manufacturer} & \textbf{Reference}\\
\rowcolor[RGB]{250,250,250}
Whatever & from whomever & 123456789\\
\rowcolor[RGB]{250,250,250}
Whatever & from whomever & 123456789\\
\rowcolor[RGB]{250,250,250}
Whatever & from whomever & 123456789\\
\end{longtabu}

\end{document}


The more elegant way I expected to work but doesn't:

\documentclass[11pt,a4paper, twoside]{scrbook}

\usepackage[english]{babel}
\usepackage{array}
\usepackage{longtable, tabu}
\usepackage[longtable,tabu]{xcolor}
\usepackage{colortbl}
\definecolor{grey1}{rgb}{240,240,240}
\definecolor{grey2}{rgb}{250,250,250}

\begin{document}

\rowcolors{1}{grey2}{grey2}
\begin{longtabu}[H]{XXl}
\caption[Chemicals and Reagents] {\textbf{Chemicals and Reagents}} \vspace{2mm}\\
\rowcolor{grey1}
\textbf{Product} & \textbf{Manufacturer} & \textbf{Reference}\\
Whatever & from whomever & 123456789\\
Whatever & from whomever & 123456789\\
Whatever & from whomever & 123456789\\
\end{longtabu}

\end{document}


Welcome to TeX.SX!

tabu needs the \taburowcolors command, not \rowcolors:

\documentclass[11pt,a4paper, twoside]{scrbook}

\usepackage{longtable,tabu}
\usepackage[table]{xcolor}
\definecolor{grey1}{gray}{0.6}
\definecolor{grey2}{gray}{0.9}

\begin{document}

\taburowcolors[2] 2 {grey2..grey2}
\begin{longtabu}[H]{XXl}
\caption{Chemicals and Reagents}\\[2mm]
\rowcolor{grey1}
\textbf{Product} & \textbf{Manufacturer} & \textbf{Reference}\\
Whatever & from whomever & 123456789\\
Whatever & from whomever & 123456789\\
Whatever & from whomever & 123456789\\
\end{longtabu}

\end{document}


result:

Deprecated Answer, without tabu

The problem with your second document is that the \rowcolors command requires the table option for the package xcolor.

\documentclass[11pt,a4paper, twoside]{scrbook}

\usepackage{longtable}
\usepackage[table]{xcolor}
\definecolor{grey1}{gray}{0.6}
\definecolor{grey2}{gray}{0.9}

\begin{document}

\rowcolors{2}{grey2}{grey2}
\begin{longtable}{lll}
\caption{Chemicals and Reagents}\\[2mm]
\rowcolor{grey1}
\textbf{Product} & \textbf{Manufacturer} & \textbf{Reference}\\
Whatever & from whomever & 123456789\\
Whatever & from whomever & 123456789\\
Whatever & from whomever & 123456789\\
\end{longtable}

\end{document}


I have changed some other things, like switching from longtabu to longtable. I've been wary of tabu since encountering this thread a while back. Your mileage may vary though, and that's certainly not where the problem in your example was located. Just thought I'd mention it.

I have also changed the way I define the grey1 and grey2 colors just to show you that the possibility exists; you can of course change it back to the RGB model if you prefer (or change the tone of your greys).

Also, you can just pass [2mm] as an optional argument to the \\ command; no need to insert a \vspace before \\.

Result:

• Thank you alpenwasser for the quick answer! I already tried around with other table/longtable environments but I want to use the X columns that's why I'm using longtabu and that's giving me a "undefined control sequence" error. Is there a way to use longtabu? Otherwise I really might need to figure out the ltxtable package. – Anne May 13 '17 at 15:34
• the table option from xcolor package loads the colortbl package, so there's no longer a need to load it separately. additionally, do you know if longtable is a valid option for xcolor? – Troy May 13 '17 at 15:37
• @Troy Yeah, I was just browsing the xcolor docs to see if I'd missed anything, thanks for the catch! longtable is indeed not a valid option, correct, and the example still works without the separate loading of colortbl. Will update my code accordingly. – alpenwasser May 13 '17 at 15:42
• @Anne I can get this to work with longtabu, more or less. But there is a small issue: \rowcolors{2}... still seems to color the caption; it doesn't seem to respect the starting value for where it is supposed to start coloring rows. But I understand your desire for those nice X columns; I'll see what can be done. – alpenwasser May 13 '17 at 15:44
• Ha! It actually works if I just load the table packages seperately as \usepackage{longtable, tabu} and \usepackage[table]{xcolor} ! But you are right with the caption. – Anne May 13 '17 at 15:52

With tabu you should use \taburowcolors for alternating colors. And if you want to define the color with values "240" etc, then you must use RGB as model, not rgb.

\documentclass[11pt,a4paper, twoside]{scrbook}

\usepackage[english]{babel}
\usepackage{array}
\usepackage[table]{xcolor}
\usepackage{longtable, tabu}

\definecolor{grey1}{RGB}{240,240,240}
\definecolor{grey2}{RGB}{250,250,250}

\begin{document}

\taburowcolors[1]{grey1..grey2}
\begin{longtabu}[H]{XXl}
\caption[Chemicals and Reagents] {\textbf{Chemicals and Reagents}} \vspace{2mm}\\
\rowcolor{red}%to show it better
\textbf{Product} & \textbf{Manufacturer} & \textbf{Reference}\\
Whatever & from whomever & 123456789\\
Whatever & from whomever & 123456789\\
Whatever & from whomever & 123456789\\
\end{longtabu}

\end{document}