# row color and a toggle

I'd like to create a new command that I would repeat in front of each row, and which would colorize the row if a toggle is true, and would leave it uncolored if the toggle is false. I would change the state of the toggle at some specific places, for the following rows to be colorized or not.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{array}
\usepackage{etoolbox}
\usepackage{colortbl}

\begin{document}

\newtoggle{fond}
\providecommand{\chfd}%
{\iftoggle{fond}%
{\rowcolor[rgb]{.95,.95,1}}%
{}%
}%

\toggletrue{fond}

\begin{tabular}{cc}
1 & 2 \\

\hline

\chfd a & b \\
\togglefalse{fond}%
\chfd c & d \\
\chfd e & f \\
%\toggletrue{fond}
\chfd g & h \\
\end{tabular}
\end{document}


Appart from the \hline (which seems to be cancelled by the following \chfd), it does what I expect up to the 'd' cell. But why is the e-f row colorized back? I would expect the toggle to have stayed in its 'false' state, and the row to be uncolored?

And I would expect to have to set the toggle to 'true' for the g-h row to be colorized... Actually it is colorized anyway, and if I uncomment the %\toggletrue{fond} I get an error :

Misplaced \noalign.
\rowcolor ->\noalign
{\ifnum 0=}\fi \global \let \CT@do@color \CT@@do@color...
l.30 \chfd
g & h \\


What am I doing wrong?

• Have a look at \rowcolors in the xcolor package. It does what (I think) you want. See page 35 of the xcolor documentation for a good example.
– user30471
Feb 2, 2016 at 11:06
• in tabular each column is group so \togglefalse{fond} is local to that raw (in fact to first column) you need to make it global by \global\togglefalse{fond} Feb 2, 2016 at 11:09
• @touhami: I think it's rather an issue of an incomplete test
– user31729
Feb 2, 2016 at 11:18
• @ChristianHupfer well, yes, there is 2 issues Feb 2, 2016 at 11:34
• @ChristianHupfer What do you mean by an incomplete test? Feb 2, 2016 at 20:33

\rowcolors is (like \multicolumn) rather picky. It wants to be the first thing in a row. So you can't execute every code before it. With \togglefalse it doesn't matter as the \rowcolors is then not seen, but \toogletrue should be either in the cell before or hidden in a \noalign. In both cases it then must be global:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{array}
\usepackage{etoolbox}
\usepackage{colortbl}

\begin{document}

\newtoggle{fond}
\providecommand{\chfd}%
{\iftoggle{fond}%
{\rowcolor[rgb]{.95,.95,1}}%
{}%
}%

\togglefalse{fond}

\begin{tabular}{cc}
\chfd a & b \global\toggletrue{fond} \\
\chfd c & d \\
\end{tabular}

\togglefalse{fond}
\begin{tabular}{cc}
\chfd a & b \\\noalign{\global\toggletrue{fond}}
\chfd c & d \\
\end{tabular}

\end{document}


• Thank you Ulrike, it works. I'm still not quite sure that my code is really correct, because as I mentioned the \hline seems to be cancelled by the following \chfd. But that's not as important and I can live with it. Feb 2, 2016 at 11:54
• The \hline is there, but rowcolors can (partly) overwrite them. I normally redraw the lines later e.g. with tikz, see e.g. tex.stackexchange.com/questions/134263/… Feb 2, 2016 at 12:04
• A solution is often to use \hhline instead of \hline`. Feb 2, 2016 at 12:22