0

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?

L.J.R.
  • 10,932
c_bfx
  • 48

2 Answers2

1

Here is a solution with csvsimple-l3 and tabularray packages:

\documentclass{report}

\usepackage[a4paper,left=1.9cm,right=1.9cm,top=2cm,bottom=2cm]{geometry}

\usepackage{csvsimple-l3} \usepackage{xcolor} \usepackage{tabularray}

\begin{filecontents}[overwrite]{\jobname A.csv} Sign High \end{filecontents}

\begin{filecontents}[overwrite]{\jobname B.csv} Sign Intermediate \end{filecontents}

\begin{filecontents}[overwrite]{\jobname C.csv} Sign Low \end{filecontents}

\begin{document}

\begin{center} \csvreader[ tabularray = { hlines, vlines, colspec = {Q[c,3.5cm]}, stretch=0, rowsep=5pt, row{1} = {font=\bfseries,bg=azure4,fg=white}, }, head to column names, table head = {SIGN\}, ]{\jobname A.csv}{}{ \ifcsvstrcmp{\csvcoli}{High}{\SetCell{red6}}{} \ifcsvstrcmp{\csvcoli}{Intermediate}{\SetCell{yellow6}}{} \ifcsvstrcmp{\csvcoli}{Low}{\SetCell{green6}}{} \csvexpval\csvcoli } \end{center}

\begin{center} \csvreader[ head to column names, tabularray = { hlines, vlines, colspec = {Q[c,3.5cm]}, stretch=0, rowsep=5pt, row{1} = {font=\bfseries,bg=azure4,fg=white}, }, table head = {SIGN\}, ]{\jobname B.csv}{}{ \ifcsvstrcmp{\csvcoli}{High}{\SetCell{red6}}{} \ifcsvstrcmp{\csvcoli}{Intermediate}{\SetCell{yellow6}}{} \ifcsvstrcmp{\csvcoli}{Low}{\SetCell{green6}}{} \csvexpval\csvcoli } \end{center}

\begin{center} \csvreader[ head to column names, tabularray = { hlines, vlines, colspec = {Q[c,3.5cm]}, stretch=0, rowsep=5pt, row{1} = {font=\bfseries,bg=azure4,fg=white}, }, table head = {SIGN\}, ]{\jobname C.csv}{}{ \ifcsvstrcmp{\csvcoli}{High}{\SetCell{red6}}{} \ifcsvstrcmp{\csvcoli}{Intermediate}{\SetCell{yellow6}}{} \ifcsvstrcmp{\csvcoli}{Low}{\SetCell{green6}}{} \csvexpval\csvcoli } \end{center}

\end{document}

enter image description here

L.J.R.
  • 10,932
  • Hi @L.J.R, when I tried to implement your solution, got the LaTeX Error: File csvsimple-l3.sty not found seems like I need to upgrade as suggested here, right now I have TeX 3.14159265 (TeX Live 2019/Debian) – c_bfx Jan 29 '22 at 09:18
  • Hi @L.J.R FYI, Your solution also worked for me when I upgrade TeX 3.14159265 (TeX Live 2019/Debian) to TeX 3.141592653 (TeX Live 2021) after following the steps suggested here. – c_bfx Jan 30 '22 at 06:51
1

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 &quot;!&quot;. 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 &quot;!High!Intermediate!Low!&quot;. % 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 &quot;!High!Intermediate!Low!&quot;. 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 &quot;!#2!Intermediate!Low!&quot;, % &quot;!High!#2!Low!&quot;, &quot;!High!Intermediate!#2!&quot;, % &quot;!High!Intermediate!Low!&quot; matches the delimiter you can % crank out which of the phrasesHigh,Intermediate% orLow` 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

Ulrich Diez
  • 28,770
  • Hi, @Ulrich Diez Great both solutions are working for me, need more reading to understand the second one though. – c_bfx Jan 29 '22 at 09:11
  • @c_bfx If you need clarification/explanation don't hesitate to ask, either via comment or via asking another question, probably - if the context is needed - mentioning that it is a follow-up-question to this answer. If you ask another question you probably reach more readers. – Ulrich Diez Jan 29 '22 at 09:41
  • Thanks for your suggestion, will keep it in mind. – c_bfx Jan 29 '22 at 10:14