# How to conditionally shade / color a table row using datatool?

I have been trying to shade an entire row in a table conditionally on the value of an specific entry in that row.

I was trying to adapt the "stripy table" example in datatool, but it seems that I am messing up the new conditional statement. In the example below I kept the original stripy table conditional but commented it out.

Any help will be greatly appreciated.

\documentclass{article}

\usepackage{datatool}
\usepackage{colortbl}

\begin{filecontents*}{bogus.csv}
FirstName,Surname,StudentNo,Score
John,"Smith, Jr",102689,68
Jane,Brown,102647,75
Andy,Brown,103569,42
Clare,Verdon,104356,45
John,"Smith, Jr",102683,68
Clare,Verdon,104356,45
\end{filecontents*}

\begin{document}

\begin{table}[htbp]
\caption{Conditionally colored rows}\label{tab:colored_row}
\centering
\begin{tabular}{llc}
\bfseries First Name &
\bfseries Surname &
\DTLforeach{dat}
{\firstname=FirstName, \surname=Surname,\score=Score} %
{\\ % skip line
%\DTLifoddrow{ \rowcolor{blue} }{ \rowcolor{green} } % stripy conditional. works fine.
\DTLifnumlt{\score}{60}{\rowcolors{geen}}{\rowcolors{blue}} % my conditional. no go.
\firstname & \surname & \score} %
\end{tabular}
\end{table}

\end{document}


Here a solution :

\documentclass{article}

\usepackage{datatool}
\usepackage{colortbl}

\begin{filecontents*}{bogus.csv}
FirstName,Surname,StudentNo,Score
John,"Smith, Jr",102689,68
Jane,Brown,102647,75
Andy,Brown,103569,42
Clare,Verdon,104356,45
John,"Smith, Jr",102683,68
Clare,Verdon,104356,45
\end{filecontents*}

\begin{document}

\begin{table}[htbp]
\caption{Conditionally colored rows}\label{tab:colored_row}
\centering
\begin{tabular}{llc}
\bfseries First Name &
\bfseries Surname &
\DTLforeach{dat}
{\firstname=FirstName, \surname=Surname,\score=Score}{
\DTLiffirstrow{}{
\DTLifnumlt{\score}{60}{\\\rowcolor{green}}{\\\rowcolor{blue}}
}
\firstname & \surname & \score}
\end{tabular}
\end{table}

\end{document}


It seems that using the \rowcolor command instead of \rowcolors did the trick (or almost did it).

The code at the bottom is compiling now, but I'm still getting the following error:

Misplaced \noalign.
\rowcolor ->\noalign
{\ifnum 0=}\fi \global \let \CT@do@color \CT@@do@color...


Bottomline, I'm not really happy with the solution but I got the output I need.

\documentclass{article}

\usepackage{datatool}
\usepackage{colortbl}

\begin{filecontents*}{bogus.csv}
FirstName,Surname,StudentNo,Score
John,"Smith, Jr",102689,68
Jane,Brown,102647,75
Andy,Brown,103569,42
Clare,Verdon,104356,45
John,"Smith, Jr",102683,68
Clare,Verdon,104356,45
\end{filecontents*}

\begin{document}

\begin{table}[htbp]
\caption{Conditionally colored rows}\label{tab:colored_row}
\centering
\begin{tabular}{llc}
\bfseries First Name &
\bfseries Surname &
`