1

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
Z\"oe,Adams,105987,52
Clare,Verdon,104356,45
John,"Smith, Jr",102683,68
Roger,Brady,106872,58
Clare,Verdon,104356,45
\end{filecontents*}

\begin{document}

\DTLloaddb{dat}{bogus.csv}
\begin{table}[htbp]
\caption{Conditionally colored rows}\label{tab:colored_row}
\centering
\begin{tabular}{llc}
\bfseries First Name &
\bfseries Surname &
\bfseries Grade (\%)%
\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}
1

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
Z\"oe,Adams,105987,52
Clare,Verdon,104356,45
John,"Smith, Jr",102683,68
Roger,Brady,106872,58
Clare,Verdon,104356,45
\end{filecontents*}

\begin{document}

\DTLloaddb{dat}{bogus.csv}
\begin{table}[htbp]
\caption{Conditionally colored rows}\label{tab:colored_row}
\centering
\begin{tabular}{llc}
\bfseries First Name &
\bfseries Surname &
\bfseries Grade (\%)\\
  \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}

enter image description here

0

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
Z\"oe,Adams,105987,52
Clare,Verdon,104356,45
John,"Smith, Jr",102683,68
Roger,Brady,106872,58
Clare,Verdon,104356,45
\end{filecontents*}

\begin{document}


\DTLloaddb{dat}{bogus.csv}
\begin{table}[htbp]
\caption{Conditionally colored rows}\label{tab:colored_row}
\centering
\begin{tabular}{llc}
\bfseries First Name &
\bfseries Surname &
\bfseries Grade (\%)%
\DTLforeach{dat}
           {\firstname=FirstName, \surname=Surname,\score=Score} %
           {\\
           \DTLifnumlt{\score}{50}{\rowcolor{red}}{}
           \firstname & \surname & \score
           }% 
\end{tabular}
\end{table}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.