pgfplotstable:按字符串值进行条件格式化

pgfplotstable:按字符串值进行条件格式化

我想扩展以下示例,以便输入 YES 的单元格显示为绿色,输入 NO 的单元格显示为红色:

\documentclass{article}
\usepackage{pgfplotstable}

\begin{filecontents}{data.csv}
features,model1,model2,model3 
feature1,YES,NO,NO
feature2,NO,YES,YES
\end{filecontents}

\begin{document}

\pgfplotstabletypeset[col sep=comma,string type]{data.csv}

\end{document}

谢谢

答案1

弄清楚了:

\documentclass{article}
\usepackage{pgfplotstable, colortbl}

\begin{filecontents}{data.csv}
features,model1,model2,model3 
feature1,YES,NO,1
feature2,NO,YES,2
\end{filecontents}

\begin{document}

% apply conditional formatting to whole table
% \unexpanded{#1} stops errors if cell contains math, etc
\pgfplotstabletypeset[
col sep=comma,
string type,
postproc cell content/.style={        
   @cell content/.add={
   \ifnum\pdfstrcmp{\unexpanded{#1}}{YES}=0
    \cellcolor{green!40!white}      
   \fi      
   \ifnum\pdfstrcmp{\unexpanded{#1}}{NO}=0
    \cellcolor{red!40!white}
   \fi
   }{}}
]{data.csv}

% apply conditional formatting for each column
\pgfplotstabletypeset[
col sep=comma,
string type,
% format backgroud color
columns/model1/.style={column name={a model1},
        postproc cell content/.style={        
               @cell content/.add={
               \ifnum\pdfstrcmp{##1}{YES}=0
                \cellcolor{green!30!white}      
               \fi      
               \ifnum\pdfstrcmp{##1}{NO}=0
                \cellcolor{red!30!white}
               \fi
               }{}
        }   
    },
% format string color
columns/model2/.style={column name={a model2},
        postproc cell content/.style={        
               @cell content={
               \ifnum\pdfstrcmp{##1}{YES}=0
                \color{red} \textit{##1}                
               \else
                \color{green} \textbf{##1}
               \fi
               }
        }   
    },
% format numbers
columns/model3/.style={column name={a model3},numeric type,
        postproc cell content/.style={        
               @cell content={
               \pgfmathparse{##1<2} 
               \ifnum\pgfmathresult=1
                \color{red} \textit{##1}                
               \else
                \textbf{##1}
               \fi
               }
        }   
    },
]{data.csv}

\end{document}

相关内容