0

I'm using tabularray to create my confusion matrix but I've been unable to automatically changing the background color of it, like many examples out there:

Here is my minimal working example:

\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}

I've been unsuccessful in applying a formula to a range, such as:

cell{3-5}{3-5} = {cmd=\formula_to_automatically_scale_color} Or directly applying it to a cell: \formula_to_automatically_scale_color{} nothing seems to work.

I would greatly appreciate your help

1 Answer 1

0

Just a starting point.

\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .