![使用 LaTeX3 / expl3 与包数组一起使用时出现“Illegal pream-token”错误](https://linux22.com/image/462527/%E4%BD%BF%E7%94%A8%20LaTeX3%20%2F%20expl3%20%E4%B8%8E%E5%8C%85%E6%95%B0%E7%BB%84%E4%B8%80%E8%B5%B7%E4%BD%BF%E7%94%A8%E6%97%B6%E5%87%BA%E7%8E%B0%E2%80%9CIllegal%20pream-token%E2%80%9D%E9%94%99%E8%AF%AF.png)
当我尝试将 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}