3

I am trying to create a custom tblr environment for a schedule which I reuse often. I want to include an optional label for cross-referencing.

I'm using the xparse \NewDocumentEnvironment command together with tabularray, but I cannot get tabularray to expand the command \IfValueT{#1}{label={#1},} in the outer specification.

Everything works if I instead use \IfValueTF and duplicate the \begin{tblr}[, but that doesn't seem very robust.

Is there a way to make the "testa" option in the below MWE work?

MWE:

\documentclass{scrreprt}

\usepackage{tabularray}

\NewDocumentEnvironment{testa}{o m +b}{
\begin{tblr}[
    \IfValueT{#1}{label={#1},}
    caption={#2},
    long,
]{
    colspec = {ll},
}
    Column 1 & Column 2 \\
    #3
\end{tblr}
}{}


\NewDocumentEnvironment{testb}{o m +b}{
\IfValueTF{#1}{\begin{tblr}[label={#1},}{\begin{tblr}[}
    caption={#2},
    long,
]{
    colspec = {ll},
}
    Column 1 & Column 2 \\
    #3
\end{tblr}
}{}



\begin{document}

\begin{testa}[labela]{captiona}
    a & b
\end{testa}

\begin{testb}[labelb]{captionb}
    a & b
\end{testb}

Tables \ref{labela} and \ref{labelb} should be the same, but Table \ref{labela} is an undefined reference.

\end{document}

2 Answers 2

2

I would set the option outside of the table:

\documentclass{scrreprt}

\usepackage{tabularray}

\NewDocumentEnvironment{testa}{o m +b}{%
\IfValueT{#1}{\SetTblrOuter{label={#1}}}%
\begin{tblr}[
    caption={#2},
    long,
]{
    colspec = {ll},
}
    Column 1 & Column 2 \\
    #3
\end{tblr}
}{}




\begin{document}

\begin{testa}[labela]{captiona}
    a & b
\end{testa}


Tables \ref{labela} 

\end{document}
2

You could go with

\documentclass{scrreprt}

\usepackage{tabularray}

\NewDocumentEnvironment{testa}{o m +b}{%
  \expanded{\noexpand\begin{tblr}[
    \IfValueT{#1}{label={#1},}}
    caption={#2},
    long,
]{
    colspec = {ll},
}
    Column 1 & Column 2 \\
    #3
\end{tblr}%
}{}

\begin{document}

\begin{testa}[labela]{captiona}
    a & b
\end{testa}

\end{document}

A cleaner version:

\documentclass{scrreprt}

\usepackage{tabularray}

\ExplSyntaxOn
\NewDocumentCommand{\testastart}{mm}
 {
  \jw_testastart:en { \tl_if_novalue:nF { #1 } { label={#1} } } { #2 }
 }
\cs_new_protected:Nn \jw_testastart:nn
 {
  \begin{tblr}[#1, caption={#2},long]
 }
\cs_generate_variant:Nn \jw_testastart:nn { e }
\ExplSyntaxOff

\NewDocumentEnvironment{testa}{o m +b}{%
    \testastart{#1}{#2}{
    colspec = {ll},
}
    Column 1 & Column 2 \\
    #3
\end{tblr}%
}{}

\begin{document}

\begin{testa}[labela]{captiona}
    a & b
\end{testa}

\end{document}

enter image description here

The .aux file

\relax 
\providecommand*\new@tpo@label[2]{}
\newlabel{labela}{{1}{1}{}{}{}}
\@writefile{lot}{\contentsline {table}{\numberline {1}{captiona}}{1}{}\protected@file@percent }
\gdef \@abspage@last{1}
1
  • Thanks egreg! The particular combination of \expanded and \noexpand is what I was trying but couldn't work out. Just putting \expanded before the \IfValueT doesn't help of course. I've marked Ulrike's answer as the preferred one though, since it seems the best solution for tabularray specifically (though yours would work with any environment).
    – jw5801
    Nov 20 at 23:42

You must log in to answer this question.

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