我想根据表格中的每个单元格的值来为其着色。我知道有解决方案,但您必须为表格全局设置最小值和最大值。例如,可以在是否有一种简单的方法可以根据每个单元格中的值为表格着色?和使用 cellcolor 自动对单元格进行着色:未定义的控制序列。
我试图想出一个解决方案,使我能够设置每列的最小值和最大值,以便能够用各自的最小值和最大值为每个单元格着色。
\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{tabularx}
\usepackage{collcell}
\usepackage{xfp}
\definecolor{goodgreen}{HTML}{c5eecc}
\definecolor{goodred}{HTML}{ffc7ce}
\newcommand{\MinNumber}{0}%
\newcommand{\MaxNumber}{1}%
\newcommand{\ApplyGradient}[1]{%
\cellcolor{goodgreen!\fpeval{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}!goodred}{#1}
}
\newcolumntype{H}[2]{>{\SetGradientLimits{#1}{#2}\collectcell\ApplyGradient}X<{\endcollectcell}}
\begin{document}
\begin{table}
\newcommand*{\SetGradientLimits}[2]{%
\renewcommand*{\MinNumber}{#1}%
\renewcommand*{\MaxNumber}{#2}%
}
\begin{tabularx}{\textwidth}{l|l|H{0}{1}|l|H{0}{100000}}
\hline
Name & Value 1 & \multicolumn{1}{c}{Value 2} & Value 3 & \multicolumn{1}{c}{Value 4} \\
\hline
a & \colorbox{goodgreen!20!goodred}{0.2} & 0.2 & \colorbox{goodgreen!20!goodred}{20000} & 20000\\
b & \colorbox{goodgreen!50!goodred}{0.5} & 0.5 & \colorbox{goodgreen!50!goodred}{50000} & 50000\\
c & \colorbox{goodgreen!60!goodred}{0.6} & 0.6 & \colorbox{goodgreen!60!goodred}{60000} & 60000\\
d & \colorbox{goodgreen!80!goodred}{0.8} & 0.8 & \colorbox{goodgreen!80!goodred}{80000} & 80000\\
\end{tabularx}
\end{table}
\end{document}
不幸的是,这不能正常工作,因为它似乎在设置\MinNumber
和的值之前以某种方式计算了混合百分比\MaxNumber
。错误消息表明计算是在之前完成的:
Package color Error: Argument `-26.265' not in range [0,1].
Package color Error: Argument `25.83789' not in range [0,1].
Package color Error: Argument `-0.47716' not in range [0,1].
如果我在宏\cellcolor
中用 替换命令,它就会起作用。但我希望整个单元格都变成彩色,就像命令一样。\colorbox
\ApplyGradient
\cellcolor
\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{tabularx}
\usepackage{collcell}
\usepackage{xfp}
\definecolor{goodgreen}{HTML}{c5eecc}
\definecolor{goodred}{HTML}{ffc7ce}
\newcommand{\MinNumber}{0}%
\newcommand{\MaxNumber}{0}%
\newcommand{\ApplyGradient}[1]{%
\colorbox{goodgreen!\fpeval{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}!goodred}{#1}
}
\newcolumntype{H}[2]{>{\SetGradientLimits{#1}{#2}\collectcell\ApplyGradient}X<{\endcollectcell}}
\begin{document}
\begin{table}
\newcommand*{\SetGradientLimits}[2]{%
\renewcommand*{\MinNumber}{#1}%
\renewcommand*{\MaxNumber}{#2}%
}
\begin{tabularx}{\textwidth}{l|l|H{0}{1}|l|H{0}{100000}}
\hline
Name & Value 1 & \multicolumn{1}{c}{Value 2} & Value 3 & \multicolumn{1}{c}{Value 4} \\
\hline
a & \colorbox{goodgreen!20!goodred}{0.2} & 0.2 & \colorbox{goodgreen!20!goodred}{20000} & 20000\\
b & \colorbox{goodgreen!50!goodred}{0.5} & 0.5 & \colorbox{goodgreen!50!goodred}{50000} & 50000\\
c & \colorbox{goodgreen!60!goodred}{0.6} & 0.6 & \colorbox{goodgreen!60!goodred}{60000} & 60000\\
d & \colorbox{goodgreen!80!goodred}{0.8} & 0.8 & \colorbox{goodgreen!80!goodred}{80000} & 80000\\
\end{tabularx}
\end{table}
\end{document}
那可能吗?
更新
我特意选择使用xfp
包进行计算,因为我想绕过Dimensions too large
在 中进行计算的限制pgfplots
。
答案1
Dimensions too large
绕过限制pgfplots
更新 3-带xfp
包
-->数据范围:± 10^10000
最初的问题是关于使用xfp
包的解决方案,因此我一直在寻找方法,直到找到它。因此,我将宏重新定义\Percent
为:
\newcommand\Percent{\fpeval{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}}
在\ApplyGradient
一个:
\newcommand{\ApplyGradient}[1]{%
\newcommand\Percent{\fpeval{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}}
\pgfmathsetmacro{\PercentColor}{\Percent}
\xdef\PercentColor{\PercentColor}%
\cellcolor{goodgreen!\PercentColor!goodred}{#1}
}
就是这样。无论如何,我想我以前也尝试过这样简单的解决方案,而且方法相同,但我没有成功……
输出(与其他方法相同):
代码:
\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{tabularx}
\usepackage{collcell}
\usepackage{xfp}
\definecolor{goodgreen}{HTML}{c5eecc}
\definecolor{goodred}{HTML}{ffc7ce}
\newcommand{\MinNumber}{0}%
\newcommand{\MaxNumber}{0}%
\newcommand{\ApplyGradient}[1]{%
\newcommand\Percent{\fpeval{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}}
\pgfmathsetmacro{\PercentColor}{\Percent}
\xdef\PercentColor{\PercentColor}%
\cellcolor{goodgreen!\PercentColor!goodred}{#1}
}
\newcolumntype{H}[2]{>{\SetGradientLimits{#1}{#2}\collectcell\ApplyGradient}X<{\endcollectcell}}
\begin{document}
\begin{table}
\newcommand*{\SetGradientLimits}[2]{%
\renewcommand*{\MinNumber}{#1}%
\renewcommand*{\MaxNumber}{#2}%
}
\begin{tabularx}{\textwidth}{l|l|H{0}{1}|l|H{0}{100000}}
\hline
Name & Value 1 & \multicolumn{1}{c}{Value 2} & Value 3 & \multicolumn{1}{c}{Value 4} \\
\hline
a & \cellcolor{goodgreen!20!goodred}0.2 & 0.2 & \cellcolor{goodgreen!20!goodred}20000 & 20000\\
b & \cellcolor{goodgreen!50!goodred}0.5 & 0.5 & \cellcolor{goodgreen!50!goodred}50000 & 50000\\
c & \cellcolor{goodgreen!60!goodred}0.6 & 0.6 & \cellcolor{goodgreen!60!goodred}60000 & 60000\\
d & \cellcolor{goodgreen!80!goodred}0.8 & 0.8 & \cellcolor{goodgreen!80!goodred}80000 & 80000\\
\end{tabularx}
\end{table}
\end{document}
更新 2-带有fpu
库(pfg/tikz
)
-->数据范围:± 10^324 (至少是 IEEE 双精度数据范围)
施塔达德的评论:
pgf
还有一个名为的库fpu
,可让您使用最多为的数字10^{324}
。(请参阅手册第 56 节。)
给了我克服限制的线索(数字最多±9.99910^{17}
) 的fp
包(感谢您的提示)。出于这个原因,我尝试fpu
tikzlibrary
在我的代码中实现。因此,我添加了:
\usetikzlibrary{fpu}
\pgfkeys{/pgf/fpu=true}
\pgfkeys{/pgf/fpu/output format=fixed}
回到序言。此外,我\Applygradient
再次重新定义了宏:
\newcommand{\ApplyGradient}[1]{%
\pgfmathparse{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}
\xdef\Percent{\pgfmathresult}
\pgfmathsetmacro{\PercentColor}{\Percent}
\xdef\PercentColor{\PercentColor}%
\cellcolor{goodgreen!\PercentColor!goodred}{#1}
}
因此我获得了相同的期望输出:
代码:
\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{tabularx}
\usepackage{collcell}
\usetikzlibrary{fpu}
\pgfkeys{/pgf/fpu=true}
\pgfkeys{/pgf/fpu/output format=fixed}
\definecolor{goodgreen}{HTML}{c5eecc}
\definecolor{goodred}{HTML}{ffc7ce}
\newcommand{\MinNumber}{0}%
\newcommand{\MaxNumber}{0}%
\newcommand{\ApplyGradient}[1]{%
\pgfmathparse{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}
\xdef\Percent{\pgfmathresult}
\pgfmathsetmacro{\PercentColor}{\Percent}
\xdef\PercentColor{\PercentColor}%
\cellcolor{goodgreen!\PercentColor!goodred}{#1}
}
\newcolumntype{H}[2]{>{\SetGradientLimits{#1}{#2}\collectcell\ApplyGradient}X<{\endcollectcell}}
\begin{document}
\begin{table}
\newcommand*{\SetGradientLimits}[2]{%
\renewcommand*{\MinNumber}{#1}%
\renewcommand*{\MaxNumber}{#2}%
}
\begin{tabularx}{\textwidth}{l|l|H{0}{1}|l|H{0}{100000}}
\hline
Name & Value 1 & \multicolumn{1}{c}{Value 2} & Value 3 & \multicolumn{1}{c}{Value 4} \\
\hline
a & \cellcolor{goodgreen!20!goodred}0.2 & 0.2 & \cellcolor{goodgreen!20!goodred}20000 & 20000\\
b & \cellcolor{goodgreen!50!goodred}0.5 & 0.5 & \cellcolor{goodgreen!50!goodred}50000 & 50000\\
c & \cellcolor{goodgreen!60!goodred}0.6 & 0.6 & \cellcolor{goodgreen!60!goodred}60000 & 60000\\
d & \cellcolor{goodgreen!80!goodred}0.8 & 0.8 & \cellcolor{goodgreen!80!goodred}80000 & 80000\\
\end{tabularx}
\end{table}
\end{document}
更新 1-带fp
包
-->数据范围:± 9.99910^17
我找到了一种使用包的\cellcolor
宏而不是使用包的宏的方法(我希望这对你来说不是问题)。因此,我将解决方案中的宏重新定义为\FPeval
fp
\colorbox
\fpeval
xfp
\Applygradient
使用 cellcolor 自动对单元格进行着色:未定义的控制序列经过沃纳:
\newcommand{\ApplyGradient}[1]{%
\FPeval{\Percent}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}
\pgfmathsetmacro{\PercentColor}{\Percent}
\xdef\PercentColor{\PercentColor}%
\cellcolor{goodgreen!\PercentColor!goodred}{#1}
}
这可以正确编译并可以获得所需的输出:
代码:
\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{tabularx}
\usepackage{collcell}
\usepackage{fp}
\definecolor{goodgreen}{HTML}{c5eecc}
\definecolor{goodred}{HTML}{ffc7ce}
\newcommand{\MinNumber}{0}%
\newcommand{\MaxNumber}{0}%
\newcommand{\ApplyGradient}[1]{%
\FPeval{\Percent}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}
\pgfmathsetmacro{\PercentColor}{\Percent}
\xdef\PercentColor{\PercentColor}%
\cellcolor{goodgreen!\PercentColor!goodred}{#1}
}
\newcolumntype{H}[2]{>{\SetGradientLimits{#1}{#2}\collectcell\ApplyGradient}X<{\endcollectcell}}
\begin{document}
\begin{table}
\newcommand*{\SetGradientLimits}[2]{%
\renewcommand*{\MinNumber}{#1}%
\renewcommand*{\MaxNumber}{#2}%
}
\begin{tabularx}{\textwidth}{l|l|H{0}{1}|l|H{0}{100000}}
\hline
Name & Value 1 & \multicolumn{1}{c}{Value 2} & Value 3 & \multicolumn{1}{c}{Value 4} \\
\hline
a & \cellcolor{goodgreen!20!goodred}0.2 & 0.2 & \cellcolor{goodgreen!20!goodred}20000 & 20000\\
b & \cellcolor{goodgreen!50!goodred}0.5 & 0.5 & \cellcolor{goodgreen!50!goodred}50000 & 50000\\
c & \cellcolor{goodgreen!60!goodred}0.6 & 0.6 & \cellcolor{goodgreen!60!goodred}60000 & 60000\\
d & \cellcolor{goodgreen!80!goodred}0.8 & 0.8 & \cellcolor{goodgreen!80!goodred}80000 & 80000\\
\end{tabularx}
\end{table}
\end{document}
局限性
该包的文档中fp
指出:
用于 TEX 的定点运算,数字范围从 −9999999999999999999.999999999999999999 到 +9999999999999999999.999999999999999999
由于Dimensions too large
pgfplots
-->数据范围:±16384
只需将 替换\colorbox
到您的工作解决方案\cellcolor
中,并从解决方案中tabularx
获取 的原始定义即可\Applygradient
使用 cellcolor 自动对单元格进行着色:未定义的控制序列经过沃纳:
\newcommand{\ApplyGradient}[1]{%
\pgfmathsetmacro{\PercentColor}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}
\xdef\PercentColor{\PercentColor}%
\cellcolor{goodgreen!\PercentColor!goodred}{#1}
}
你获得了你想要的输出:
代码:
\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{tabularx}
\usepackage{collcell}
\usepackage{xfp}
\definecolor{goodgreen}{HTML}{c5eecc}
\definecolor{goodred}{HTML}{ffc7ce}
\newcommand{\MinNumber}{0}%
\newcommand{\MaxNumber}{0}%
\newcommand{\ApplyGradient}[1]{%
\pgfmathsetmacro{\PercentColor}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}
\xdef\PercentColor{\PercentColor}%
\cellcolor{goodgreen!\PercentColor!goodred}{#1}
}
\newcolumntype{H}[2]{>{\SetGradientLimits{#1}{#2}\collectcell\ApplyGradient}X<{\endcollectcell}}
\begin{document}
\begin{table}
\newcommand*{\SetGradientLimits}[2]{%
\renewcommand*{\MinNumber}{#1}%
\renewcommand*{\MaxNumber}{#2}%
}
\begin{tabularx}{\textwidth}{l|l|H{0}{1}|l|H{0}{100}}
\hline
Name & Value 1 & \multicolumn{1}{c}{Value 2} & Value 3 & \multicolumn{1}{c}{Value 4} \\
\hline
a & \cellcolor{goodgreen!20!goodred}0.2 & 0.2 & \cellcolor{goodgreen!20!goodred}20 & 20\\
b & \cellcolor{goodgreen!50!goodred}0.5 & 0.5 & \cellcolor{goodgreen!50!goodred}50 & 50\\
c & \cellcolor{goodgreen!60!goodred}0.6 & 0.6 & \cellcolor{goodgreen!60!goodred}60 & 60\\
d & \cellcolor{goodgreen!80!goodred}0.8 & 0.8 & \cellcolor{goodgreen!80!goodred}80 & 80\\
\end{tabularx}
\end{table}