![如何使用数据工具有条件地为表格行添加阴影/颜色?](https://linux22.com/image/342895/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E6%95%B0%E6%8D%AE%E5%B7%A5%E5%85%B7%E6%9C%89%E6%9D%A1%E4%BB%B6%E5%9C%B0%E4%B8%BA%E8%A1%A8%E6%A0%BC%E8%A1%8C%E6%B7%BB%E5%8A%A0%E9%98%B4%E5%BD%B1%2F%E9%A2%9C%E8%89%B2%EF%BC%9F.png)
我一直尝试根据表中特定条目的值有条件地对整行进行着色。
我试图改编 中的“条纹表”示例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}