如何使用数据工具有条件地为表格行添加阴影/颜色?

如何使用数据工具有条件地为表格行添加阴影/颜色?

我一直尝试根据表中特定条目的值有条件地对整行进行着色。

我试图改编 中的“条纹表”示例datatool,但似乎我弄乱了新的条件语句。在下面的示例中,我保留了原始stripy table条件,但将其注释掉。

任何帮助将不胜感激。

\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

这里有一个解决方案:

\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}

在此处输入图片描述

答案2

看来使用\rowcolor命令而不是\rowcolors达到了目的(或者几乎达到了目的)。

底部的代码正在编译,但我仍然收到以下错误:

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

总而言之,我对这个解决方案并不是很满意,但我得到了我需要的输出。

\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}

相关内容