2

I'm attempting to create an environment that takes its arguments as key-value pairs, uses some of them, and then forwards the rest on to a tblr environment from tabularray.

This results in an "Unexpandable command \l_foo_colspec_str inside column type" error for the colspec parameter, and while the style variable doesn't yield an error, neither does hlines get forwarded to tblr.

I'm reasonably certain that the issue is on the l3keys side rather than tabularray, since replacing the tblr with a tabular (and dropping the hlines bits) also fails (with an "Illegal pream-token" error).

I would like to either:

  1. Pass specific arguments on to tblr individually
  2. Remove specific key-value pairs from #1 and then do \begin{tblr}{#1}

(It can be made to work by passing the "outer" argument list and tblr argument list separately (\begin{tbl}{colspec=llx,hlines}{foo=bar}), but that's a less elegant interface.)

\ExplSyntaxOn
\keys_define:nn { foo } {
    bar .tl_set:N = \l_foo_bar_str,
    colspec .tl_set:N = \l_foo_colspec_str,
    style .tl_set:N = \l_foo_style_clist,
}

\newenvironment{tbl}[1]
{
    \keys_set:nn{foo}{#1}
    \l_foo_bar_str
    \begin{tblr}{colspec=\l_foo_colspec_str,\l_foo_style_clist}%
}
{
    \end{tblr}
}
\ExplSyntaxOff

\begin{document}
\begin{tbl}{
    bar=baz,
    colspec=llx,
    style={hlines},
}
a & b & c \\
a & b & c \\
a & b & c \\
\end{tbl}
\end{document}

1 Answer 1

2

Since tblr is defined in terms of a +b argument type with \NewDocumentEnvironment, also environments defined through it must comply. You also need to expand the argument before feeding it to \begin{tblr}.

\documentclass{article}
\usepackage{tabularray}

\ExplSyntaxOn
\keys_define:nn { foo } {
    bar .tl_set:N = \l_foo_bar_tl,
    colspec .tl_set:N = \l_foo_colspec_tl,
    style .tl_set:N = \l_foo_style_clist,
}

\NewDocumentEnvironment{tbl}{m +b}
{
    \keys_set:nn{foo}{#1}
    \l_foo_bar_tl
    \exp_args:Nne \begin{tblr}{colspec=\l_foo_colspec_tl,\l_foo_style_clist}
    #2
    \end{tblr}
}{}
\ExplSyntaxOff

\begin{document}

\begin{tbl}{
    bar=baz,
    colspec=llX,
    style={hlines},
}
a & b & c \\
a & b & c \\
a & b & c \\
\end{tbl}

\end{document}

Please, use tl and not str for token list variables.

enter image description here

1
  • Thanks. I'd tried the +b before, but exp_args was the major piece I was missing completely. A couple of followup questions: Is tl vs str syntactically significant, or just a naming convention? (I'll do the right thing either way, but I want to understand why.) And for the l_foo_style_clist argument, if I pass a somewhat more complicated argument than in the MWE, style={hlines,row{odd}={font=\large}}, it gives a TeX capacity exceeded. Tossing a \noexpand in front of \large makes it work, but is there any way to declare the argument (or the environment) s.t. that's unnecessary?
    – Ray
    Jan 6, 2023 at 15:26

You must log in to answer this question.

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