我对 LaTex 还很陌生,所以请原谅我代码写得不太好。我试图从 csv 文件创建一个表格,并传入一个颜色列表,这样 LaTex 就会根据列表中的相应元素为表格中某一列的每个单元格设置不同的颜色。我无法让它工作。有什么建议吗?
\documentclass{article}
\usepackage{pgfplotstable,filecontents}
\RequirePackage{listofitems}
\begin{filecontents*}{test.csv}
ColA, ColB, ColC
0.17, 0.91, 0.67
0.15, 0.17, 0.92
0.48, 0.1, 0.28
\end{filecontents*}
\begin{document}
\pgfplotstabletypeset[col sep=comma,
]{test.csv}
\def\mylistOne{green, blue, orange}
\readlist*\mylistTwo{\mylistOne}
\newcounter{myCounter}
\setcounter{myCounter}{-1}
\foreachitem\x\in\mylistTwo[]{
\stepcounter{myCounter}
\pgfplotstableset{
every row \themyCounter column 1/.style={
postproc cell content/.append style={
/pgfplots/table/@cell content/.add={\cellcolor{\x}}{},
}
}
}
}
\end{document}
答案1
由于数字匹配,我创建了另一列,以便于访问颜色
\documentclass{article}
\usepackage{pgfplotstable,colortbl}
\pgfplotsset{compat=1.16}
\pgfplotstableread[col sep=comma]{
ColA, ColB, ColC
0.17, 0.91, 0.67
0.15, 0.17, 0.92
0.48, 0.1, 0.28
}\mytable
\def\mylistOne{green, blue, orange}
\pgfplotstablecreatecol[create col/set list/.expand once=\mylistOne]{colors}{\mytable}
\begin{document}
\pgfplotstabletypeset[
columns={ColA,ColB,ColC},
row setter/.style={
every row no #1/.style={
before row={%
\pgfplotstablegetelem{#1}{colors}\of\mytable%
\expandafter\rowcolor\expandafter{\pgfplotsretval}%
}
}
},
row setter/.list={0,1,2}
]\mytable
\end{document}
对于单列
\documentclass{article}
\usepackage{pgfplotstable,colortbl}
\pgfplotsset{compat=1.16}
\pgfplotstableread[col sep=comma]{
ColA, ColB, ColC
0.17, 0.91, 0.67
0.15, 0.17, 0.92
0.48, 0.1, 0.28
}\mytable
\def\mylistOne{green, blue, orange}
\pgfplotstablecreatecol[create col/set list/.expand once=\mylistOne]{colors}{\mytable}
\begin{document}
\pgfplotstabletypeset[
columns={ColA,ColB,ColC},
columns/ColB/.style={
postproc cell content/.code={%
\pgfplotstablegetelem{\pgfplotstablerow}{colors}\of\mytable%
\begingroup\edef\temp{\endgroup\noexpand\pgfkeyssetvalue{/pgfplots/table/@cell content}%
{\noexpand\cellcolor{\pgfplotsretval}##1}}\temp%
}
}
]\mytable
\end{document}