2

I would like to extend the following example so that cells with YES in are colored green and cells that have NO in are colored red:

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

Thanks

1 Answer 1

3

Figured it out:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .