2

I'm encountering a strange problem when trying to pass a LaTeX3 object (a token list) as table column specifier. LaTeX issues an error

Package array Error:  Illegal pream-token (\l_my_tl): `c' used.

Below is a minimal example that reproduces the problem. Interestingly, the problem disappears if the line \usepackage{array} is commented out.

\documentclass{article}

% ===>> The problem disappears if this line is commented out: 
\usepackage{array}  
% <<=== 

% Uncomment for older versions of latex
% \usepackage{xparse}  

\ExplSyntaxOn

\NewDocumentCommand{\mytabular}{}{%
  \tl_set:Nn \l_my_tl { l c }
  % Same error if we use a string instead of a token list.
  % \str_set:Nn \l_my_str { l c }
  \begin{tabular}{\l_my_tl}
    Header1 & Header2 \\
    Row1 & Row2 \\
  \end{tabular}

  % Here with an additional vertical alignment specifier.
  % Issues a similar error as above.
  \begin{tabular}[t]{\l_my_tl}
    Header1 & Header2 \\
    Row1 & Row2 \\
  \end{tabular}
}
\ExplSyntaxOff

\begin{document}
\mytabular

\end{document}

What is going on here? Is this a bug of array? What to do about it?

3
  • 1
    no it is a documented feature so user error not a bug.use\exp_args:Nne\begin or \ExpandArgs{ne}\begin Mar 18 at 7:45
  • Thanks a million! Where is this documented? Very difficult for a beginner to track down this kind of problems.
    – normanius
    Mar 18 at 8:02
  • @DavidCarlisle I just noticed that there is another problem when the vertical alignment [t] is specified. What can be done about it?
    – normanius
    Mar 18 at 8:20

2 Answers 2

4

the arraypackage documentation says

Lamport’s original definition allowed you to do the following (although it was probably a mis-use of the system).

       \newcommand{\X}{clr}
       \begin{tabular}{\X} . . .

array.sty takes great care not to expand the preamble, and so the above does not work with the new scheme. With the new version this functionality is returned:

       \newcolumntype{X}{clr}
       \begin{tabular}{X} . . .

enter image description here

\documentclass{article}

% ===>> The problem disappears if this line is commented out: 
\usepackage{array}  
% <<=== 

% Uncomment for older versions of latex
% \usepackage{xparse}  

\ExplSyntaxOn

\NewDocumentCommand{\mytabular}{}{%
  \newcolumntype\l_my_tl { l c }
  % Same error if we use a string instead of a token list.
  % \str_set:Nn \l_my_str { l c }
  \begin{tabular}{\l_my_tl}
    Header1 & Header2 \\
    Row1 & Row2 \\
  \end{tabular}

  \par
  % Here with an additional vertical alignment specifier.
  % Issues a similar error as above.
  \begin{tabular}[t]{\l_my_tl}
    Header1 & Header2 \\
    Row1 & Row2 \\
  \end{tabular}
}
\ExplSyntaxOff

\begin{document}
\mytabular

\end{document}
0
3

Expand before evaluating \begin{tabular}:

\documentclass{article}

% ===>> The problem disappears if this line is commented out: 
\usepackage{array}  
% <<=== 

% Uncomment for older versions of latex
% \usepackage{xparse}  

\ExplSyntaxOn

\NewDocumentCommand{\mytabular}{}{%
  \tl_set:Nn \l_my_tl { l c }
  \use:e { \exp_not:N \begin{tabular}{\l_my_tl} }
    Header1 & Header2 \\
    Row1 & Row2 \\
  \end{tabular}

  % Here with an additional vertical alignment specifier.
  % Issues a similar error as above.
  \use:e { \exp_not:N \begin{tabular}[t]{\l_my_tl} }
    Header1 & Header2 \\
    Row1 & Row2 \\
  \end{tabular}
}
\ExplSyntaxOff

\begin{document}
\mytabular

\end{document}

enter image description here

Let's try a more “real world” example.

\documentclass{article}
\usepackage{array}  

\ExplSyntaxOn

\keys_define:nn { normanius/tabular }
 {
  valign .tl_set:N  = \l__normanius_tabular_valign_tl,
  cols   .tl_set:N  = \l__normanius_tabular_cols_tl,
  valign .initial:n = c,
 }

\NewDocumentEnvironment{mytabular}{m}
 {
  \normanius_tabular:n { #1 }
 }
 { \endtabular }

\cs_new_protected:Nn \normanius_tabular:n
 {
  \keys_set:nn { normanius/tabular } { #1 }
  \__normanius_tabular:VV \l__normanius_tabular_valign_tl \l__normanius_tabular_cols_tl
 }
\cs_new_protected:Nn \__normanius_tabular:nn
 {
  \tabular[#1]{#2}
 }
\cs_generate_variant:Nn \__normanius_tabular:nn { VV }

\ExplSyntaxOff

\begin{document}

X\begin{mytabular}{cols=lc}
    Header1 & Header2 \\
    Row1 & Row2 \\
\end{mytabular}X

\bigskip

X\begin{mytabular}{valign=t,cols=lc}
    Header1 & Header2 \\
    Row1 & Row2 \\
\end{mytabular}X

\end{document}

enter image description here

2
  • Great to learn about the named options! Thanks for this. I didn't know that these existed.
    – normanius
    Mar 20 at 22:27
  • And the expansion problem is avoided by using NewDocumentEnvironment. Slick!
    – normanius
    Mar 20 at 22:45

You must log in to answer this question.

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