2

I try to set the color of a cell in a tabularray by a command:

\documentclass{article}

\usepackage{xcolor}
\usepackage{tabularray}

\ExplSyntaxOn

\newcommand{\Tcolor}{
    green
}

\ExplSyntaxOff


\begin{document}
    \textcolor{\Tcolor}{abc}
    
\begin{tblr}{
        colspec  = {l},
        cell{1}{1} = {\Tcolor},
    }
abc  \\
\end{tblr}

\end{document}

Somehow the color cell{1}{1} = {\Tcolor}, breaks. Why?

cell{1}{1} = {green}, works...

EDIT: I guess in my attempt to create a MWE I shortened it too much. There is a need for a function instead of a simple color:

\documentclass{article}

\usepackage{xcolor}
\usepackage{tabularray}

\ExplSyntaxOn


\newcommand{\Tcolor}[1]{
    \int_compare:nNnTF
    {#1} = {0}
    { red } 
    {
        \int_compare:nNnTF
        {#1} < {30}
        { orange } 
        {
            \int_compare:nNnTF
            {#1} < {40}
            { green } 
            {
                blue
            }
        }
    }
    
}

\ExplSyntaxOff


\begin{document}
    \textcolor{\Tcolor{32}}{abc}
    
\begin{tblr}{
        colspec  = {l},
        cell{1}{1} = {\Tcolor{32}},
        cell{1}{2} = {\Tcolor{48}}, 
    }
abc  \\
def \\
\end{tblr}

\end{document}
1
  • 1
    This is not the best way to define colors. Do \colorlet{Tcolor}{green} and use cell{1}{1}=Tcolor
    – egreg
    Jul 27, 2023 at 20:09

1 Answer 1

3

The cell{<i>}{<j>} key can accept several types of values; for instance, according to the manual, you can do

cell{1}{1}={cmd=\fbox}

Thus the managing of values is necessarily complex. When a value is just a string that's not another key, it is interpreted as a color name for the background.

You can use bg=<color> and, in this case, you can use a macro.

\documentclass{article}

\usepackage{xcolor}
\usepackage{tabularray}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\Tcolor}{m}
  {
    \bool_case:nF
      {
        { \int_compare_p:n {#1 = 0} }{ red!80 } 
        { \int_compare_p:n {0 < #1 < 30} }{ orange } 
        { \int_compare_p:n {30 <= #1 < 40} }{ green!80 }
      }
      { blue!60!green }
  }

\ExplSyntaxOff

\begin{document}

\textcolor{\Tcolor{32}}{abc}

\medskip
    
\begin{tblr}{
   colspec  = {l},
   cell{1}{1} = {bg=\Tcolor{0}},
   cell{2}{1} = {bg=\Tcolor{15}}, 
   cell{3}{1} = {bg=\Tcolor{32}}, 
   cell{4}{1} = {bg=\Tcolor{48}}, 
}
abc \\
def \\
ghi \\
jkl \\
\end{tblr}

\end{document}

Note the use of \bool_case:nF in order to avoid the awkward nesting of conditionals.

enter image description here

4
  • Thanks. But I now realize that I had shortened my example too much. I really want to choose a color depending on a variable. Please see my edited question. Jul 27, 2023 at 20:53
  • @mrCarnivore Edited to suit your needs.
    – egreg
    Jul 27, 2023 at 21:23
  • Thank you very much for that elegant solution. The \bool_case:nF is very neat compared to my earlier attempt. I also noticed the subtle change of the colors to make them visually more appealing. I fully agree your colors are better than in my example. In my real example I am using other colors that are also not pure green or red. Jul 28, 2023 at 11:03
  • @mrCarnivore I changed the colors so they're not punching in the eye of the beholder. 😁 I can tell you that I made the request for \bool_case:n(TF).
    – egreg
    Jul 28, 2023 at 12:18

You must log in to answer this question.

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