# How do I create row and column dependent color coding using csvsimple?

I have the following MWE:

\documentclass{article}

\usepackage{csvsimple}
\usepackage{filecontents}
\usepackage{longtable}
\usepackage[table]{xcolor}

\renewcommand{\rmdefault}{ptm}

\begin{filecontents*}{test.csv}
freq   ,R1,R2,R3,R4,R5,R6,R7,R8,R9,C
100\,Hz, 0, 1, 2, 3, 4, 5, 6, 7, 8,10\,nF
200\,Hz,10,11,12,13,14,15,16,17,18,10\,nF
300\,Hz,20,21,22,23,24,25,26,27,28,20\,nF
400\,Hz,30,31,32,33,34,35,36,37,38,20\,nF
500\,Hz,40,41,42,43,44,45,46,47,48,30\,nF
600\,Hz,50,51,52,53,54,55,56,57,58,30\,nF
\end{filecontents*}

\begin{document}
\definecolor{tch}{gray}{0.4}
\definecolor{tc1l}{gray}{0.90}
\definecolor{tc2l}{gray}{0.95}
\definecolor{tc1d}{gray}{0.80}
\definecolor{tc2d}{gray}{0.85}

\setlength\arrayrulewidth{1pt}
\setlength\extrarowheight{2.5pt}

longtable=>{\raggedleft}p{4.5em}!{\color{white}\vrule width 0.8pt}%
>{\raggedleft}p{2.0em}!{\color{white}\vrule width 0.8pt}%
>{\raggedleft}p{2.0em}!{\color{white}\vrule width 0.8pt}%
>{\raggedleft}p{2.0em}!{\color{white}\vrule width 0.8pt}%
>{\raggedleft}p{2.0em}!{\color{white}\vrule width 0.8pt}%
>{\raggedleft}p{2.0em}!{\color{white}\vrule width 0.8pt}%
>{\raggedleft}p{2.0em}!{\color{white}\vrule width 0.8pt}%
>{\raggedleft}p{2.0em}!{\color{white}\vrule width 0.8pt}%
>{\raggedleft}p{2.0em}!{\color{white}\vrule width 0.8pt}%
>{\raggedleft}p{2.0em}!{\color{white}\vrule width 0.8pt}%
>{\raggedleft\arraybackslash}%
p{3.0em}!{\color{white}\vrule width 0.8pt},
{\color{white}\bf Frequency} &
{\color{white}\bf R1} &
{\color{white}\bf R2} &
{\color{white}\bf R3} &
{\color{white}\bf R4} &
{\color{white}\bf R5} &
{\color{white}\bf R6} &
{\color{white}\bf R7} &
{\color{white}\bf R8} &
{\color{white}\bf R9} &
{\color{white}\bf Cap},
late after line=\csvifoddrow{\\\hline\rowcolor{tc1l}}
{\\\hline\rowcolor{tc2l}},
]
{test.csv}
{freq=\freq,R1=\ra,R2=\rb,R3=\rc,R4=\rd,R5=\re,R6=\rf,R7=\rg,R8=\rh,
R9=\ri,C=\c}
{\freq & \ra & \rb & \rc & \rd & \re & \rf & \rg & \rh & \ri & \c}

\end{document}


This produces the following result:

However, I need something like shown in the following picture. I don't know how to create it based on the cell's row and column number. How would I best go about this (hoping to be able to do this with csvsimple)?

• Use pgfplotstable. – TeXnician Aug 14 '18 at 16:46
• No way to continue to use csvsimple? I've just gotten familiar with csvsimple and now I should look into yet another package? – Hansel Aug 14 '18 at 16:47
• There might be (probably something with tikzmarks or something similar), but the simple in the name of csvsimple is not that wrong. You could maybe try to manually insert a kind of color/cellcolor macro at the start of each cell which only adjusts in the row and column you want it to, but it it probably is a very large effort. – TeXnician Aug 14 '18 at 16:54
• OK, I guess I'll look into pgfplotstable. – Hansel Aug 14 '18 at 17:27

I have edited this answer in light of TeXnician's comment. Previously I did not have an answer, just a suggestion. In order to show that the suggestion actually accomplishes the desired result, here is the code using the datatool package. Maybe something like this can be done with csvsimple, but as TeXnician rightly points out, the 'simple' in csvsimple makes me suspect that it cannot.

Note: in the code that follows, test.csv is the same as in the example above except that the cells with a \, in them are in quotation marks so that they can be read as part of a csv file.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{datatool}
\renewcommand\familydefault\sfdefault
\definecolor{lgray}{gray}{0.7}
\arrayrulecolor{white}
\setlength{\arrayrulewidth}{1pt}
\setlength\extrarowheight{2.5pt}

\begin{document}

\begin{tabular}{*{11}{|r}}
\rowcolor{gray}
\DTLforeach*{data}{}
{%
\gdef\rowint{20}%
\ifthenelse{\equal{\theDTLrowi}{1}}{\gdef\rowint{40}}{}
\ifthenelse{\equal{\theDTLrowi}{3}}{\gdef\rowint{70}}{}
\ifthenelse{\equal{\theDTLrowi}{5}}{\gdef\rowint{40}}{}
\DTLforeachkeyinrow{\v}{%
\ifthenelse{\equal{\dtlcol}{1}}{}{&}%
\cellcolor{lgray!\rowint}%
\ifthenelse{\equal{\dtlcol}{1}}{\cellcolor{blue!\rowint}}{}%
\ifthenelse{\equal{\dtlcol}{5}}{\cellcolor{gray!\rowint}}{}%
\ifthenelse{\equal{\dtlcol}{6}}{\cellcolor{gray!\rowint}}{}%
\ifthenelse{\equal{\dtlcol}{7}}{\cellcolor{gray!\rowint}}{}%
\ifthenelse{\equal{\dtlcol}{11}}{\cellcolor{green!\rowint}}{}%
\v}%
\\\hline
}
\end{tabular}

\end{document}


• Welcome to TeX.SX! This is a good idea, but has one drawback: The differing colors. You use cellcolor once, but this highlighted row has four different color intensities. – TeXnician Aug 15 '18 at 6:16
• Wow this is really cool. it works like a charm. It doesn't really matter to me how to do this, I don't need to use csvsimple. I just dreaded the coding so I highly appreciate the time you've taken to do so. – Hansel Aug 18 '18 at 6:13