計算と表形式環境を使用してヒートマップを作成する

計算と表形式環境を使用してヒートマップを作成する

pgf が存在することは知っています。多くの人がこれを使用してヒートマップを作成しています。ただし、このパッケージを使用することはできません。そのため、さまざまなセルに色を付けることができるコマンドを作成したいと考えています。

私はこれを見つけました質問、次のことを示したdef

\def\cca#1{\cellcolor{black!#10}\ifnum #1>5\color{white}\fi{#1}}

しかし、彼のコメントと私のテストによると、0 から 9 までしか機能しません。

私は、次のような疑似コードのように、より汎用的なものを作成したいと考えました。

\newcommand{\cTab}[2]
{
    \res = #1/#2 %maybe using FP (?)
    \eighty = 0.8*#2 %maybe using FP (?)

    \cellcolor{black!\res} 
    \ifnum #1>\eighty 
       \color{white}
    \fi{#1}
}

つまり、基本的に問題は数値を計算し、それを再利用して色などを定義することですか?

FP を使用して数値を計算することはできますが、cellcolorまたは内で再利用することはできませんifnum

ムウェ

\documentclass[letterpaper, 10 pt]{article}

% Color
\usepackage{xcolor,colortbl}%

\begin{document}
\begin{table}[!ht]
\centering
\caption{True data classification.}
\label{tab:CollectedErrors}
\begin{tabular}{|p{1.5cm}|p{0.5cm}|}
\hline
\textbf{Exposure}& \textbf{Attribute} \\ \hline

  4 & 2 \\ \hline
   11 &144 \\ \hline 
\end{tabular}
\end{table}
\end{document}

基本的に私が望んでいるのは、ヒートマップ、つまりテーブル内のセル内の数値に基づいてセルに色を付けることです。

ソリューション 1 のコードを使用した MWE

\documentclass[letterpaper, 10 pt]{article}

% Color
\usepackage{xcolor,colortbl}%
\usepackage{xintexpr}

\newcommand{\cTab}[2]
{%
    \edef\res    {\xinttheiexpr [2] #1/#2\relax}% [2] = "two digits after ."
    \edef\eighty {\xinttheiexpr [2] 0.8*#2\relax}%
    \cellcolor{black!\res}%
    \xintifboolexpr {#1>\eighty}
        % yes branch 
           {\color{white}}
        % no branch (nothing to do)
           {}% 
    {#1}%
}%

\begin{document}
\begin{table}[!ht]
\centering
\caption{True data classification.}
\label{tab:CollectedErrors}
\begin{tabular}{|p{1.5cm}|p{0.5cm}|}
\hline
\textbf{Exposure}& \textbf{Attribute} \\ \hline

  \cTab{4}{4} & 2 \\ \hline %example, could also be \cTab{4}{144}, in theory all cells should be changed to cTab
   11 &144 \\ \hline 
\end{tabular}
\end{table}
\end{document}

答え1

試してみるといい

\usepackage{xintexpr}

\newcommand{\cTab}[2]
{%
    \edef\res    {\xinttheiexpr [2] #1/#2\relax}% [2] = "two digits after ."
    \edef\eighty {\xinttheiexpr [2] 0.8*#2\relax}%
    \cellcolor{black!\res}%
    \xintifboolexpr {#1>\eighty}
    % yes branch 
       {\color{white}}
    % no branch (nothing to do)
       {}% 
    {#1}%
}%

しかし、mwe は役立ちます。わかりました。mwe で必要なこと\xdef\resはわかりましたが、何を目的としているのかよくわかりません。説明を更新して、今はよく理解しています... ( 0 から 100 のパーセンテージが求められるのに、0 から 1 の数値が必要だと信じていたため、 xcolora での色の指定について!混乱しました)。

マクロの定義を回避するように更新されました\res(\eighty特に\resグローバル スコープが必要なため煩わしかったのですが、ここでは の拡張性を使用できます\xinttheiexpr)。

\documentclass[letterpaper, 10 pt]{article}

% Color
\usepackage{xcolor,colortbl}

% Fine stuff
\usepackage{xintexpr, xinttools}

\newcommand{\cTab}[2]% #1 = cell, #2 = max
{%
    \cellcolor{black!\xinttheiexpr 100*#1/#2\relax}%
    \xintifboolexpr {#1>0.8*#2}%
    % "yes" branch 
       {\textcolor{blue}{#1}}% when #1 is big, print it blue
    % "no" branch
       {#1}% 
}%

\begin{document}
\begin{table}[!ht]
\centering
\caption{True data classification.}
\label{tab:CollectedErrors}
\smallskip
\begin{tabular}{|p{1.5cm}|c|}
\hline
\textbf{Exposure}& \textbf{Attribute} \\ \hline
\xintFor* #1 in {\xintSeq[3]{1}{100}}\do
{%
  \cTab{#1}{100} & \cTab{\the\numexpr100-#1\relax}{100}\\
}
\hline 
\end{tabular}
\end{table}
\end{document}

の使用にも注意してください\textcolor

引用ブロック

関連情報