代码

代码

为什么在表格内工作正常\@ifnextchar\end{tabular}{}{\midrule}但是却不起作用?\midrule

我特别对规范的答案感兴趣,以满足我的好奇心。

但是,我的目的是编写自动消除\hline最后一行后面的代码。我愿意听取建议。

代码

\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{booktabs}% \midrule

\catcode`@=11 % or \makeatletter to change category code of @ to 11 and temporarily to access kernel macro \@tabularcr
\let \clone@tabularcr \@tabularcr
\def\@tabularcr{\clone@tabularcr \midrule}% first version
%\def\@tabularcr{\clone@tabularcr \@ifnextchar\end{tabular}{}{}}% <-- this line works, injecting no tokens
%\def\@tabularcr{\clone@tabularcr \@ifnextchar\end{tabular}{}{\midrule}}% <-- this does not work, injecting hopefully just a \midrule as in first version
\catcode`@=12 % or \makeatother to restore category code of @ to 12

\begin{document}
\begin{tabular}{lll}
Meaning & col2 & col3 \\
\meaning\\ & col2 & col3 \\
col1 & col2 & col3 \\
\end{tabular}
\end{document}

答案1

错误是

! Misplaced \noalign.
\midrule ->\noalign 

\@ifnextchar这和你从哪里得到同样的东西无关

\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{booktabs}% \midrule

\begin{document}
\begin{tabular}{lll}
Meaning & col2 & col3 \\
\meaning\\ & col2 & col3 \\
col1 & col2 & col3 \\
\relax\midrule
\end{tabular}
\end{document}

\midrulelike\hline和其他几个表命令必须是行中的第一个(扩展后),因为它使用了\noalign原语。

因此,任何不可扩展的标记都会产生错误,在进行测试时内部\@ifnextchar涉及几项分配。\def

相关内容