3

I'm trying to expand in tabularry multiple macros to automatically add different background colors, but the color is not taken into account:

enter image description here

\documentclass{memoir}
\usepackage{tabularx}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\NewExpandableDocumentCommand{\yes}{O{Yes}m}{\SetCell{bg=green9} #1} \NewExpandableDocumentCommand{\no}{O{No}m}{\SetCell{bg=red8} #1}

\begin{document}

\begin{tblr}[expand=\expandafter]{cc} What I want & is below\ \SetCell{bg=green9} Yes & \SetCell{bg=red8} No\ \SetCell{bg=green9} Great & \SetCell{bg=red8} Bad\ What I get & is below\ \expandafter\empty\yes{} & \expandafter\empty\no{}\ \expandafter\empty\yes[Great]{} & \expandafter\empty\no[Bad]{} \end{tblr}

\end{document}

Related, but don't fix my issue: tabulararray: can't use def to set cell properties? tabularray: Expand multiple macros

tobiasBora
  • 8,684
  • expandable doesn't mean "expands in one step" (as you would need for \expandafter). Beside this: even if it would expand to \SetCell, it wouldn't work as there is still the \empty before, and something like \empty\SetCell{bg=green9} Yes doesn't work. – Ulrike Fischer Feb 15 '23 at 14:48
  • @UlrikeFischer I see thanks… so there is no way to achieve what I want to do? – tobiasBora Feb 15 '23 at 16:46
  • Do you need any text inside these macros or just to adjust the color? – Udi Fogiel Feb 15 '23 at 17:28

3 Answers3

4

From the manual of tabularray:

enter image description here

So, NO optional argument. That's not possible. And only one macro can be expanded.

\documentclass{memoir}
\usepackage{tabularx}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\newcommand{\yn}[2]{\SetCell{bg=\if#1ygreen9\else red8\fi}#2}

\begin{document}

\begin{tblr}[expand=\yn]{cc} What I want & is below\ \SetCell{bg=green9} Yes & \SetCell{bg=red8} No\ \SetCell{bg=green9} Great & \SetCell{bg=red8} Bad\ What I get & is below\ \yn{y}{Yes} & \yn{n}{No}\ \yn{y}{Great} & \yn{n}{Bad} \end{tblr}

\end{document}

enter image description here

A possibly better definition of \yn is

\ExplSyntaxOn
\cs_new:Npn \yn #1 #2
 {
  \SetCell{bg=\str_case:nn {#1}{{y}{green9}{n}{red8}}}#2
 }
\ExplSyntaxOff

With standard methods:

\documentclass{memoir}
\usepackage[table]{xcolor}
\usepackage{ninecolors}

\NewExpandableDocumentCommand{\yes}{O{Yes}m}{\cellcolor{green9}#1} \NewExpandableDocumentCommand{\no}{O{No}m}{\cellcolor{red8}#1}

\begin{document}

\begin{tabular}{cc} First & Second \ \yes{} & \no{}\ \yes[Great]{} & \no[Bad]{} \end{tabular}

\end{document}

enter image description here

egreg
  • 1,121,712
3

If you don't need any text inside the commands, and just want a shorter macro name for \SetCell{bg=red8}, tabularray has the command \NewTableCommand for that (see section 3.6 and subsection 3.2.3 of the documentation):

\documentclass{memoir}
\usepackage{tabularx}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\NewTableCommand\yes{\SetCell{bg=green9}} \NewTableCommand\no{\SetCell{bg=red8}}

\begin{document}

\begin{tblr}{cc}
    What I want & is below\\
    \SetCell{bg=green9} Yes & \SetCell{bg=red8} No\\
    \SetCell{bg=green9} Great & \SetCell{bg=red8} Bad\\
    What I get & is below\\
     \yes Yes & \no No\\
    \yes Great & \no Bad
\end{tblr}

\end{document}

Udi Fogiel
  • 3,824
1

That's the great answer of lvjr (here, feel free to write your own answer if you want me to accept it), whose trick is to use \expanded:

\documentclass{memoir}
\usepackage{xcolor}
\usepackage{tabularray}

\NewExpandableDocumentCommand{\yes}{O{Yes}m}{\SetCell{bg=green9}#1} \NewExpandableDocumentCommand{\no}{O{No}m}{\SetCell{bg=red8}#1}

\begin{document}

\begin{tblr}[expand=\expanded]{cc} What I want & is below \ \SetCell{bg=green9} Yes & \SetCell{bg=red8} No \ \SetCell{bg=green9} Great & \SetCell{bg=red8} Bad \ What I get & is below \ \expanded{\yes{}} & \expanded{\no{}} \ \expanded{\yes[Great]{}} & \expanded{\no[Bad]{}} \end{tblr}

\end{document}

Note that you need to protect fragile commands (if any) inside them with \unexpanded command.

tobiasBora
  • 8,684