![使用 collcell 生成热图,但里面的数字用逗号分隔](https://linux22.com/image/396722/%E4%BD%BF%E7%94%A8%20collcell%20%E7%94%9F%E6%88%90%E7%83%AD%E5%9B%BE%EF%BC%8C%E4%BD%86%E9%87%8C%E9%9D%A2%E7%9A%84%E6%95%B0%E5%AD%97%E7%94%A8%E9%80%97%E5%8F%B7%E5%88%86%E9%9A%94.png)
\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}