使用 LaTeX3 / expl3 与包数组一起使用时出现“Illegal pream-token”错误

使用 LaTeX3 / expl3 与包数组一起使用时出现“Illegal pream-token”错误

当我尝试将 LaTeX3 对象(标记列表)作为表列说明符传递时,我遇到了一个奇怪的问题。LaTeX 发出错误

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

\usepackage{array}下面是一个重现该问题的最小示例。有趣的是,如果注释掉该行,问题就会消失。

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

这是怎么回事?这是 的一个错误吗array?该怎么办?

答案1

包装array文档说

Lamport 最初的定义允许您执行以下操作(尽管这可能是系统的误用)。

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

array.sty非常小心地不扩展前导码,因此上述内容不适用于新方案。在新版本中,此功能将恢复:

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

在此处输入图片描述

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

答案2

求值前先展开\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}

在此处输入图片描述

让我们尝试一个更“真实世界”的例子。

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

在此处输入图片描述

相关内容