3

I am trying to use a background color on longtblr of tabularray package within \SetCell command and it seems I only can used named colors. E.g. the ones defined before with \definecolor. I am buidling a generator and would like to use the color defined on the spot.

\definecolor{c5E90CB}{HTML}{5E90CB}
...    
\SetCell[r=1,c=3]{bg=c5E90CB,halign=l,valign=h}\makecell{Test instructions and report}

I would like to use the color definition directly, without defining it upfront - something like

\SetCell[r=1,c=3]{bg=\color[HTML]{5E90CB},halign=l,valign=h}{Test instructions and report}

but this is not working.

Thanks, Blaž

2
  • There is an open issue to add this feature (with some discussion on the best syntax) github.com/lvjr/tabularray/issues/106 Sep 27, 2021 at 15:26
  • Thank you David. Would it be possible to define the color somewhere in the "cell section"? After the \hline or & and before \SetCell. Sep 27, 2021 at 15:55

2 Answers 2

6

Would it be possible to define the color somewhere in the "cell section"? After the \hline or & and before \SetCell?

Yes, you can use \NewTableCommand to create a \DefineColor command. Then you can put it at the beginning of cells.

\documentclass{article}

\usepackage{xcolor}
\usepackage{tabularray}

\NewTableCommand{\DefineColor}[3]{\definecolor{#1}{#2}{#3}}

\begin{document}

\begin{tblr}{lcr}
\hline
 Alpha   & Beta  & Gamma \\
\hline
 \DefineColor{c5E90CB}{HTML}{5E90CB}
 \SetCell[r=1,c=3]{bg=c5E90CB,halign=l,valign=h} Epsilon & & \\
\hline
 Iota    & Kappa & Lambda \\
\hline
\end{tblr}

\end{document} 

enter image description here

2

Here's a slightly more automated solution that avoids choosing a color name (and also answers this question more precisely):

\documentclass{article}

\usepackage{xcolor}
\usepackage{tabularray}

\newcounter{CurrentCellBgColor}
\NewTableCommand{\SetCellBgColor}[2]{%
  \stepcounter{CurrentCellBgColor}%
  \definecolor{CurrentCellBgColor-\theCurrentCellBgColor}{#1}{#2}%
  \SetCell{bg=CurrentCellBgColor-\theCurrentCellBgColor}%
}

\begin{document}

\begin{tblr}{lcr}
  \hline
  Alpha   & Beta  & Gamma \\
  \hline
  \SetCellBgColor{HTML}{5E90CB}
  \SetCell[r=1,c=3]{halign=l,valign=h} Epsilon & & \\
  \hline
  \SetCellBgColor{HTML}{905eCB}
  \SetCell[r=1,c=3]{halign=c,valign=h} Epsilon & & \\
  \hline
  \SetCellBgColor{rgb}{200,100,0}
  \SetCell[r=1,c=3]{halign=r,valign=h} Epsilon & & \\
  \hline
  Iota    & Kappa & Lambda \\
  \hline
\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 .