我在回答另一个类似问题时看到了以下代码,该代码在表格中的数字介于 0 到 100 之间时有效。我该如何修改此代码,使其适用于 0 到 1 之间的数字(或任意范围?)。我的想法是,我需要修改以下行:
\cellcolor{black!##1}
这样它就会以某种方式将其乘以 100,但我不知道如何做到这一点。
这是适用于给定数字(0 到 100 之间)的完整代码 - 我想对 0 到 1 之间的数字执行相同的操作。谢谢
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\pgfplotstableset{
color cells/.style={
col sep=comma,
string type,
postproc cell content/.code={%
\pgfkeysalso{@cell content=\rule{0cm}{2.4ex}\cellcolor{black!##1}\pgfmathtruncatemacro\number{##1}\ifnum\number>50\color{white}\fi##1}%
},
columns/x/.style={
column name={},
postproc cell content/.code={}
}
}
}
\begin{document}
\begin{table}\caption{Correlation or something}
\centering
\pgfplotstabletypeset[color cells]{
x,a,b,c,d
a,90,10.5,0,0
b,0,80,10,10
c,0,0,95,5
d,0,10,5,85
}
\end{table}
\end{document}
答案1
我对 Christian 的代码做了一些修改(基本上是视觉上的破坏,并转换为样式),以限制定义的宏的有效范围。否则,string type
列也会获取单元格内容设置指令。这允许有选择地启用单元格着色。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable,colortbl}
\pgfplotsset{compat=1.12}
\pgfplotstableset{
/color cells/min/.initial=0,
/color cells/max/.initial=1000,
/color cells/textcolor/.initial=,
color cells/.style={
postproc cell content/.append code={%
\pgfkeys{/color cells/.cd,#1}%
\pgfkeysgetvalue{/pgfplots/table/@preprocessed cell content}\value%
\ifx\value\empty%
\else%
\pgfmathfloatparsenumber{\value}\pgfmathfloattofixed{\pgfmathresult}%
\let\value=\pgfmathresult%
\pgfplotscolormapaccess%
[\pgfkeysvalueof{/color cells/min}:\pgfkeysvalueof{/color cells/max}]%
{\value}{\pgfkeysvalueof{/pgfplots/colormap name}}%
\pgfkeysgetvalue{/pgfplots/table/@cell content}\typesetvalue%
\pgfkeysgetvalue{/color cells/textcolor}\textcolorvalue%
\toks0=\expandafter{\typesetvalue}%
\edef\temp{\noexpand\pgfkeyssetvalue{/pgfplots/table/@cell content}{%
\noexpand\cellcolor[rgb]{\pgfmathresult}%
\noexpand\definecolor{mapped color}{rgb}{\pgfmathresult}%
\ifx\textcolorvalue\empty\else\noexpand\color{\textcolorvalue}\fi%
\the\toks0%
}%
}%
\temp%
\fi%
}%
}%
}%
\begin{document}
\pgfplotstabletypeset[col sep=comma,
/color cells/max=100,
/color cells/min=0,
/color cells/textcolor=white,
columns/a/.style={color cells},
columns/b/.style={color cells},
columns/c/.style={color cells},
columns/d/.style={color cells},
columns/x/.style={string type},
/pgfplots/colormap/blackwhite,
]{
x,a,b,c,d
a,90,10.5,0,0
b,0,80,10,10
c,0,0,95,5
d,0,10,5,85
}
\end{document}