7

I'm trying to build a tblr table and have multiple macros containing alignment characters expanded in it, like so:

\documentclass{article}

\usepackage{tabularray}

\newcommand{\introrow}{Cat & Dog \\}
\newcommand{\outrorow}{Frog & Duck \\}


\begin{document}

\begin{tblr}{cc}
    \introrow
    Crow & Hawk \\
    \outrorow
\end{tblr}

\end{document}

(Background: I'm trying to define an environment that provides the specifications for a table written by the user, and provides multiple building blocks in the form of macros that the user can insert as needed.)

This doesn't work as tabularray needs to see all alignment characters. By passing expand=\introrow or expand=\outrorow, I can fix this for one of my macros.

Is there a way to have the tblr environment expand both macros? Adding two expand keys doesn't work.

1

4 Answers 4

8

Here is a different answer than my other, which requires minimal changes from the OP's MWE. Basically, instead of employing \newcommand to define his intro and outro, use \newtoks. That way, one can use [expand=\the] to get both of them expanded inside of tblr:

\documentclass{article}

\usepackage{tabularray}

\newtoks\introrow
\newtoks\outrorow

\introrow{Cat & Dog \\}
\outrorow{Frog & Duck \\}

\begin{document}

\begin{tblr}[expand=\the]{cc}
    \the\introrow
    Crow & Hawk \\
    \the\outrorow
\end{tblr}

\end{document}
3
5

I'm not sure this is playing the game as intended but

\documentclass{article}

\usepackage{tabularray}

\newcommand{\introrow}{Cat & Dog \\}
\newcommand{\outrorow}{Frog & Duck \\}



\begin{document}

\begin{tblr}[expand=\expandafter]{cc}
    \expandafter\empty\introrow
    Crow & Hawk \\
    \expandafter\empty\outrorow
\end{tblr}

\end{document}
4
  • 2
    I'm impressed... Oct 6, 2021 at 16:10
  • 3
    @PhelypeOleinik that isn't how you spell horrified Oct 6, 2021 at 16:11
  • This looks like a horrible hack. I like it! (Although the hack is visible to the user, making it slightly less elegant.)
    – fefrei
    Oct 6, 2021 at 20:23
  • I tried it but it fails to display colors on the first cell… See my post here tex.stackexchange.com/questions/675331/…
    – tobiasBora
    Feb 15, 2023 at 12:38
3

Depending on what kind of structure your tblr has, this may suffice. Here, I employ a tokencycle to scan the tokens of the new xtblr environment, in search of control sequences. Any token not a control sequence gets echoed to a token list. The \\ macro also gets echoed to the token list. When any other control sequence is found, however, it is once expanded before being added to the token list.

Once the full environment content is so scanned, the token list is regurgitated as the argument to tblr.

\documentclass{article}

\usepackage{tabularray,tokcycle,environ}

\newcommand{\introrow}{Cat & Dog \\}
\newcommand{\outrorow}{Frog & Duck \\}
\NewEnviron{xtblr}[1]{%
  \resettokcycle
  \Macrodirective{\ifx\\##1\addcytoks{##1}\else\addcytoks[1]{##1}\fi}%
  \expandafter\tokcyclexpress\expandafter{\BODY}%
  \begin{tblr}[expand=\the]{#1}\the\cytoks\end{tblr}%
}
\begin{document}

\begin{xtblr}{cc}
    \introrow
    Crow & Hawk \\
    \outrorow
\end{xtblr}

\end{document}

enter image description here

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. It even works with arguments:

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

You must log in to answer this question.

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