tabularray icon indicating copy to clipboard operation
tabularray copied to clipboard

expand multiple macros

Open cdacl opened this issue 4 years ago • 5 comments

When I have more than one macro needed to expand in the table, and the macro including & or \\ placed after the macro not including & or \\, the table broken.

This sample code is ok:

\def\a{a &b\\}
\def\b{z}


\begin{tblr}[expand={\a,\b}]{hlines,vlines}
 \b \\
 \a  
\end{tblr}

1

But when I changed the order of expand macroes, the table broken:

\begin{tblr}[expand={\b,\a}]{hlines,vlines}
 \b \\
 \a  
\end{tblr}

2

cdacl avatar Aug 19 '21 09:08 cdacl

In addition, when both two macros include & or \\, no matter what order it will make the table broken.

cdacl avatar Aug 19 '21 09:08 cdacl

In fact, tabularray can only expand one macro. You can not put multiple macros there.

lvjr avatar Aug 19 '21 09:08 lvjr

So, will more macros be expanded in futrue? :-)

cdacl avatar Aug 19 '21 09:08 cdacl

Yes, I will make it possible to expand multiple macros in the future.

lvjr avatar Aug 19 '21 09:08 lvjr

Thank you very much!

In fact, if all macros don't include & or \\, the table will be built correctly as expected.

cdacl avatar Aug 19 '21 09:08 cdacl

Also interested in this feature, especially if the macros can take arguments as inputs. See my issue here https://tex.stackexchange.com/questions/675331/tabularray-expand-multiple-macros-with-background-color

tobiasBora avatar Feb 15 '23 12:02 tobiasBora

@tobiasBora It doesn't really help even if tabularray could expand multiple macros. For commands with optional arguments, you need exhaustive expansions, which are provided by \expanded command:

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

image

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

lvjr avatar Feb 16 '23 00:02 lvjr

Awesome trick, thanks a lot! This should be documented.

tobiasBora avatar Feb 16 '23 07:02 tobiasBora

Hi @lvjr. I faced upon this issue too and wondering if this is the intended behavior of tabularray. I noticed @muzimuzhi already worked on multiple macros expansion feature, but we still need to declare the macros list to be expanded.

I mean, is this inability to do automatic macros expansion of tabularray is the intended behavior, or it will be worked out in the future to reach "behavior parity" with tabular for the automatic macros expansion?

One of the use case would be if-else usage in the table environment.

mangkoran avatar Aug 01 '23 03:08 mangkoran