我正在尝试扩展结果通过 TikZ 对表格中的阴影进行参数化
负值。我广泛搜索绝对值函数,但找不到。我尝试了 abs(),但它不起作用。对负值进行编辑后的代码:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\usepackage{calc}
\pgfplotstableset{
color cells/.style={
col sep=comma,
string type,
postproc cell content/.code={%
\pgfkeysalso{@cell content=\rule{0cm}{2.4ex}\ifnum ##1 < 0 {\cellcolor{red!abs(##1)} \pgfmathtruncatemacro\number{##1}\ifnum\number>50\color{white}\fi##1}\else\
{\cellcolor{green!##1}\pgfmathtruncatemacro\number{##1}\ifnum\number>50\color{white}\fi##1}\fi
}%
},
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
您正在测试数字 ( ##1
) 是否为负数。 如果为负数,则使用 将其转换为正数-##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}%
\pgfmathtruncatemacro\number{##1}%
\ifnum\number<0
\cellcolor{red!-##1}##1
\else
\cellcolor{green!##1}##1
\fi
}},
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}