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?


\rowcolorsin the xcolor package. It does what (I think) you want. See page 35 of thexcolordocumentation for a good example. – Feb 02 '16 at 11:06\togglefalse{fond}is local to that raw (in fact to first column) you need to make it global by\global\togglefalse{fond}– touhami Feb 02 '16 at 11:09\iftoggleisn't completed due to the group feature of atabularcell – Feb 02 '16 at 20:41