Define table cell color depending on cell text value imported from the CSV file

cellcolorcolortblconditionalscsvsimpletables

I am trying to color table cell depending on the text value imported from the CSV file. The CSV file has three possible text values that are "High", "Intermediate" and "Low". I am trying to color table cell Red if "High" is in cell or yellow if "Intermediate" is in cell or green if "Low" is in a cell.

Test1.csv:

Sign
High

My MWE is below:

\documentclass{report}

\usepackage[a4paper,left=1.9cm,right=1.9cm,top=2cm,bottom=2cm]{geometry}%set margin of page
\usepackage{csvsimple,booktabs,array,filecontents,siunitx}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{color, colortbl}%enable color use in table cell

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}%this will align cell content of table to center via using 'P'


\begin{document}

\begin{center}
{\renewcommand{\arraystretch}{1.5}
\begin{tabular}{| P{3.5cm} | } \rowcolor{cyan!60!black}
\hline
    \setlength\tabcolsep{2mm}
    \bfseries \rule{0pt}{1pt} \color{white}SIGN% specify table head and its font color
    \csvreader[head to column names, late after last line =\\\hline]{./Test1.csv}{}% use head of csv as column names
    {\\\hline\csvcoli}% specify your coloumns here
\end{tabular}
}
\end{center}
\end{document}

enter image description here

Here I want the cell color to be red. Similarly, if the cell text value is "Intermediate" the cell color should be yellow, and so on.

This is a similar question Tables: Cell Color based on content / conditional cell coloring at some extent and tried to implement it but did not succeed.

Is there any other way I can achieve this?

Best Answer

Probably \colorlet is your friend. ;-)

\begin{filecontents*}{Test1.csv}
Sign
High
Intermediate
Low
\end{filecontents*}

\documentclass{report}

\usepackage[a4paper,left=1.9cm,right=1.9cm,top=2cm,bottom=2cm]{geometry}%set margin of page
\usepackage{csvsimple,booktabs,array,filecontents,siunitx}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{color, colortbl}%enable color use in table cell

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}%this will align cell content of table to center via using 'P'

\colorlet{High}{red}
\colorlet{Intermediate}{yellow}
\colorlet{Low}{green}

\begin{document}

\begin{center}
{\renewcommand{\arraystretch}{1.5}
\begin{tabular}{| P{3.5cm} | } \rowcolor{cyan!60!black}
\hline
    \setlength\tabcolsep{2mm}
    \bfseries \rule{0pt}{1pt} \color{white}SIGN% specify table head and its font color
    \csvreader[head to column names, late after last line =\\\hline]{./Test1.csv}{}% use head of csv as column names
    {\\\hline\expandafter\cellcolor\expandafter{\romannumeral`\^^@\csvcoli}\csvcoli}% specify your coloumns here
\end{tabular}
}
\end{center}
\end{document}

enter image description here

If you need more sophisticated mapping from phrase to color, you can do with delimited arguments:

\begin{filecontents*}{Test1.csv}
Sign
High
Intermediate
Low
\end{filecontents*}

\makeatletter
\@ifdefinable\@gobbletoexclam{\long\def\@gobbletoexclam#1!{}}%
\newcommand\exchange[2]{#2#1}%
%
% \mycolormapping{<Tokens>}{<s.th. that expands to one of the phrases High/Intermediate/Low>}
% -> <Tokens>{red}
% or <Tokens>{yellow}
% or <Tokens>{green}
% or <nothing>
%
\newcommand\mycolormapping[2]{%
  % Expand #2 until encountering a non-expandable token.
  % (If that non-expandable token is a space-token it will be discarded.)
  % Then call the mapping-routine.
  \expandafter\exchange\expandafter{\expandafter{\romannumeral`\^^@#2}}{\@mycolormapping{#1}}%
}%
\newcommand\@mycolormapping[2]{%
  % Check whether #2 does contain "!". If so it is not one of the phrases
  % High/Intermediate/Low but it could be a phrase which erroneously
  % matches up delimiters due to containing (parts of) the delimiter "!High!Intermediate!Low!".
  % If not so, apply the macro \mycolormappingfork which via delimited arguments
  % cranks out the right cases.
  \ifcat$\detokenize\expandafter{\@gobbletoexclam#2!}$%
  \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {%
    \mycolormappingfork!#2!Intermediate!Low!{#1{red}}%
                       !High!#2!Low!{#1{yellow}}%
                       !High!Intermediate!#2!{#1{green}}%
                       !High!Intermediate!Low!{}%
                       !!!!%
  }{}%
}%
\@ifdefinable\mycolormappingfork{%
  % Let \mycolormappingfork grab the argument #2 behind a 
  % delimiter "!High!Intermediate!Low!". Stuff before that
  % delimiter is removed as unused #1. Stuff behind the
  % second argument is removed as unused `!!!!`-delimited
  % argument #3. The delimiter in turn is completed by
  % \@mycolormapping's second argument. Depending on which
  % of the sequences "!#2!Intermediate!Low!", 
  % "!High!#2!Low!", "!High!Intermediate!#2!",
  % "!High!Intermediate!Low!" matches the delimiter you can
  % crank out which of the phrases `High`, `Intermediate`
  % or `Low` was provided via \@mycolormapping's #2. The
  % last one, "!High!Intermediate!Low!", is for the case
  % that \@mycolormapping's  #2 formed neither of these
  % phrases.
  \long\def\mycolormappingfork#1!High!Intermediate!Low!#2#3!!!!{#2}%
}%
\makeatother

\documentclass{report}

\usepackage[a4paper,left=1.9cm,right=1.9cm,top=2cm,bottom=2cm]{geometry}%set margin of page
\usepackage{csvsimple,booktabs,array,filecontents,siunitx}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{color, colortbl}%enable color use in table cell

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}%this will align cell content of table to center via using 'P'

\colorlet{High}{red}
\colorlet{Intermediate}{yellow}
\colorlet{Low}{green}

\begin{document}

\begin{center}
{\renewcommand{\arraystretch}{1.5}
\begin{tabular}{| P{3.5cm} | } \rowcolor{cyan!60!black}
\hline
    \setlength\tabcolsep{2mm}
    \bfseries \rule{0pt}{1pt} \color{white}SIGN% specify table head and its font color
    \csvreader[head to column names, late after last line =\\\hline]{./Test1.csv}{}% use head of csv as column names
    {\\\hline\mycolormapping{\cellcolor}{\csvcoli}\csvcoli}% specify your coloumns here
\end{tabular}
}
\end{center}
\end{document}

enter image description here

Related Question