我想根据值更改单元格颜色。我从另一篇文章中找到了代码
根据单元格中的数字自动更改单元格颜色
以下是有效的代码:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgf}
\usepackage{collcell}
\usepackage{booktabs}
% This selects the amount of color to use
\newcommand*{\MinNumber}{0}%
\newcommand*{\MaxNumber}{150}%
\newcommand{\ApplyGradient}[1]{%
\pgfmathsetmacro{\PercentColor}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}%
%\textcolor{black!\PercentColor}{#1}
\edef\x{\noexpand\cellcolor{green!\PercentColor}}\x\textcolor{black}{#1}%
}
\newcolumntype{R}{>{\collectcell\ApplyGradient}{r}<{\endcollectcell}}
\begin{document}
\begin{table}{
\begin{tabular}{ c R R R R R R R R}
\toprule
\multicolumn{1}{c} {Tools} &
\multicolumn{1}{c} {D1} &
\multicolumn{1}{c} {D2} &
\multicolumn{1}{c} {D3} &
\multicolumn{1}{c} {D4} &
\multicolumn{1}{c} {D5} &
\multicolumn{1}{c} {D6} &
\multicolumn{1}{c} {D7} &
\multicolumn{1}{c} {D8} \\
A & 150 & 80.81 & 54.5 & 100 & 51 & 8& 99 & 90\\
B & 66 & 99 & 73 & 0 & 66 & 59& 88 & 60\\
\toprule
\end{tabular}
}{}
\end{table}
\end{document}
我更改了数字,但当我使用更大的数字时,会出现错误。具体来说,我将 \MaxNumber 更改为 250,并将表格中的第一个数字更改为 200。表格中的数字 200 导致了问题。
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgf}
\usepackage{collcell}
\usepackage{booktabs}
% This selects the amount of color to use
\newcommand*{\MinNumber}{0}%
\newcommand*{\MaxNumber}{250}%
\newcommand{\ApplyGradient}[1]{%
\pgfmathsetmacro{\PercentColor}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}%
%\textcolor{black!\PercentColor}{#1}
\edef\x{\noexpand\cellcolor{green!\PercentColor}}\x\textcolor{black}{#1}%
}
\newcolumntype{R}{>{\collectcell\ApplyGradient}{r}<{\endcollectcell}}
\begin{document}
\begin{table}{
\begin{tabular}{ c R R R R R R R R}
\toprule
\multicolumn{1}{c} {Tools} &
\multicolumn{1}{c} {D1} &
\multicolumn{1}{c} {D2} &
\multicolumn{1}{c} {D3} &
\multicolumn{1}{c} {D4} &
\multicolumn{1}{c} {D5} &
\multicolumn{1}{c} {D6} &
\multicolumn{1}{c} {D7} &
\multicolumn{1}{c} {D8} \\
A & 200 & 80.81 & 54.5 & 100 & 51 & 8& 99 & 90\\
B & 66 & 99 & 73 & 0 & 66 & 59& 88 & 60\\
\toprule
\end{tabular}
}{}
\end{table}
\end{document}
错误信息是
ABD: EveryShipout initializing macros
! Dimension too large.
<recently read> \pgfmath@x
这是我的第一篇帖子。我不确定我是否应该对另一篇帖子发表评论或提出一个单独的问题。但由于我的“声誉”低于 50,所以我无论如何都不能发表评论。
感谢您的任何建议。