如何使用 csvsimple 创建行和列相关的颜色编码?

如何使用 csvsimple 创建行和列相关的颜色编码?

我有以下 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}

\csvreader[
  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},
  table head=\arrayrulecolor{white}\hline\rowcolor{tch}
    {\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 head=\\\endhead\rowcolor{tc1l},
    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}

其结果如下:

结果不错,但很模糊

但是,我需要如下图所示的内容。我不知道如何根据单元格的行号和列号来创建它。我该如何最好地做到这一点(希望能够使用 csvsimple 来实现)?

在此处输入图片描述

答案1

我根据 TeXnician 的评论修改了这个答案。之前我没有答案,只有一个建议。为了表明建议确实实现了预期结果,下面是使用该datatool包的代码。也许可以用 来完成这样的事情csvsimple,但正如 TeXnician 正确指出的那样, 中的“简单”csvsimple让我怀疑它不能。

注意:在下面的代码中,test.csv与上面的示例相同,只是\,其中带有的单元格放在引号中,以便可以将它们作为csv文件的一部分读取。

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

\DTLloaddb{data}{test.csv}

\begin{tabular}{*{11}{|r}}
\rowcolor{gray}
\dtlforeachkey(\theKey,\theCol,\theType,\theHead)\in{data}\do
 {\ifthenelse{\equal{\theCol}{1}}{}{&}{\color{white}\bf\theHead}} \\\hline%
\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}

输出

相关内容