# Alternating row color two at a time in tables

How can I achieve alternating row colors in tables for two rows at a time? So two rows should have the same color, then two other rows, and so on. In the following example

Bla1 & reference1 \\
& Description1 \\


should have one color and

Bla2 & reference2 \\
& Description2 \\


should have another. Then start alternating again. How can this be achieved?

\documentclass[a4paper]{scrartcl}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[table]{xcolor}

\begin{document}

\section{Section}
\begin{tabular}{lp{10cm}}
\hline
\textbf{Col 1} & \textbf{Col 2} \\ \hline
Bla1 & reference1 \\
& Description1 \\
Bla2 & reference2 \\
& Description2 \\ \hline
\end{tabular}
\end{document}


Gonzalo Medina's solution works fine, however if one uses the glossaries package the acronyms table will have row colors as well. How could I prevent the acronyms table to have this behavior?

-
If the alternating colors must apply only to one particular table, move the \rowcolors line from the preamble to a group containing the table the should have the colors; for example: {\rowcolors{1}{white}{blue!10}\begin{tabular}{Lp{10cm}}<contents>\end{tabular}}‌​ (notice the extra pair of outer braces). – Gonzalo Medina Jun 11 '12 at 17:46
@GonzaloMedina It's more like: It applies to 99 tables, but not for the acronyms table :( – cherrung Jun 11 '12 at 18:01
Then you can use \rowcolors{1}{}{} before the acronyms. – Gonzalo Medina Jun 11 '12 at 18:02

## 3 Answers

Here's a possible solution; the idea is to use an auxiliary counter for the rows, and depending on the value of this counter module 4, to set \rownum (internally used to decide wich color apply to a row) to 0 or to 1; the etoolbox package was used to patch tabular to set the row counter to 0:

\documentclass[a4paper]{scrartcl}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{pgf}
\usepackage{etoolbox}

\rowcolors{1}{white}{blue!10}

\newcounter{row}
\newcolumntype{L}{%
>{\stepcounter{row}%
\pgfmathtruncatemacro{\j}{int(Mod(\therow,4))}
\ifnum\j<2
\global\rownum=1
\else
\global\rownum=0
\fi}%
l}

\BeforeBeginEnvironment{tabular}{\setcounter{row}{0}}

\begin{document}

\section{Section}

\begin{tabular}{Lp{10cm}}
\rowcolor{blue!10}Bla1 & reference1 \\
& Description1 \\
Bla2 & reference2 \\
& Description2 \\
Bla3 & reference3 \\
& Description3 \\
Bla4 & reference4 \\
& Description4 \\
Bla5 & reference5 \\
& Description5 \\
Bla6 & reference6 \\
& Description6 \\
\end{tabular}

\end{document}


-
It's probably better to \setcounter{row}{0} before the tabular: in case two tabulars of this kind are used the previous end value would be retained. – egreg Jun 9 '12 at 22:16
@egreg you're right. I'll add the correction. Thanks. – Gonzalo Medina Jun 9 '12 at 22:16

You can use xcolor package \rowcolors command but just modify slightly to only change every other row.

\documentclass[a4paper]{scrartcl}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[table]{xcolor}

\rowcolors{0}{red}{yellow}
\newcount\xrownum
\makeatletter
\def\@rowc@lors{\noalign{%
\global\advance\xrownum\@ne
\ifodd\xrownum
\global\advance\rownum\@ne
\fi
}\@rowcolors}

\makeatother

\begin{document}

\section{Section}
\begin{tabular}{lp{10cm}}
\hline
\textbf{Col 1} & \textbf{Col 2} \\ \hline
Bla1 &{reference1} \\
Bla1 &{reference1} \\
Bla1 &{reference1} \\
Bla1 &{reference1} \\
Bla1 &{reference1} \\
& Description1 \\
& Description1 \\
Bla2 & {reference2} \\
& Description2 \\ \hline
\end{tabular}
\end{document}

-

In ConTeXt, the same idea as Gonzalo's solution can be implemented as follows:

\definecolor[lightblue][r=0.8,g=0.8,b=1]
\defineconversion[tablecolor][lightblue,lightblue,white,white]

\startsetups doublerow:color
\setupTABLE[row][each]
[background=color,
backgroundcolor={\convertnumber{tablecolor}{\positiverow}}]
\stopsetups

\starttext
\startTABLE[setups={doublerow:color}]
\dorecurse{10}
{\NC One \NC Two \NC Three \NC \NR}
\stopTABLE
\stoptext

-