1

in order to keep a coherant look in my tabular, I wanted to define a command like \def\YES{\SetCell{bg=green9} YES} to automatically add a green background when I type \YES. Unfortunately, it does not apply the background color:

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

\begin{document}

{ \def\YES{\SetCell{bg=green9} YES} \begin{center} \begin{tblr}{colspec={cc}}% Tried to use expand=\YES, does not work. \YES{} & fails but this works: & \SetCell{bg=green9} YES \ \end{tblr} \end{center} }

\end{document}

tobiasBora
  • 8,684

2 Answers2

2

Works for me:

enter image description here

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

\begin{document}

{ \def\YES{\SetCell{bg=green9} YES} \begin{center} \begin{tblr}[expand=\YES]{colspec={cc}}% Tried to use expand=\YES, does not work. \YES & fails but this works: & \SetCell{bg=green9} YES \ \end{tblr} \end{center} }

\end{document}

David Carlisle
  • 757,742
  • Ohhh I was using expand=\YES as part of the mandatory argument, I did not realized it was inside an optional environment... thanks! – tobiasBora Nov 26 '21 at 17:04
  • Hum... how would you expand multiple commands, like YES and no? – tobiasBora Nov 26 '21 at 17:09
  • 1
    @tobiasBora well I would have put money on you asking that next. https://tex.stackexchange.com/a/617982/1090 – David Carlisle Nov 26 '21 at 17:10
  • Ahah thanks ^^ Any reason such horrible hacks are necessary? I mean, wouldn't it be possible to ask to the tabularray team to implement it directly? – tobiasBora Nov 28 '21 at 20:50
  • @tobiasBora they (I think actually a person not a team) have an issue tracker you could ask. But tables are tricky and the chosen design here is to read the table body without expansion while collecting the cells. (Probably to make it easier to re-run each cell multiple times with trial settings) – David Carlisle Nov 28 '21 at 21:00
1

For reference, 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 to do multiple macro expansions:

\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