tabulararray:不能使用 def 来设置单元格属性?

tabulararray:不能使用 def 来设置单元格属性?

为了保持表格的连贯性,我想定义一个命令,比如\def\YES{\SetCell{bg=green9} YES}在输入时自动添加绿色背景\YES。不幸的是,它不应用背景颜色:

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

答案1

对我有用:

在此处输入图片描述

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

答案2

作为参考,这是 lvjr 的精彩答案(这里,如果您希望我接受,请随意写下您自己的答案),其技巧是用来\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}

请注意,您需要使用\unexpanded命令保护其中的易碎命令(如果有)。

相关内容