\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{collcell}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\def\colorModel{hsb}
\newcommand\ColCell[1]{
\pgfmathsetmacro\compA{(#1-90)/60} %H
\pgfmathsetmacro\compB{1} %S
\pgfmathsetmacro\compC{1} %B
\edef\x{\noexpand\centering\noexpand\cellcolor[\colorModel]{\compA,\compB,\compC}}\x #1
}
\newcolumntype{H}[1]{>{\collectcell\ColCell}p{#1}<{\endcollectcell}}
\begin{document}
\begin{tabular}{H{1cm} H{1cm}}
98.6 & 97.7 \\
90.2 & 99.3 \\
\end{tabular}
\end{document}
以下代码生成漂亮的热图:
有人知道是否可以用逗号而不是用点来显示数字吗?
98.6 不是 98.6
答案1
siunitx
可以使用如下方法完成:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{collcell,siunitx,pgfplots}
\pgfplotsset{compat=1.16}
\def\colorModel{hsb}
\newcommand\ColCell[1]{%
\pgfmathsetmacro\compA{(#1-90)/60}%%H
\pgfmathsetmacro\compB{1}%S
\pgfmathsetmacro\compC{1}%B
\edef\x{\noexpand\cellcolor[\colorModel]{\compA,\compB,\compC}}\x \num{#1}%
}
\newcolumntype{H}{>{\collectcell\ColCell}c<{\endcollectcell}}
\sisetup{output-decimal-marker={,}}
\begin{document}
\begin{tabular}{H H}
98.6 & 97.7 \\
90.2 & 99.3 \\
\end{tabular}
\end{document}
您应该注意\ColCell
宏不要引入额外的空间。这也适用于您的特定p
格式,如下所示
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{collcell,siunitx,pgfplots}
\pgfplotsset{compat=1.16}
\def\colorModel{hsb}
\newcommand\ColCell[1]{%
\pgfmathsetmacro\compA{(#1-90)/60}%%H
\pgfmathsetmacro\compB{1}%S
\pgfmathsetmacro\compC{1}%B
\edef\x{\noexpand\cellcolor[\colorModel]{\compA,\compB,\compC}}\centering\x
\num{#1}%
}
\newcolumntype{H}[1]{>{\collectcell\ColCell}p{#1}<{\endcollectcell}}
\sisetup{output-decimal-marker={,}}
\begin{document}
\begin{tabular}{H{1cm} H{1cm}}
98.6 & 97.7 \\
90.2 & 99.3 \\
\end{tabular}
\end{document}