我正在使用它tabularray
来创建混淆矩阵,但无法自动更改它的背景颜色,就像许多例子一样:
这是我的最小工作示例:
\documentclass{article}
\usepackage{color}
\usepackage{tabularray}
\usepackage{graphicx}
\begin{document}
\begin{table}
\centering
\resizebox{\linewidth}{!}{%
\begin{tblr}{
cell{1}{3} = {c=3}{},
cell{3}{1} = {r=3}{},
}
& & Predicted & & \\
& & Class A & Class B & Class C \\
Actual & Class A & 1.00 & & 0.10 \\
& Class B & 0.10 & 0.80 & 0.10 \\
& Class C & 0.30 & & 0.70
\end{tblr}
}
\end{table}
\end{document}
我无法将公式应用到某个范围,例如:
cell{3-5}{3-5} = {cmd=\formula_to_automatically_scale_color}
或者直接将其应用到单元格:\formula_to_automatically_scale_color{} 似乎没有任何作用。
非常感谢你的帮助
答案1
只是一个起点。
\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor}
\ExplSyntaxOn
\cs_new_protected:Npn \mysetcellcolor #1
{
\bool_lazy_and:nnT
{ \lTblrMeasuringBool }
{ ! \tl_if_empty_p:n {#1} }
{
% When `cmd` code is used, table commands like \SetCell are already
% restored to definitions outside of tblr env (i.e., using \SetCell in
% `cmd` code has no effect), a tabularray internal is used.
% see https://github.com/lvjr/tabularray/blob/e5f340b1f8a60b7b24acdf02991cf0a29811426a/tabularray.sty#L1804
\__tblr_cell_gput:ne { background } {blue!\fp_to_tl:n{#1*100}!green}
}
#1
}
\ExplSyntaxOff
\begin{document}
\begin{tblr}{
cell{1}{3} = {c=3}{},
cell{3}{1} = {r=3}{},
cell{3-5}{3-5} = {cmd=\mysetcellcolor}
}
& & Predicted & & \\
& & Class A & Class B & Class C \\
Actual & Class A & 1.00 & & 0.10 \\
& Class B & 0.10 & 0.80 & 0.10 \\
& Class C & 0.30 & & 0.70
\end{tblr}
\end{document}