如何使用 < 和 > 列说明符?

如何使用 < 和 > 列说明符?

我不太明白。我希望能够处理表中的条目,但 > 和 < 的表现并不像我预期的那样。以下是重现:

\documentclass{article}
\usepackage{array}
\newcolumntype{Z}{>{\foo}c<{\bar}}
\def\foo#1\bar{foo #1 bar}
\begin{document}
\begin{tabular}{ZZZZ}
A & B & C & D \\
E & F & G & H
\end{tabular}
\end{document}

当我运行这个程序时,xelatex我得到以下输出:

$ xelatex repro
This is XeTeX, Version 3.14159265-2.6-0.99998 (TeX Live 2017/MacPorts 2017_0) (preloaded format=xelatex)
 restricted \write18 enabled.
entering extended mode
(./repro.tex
LaTeX2e <2017-04-15>
Babel <3.10> and hyphenation patterns for 5 language(s) loaded.
(/opt/local/share/texmf-texlive/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/opt/local/share/texmf-texlive/tex/latex/base/size10.clo))
(/opt/local/share/texmf-texlive/tex/latex/tools/array.sty) (./repro.aux)
! Misplaced \cr.
\reserved@c ->\ifnum 0=`{}\fi \cr 

l.8 E &
        F & G & H
? 

奇怪的是,如果我使用作为前导,它会按我预期的方式工作{ZZcc},但这肯定只是运气好而已。如何使用 > 和 < 列说明符来处理表条目?

答案1

最后一列不太容易捕捉,因为它不是以 结束的&。然后\foo获取到下一列之前的所有内容,&包括\\

一个解决方法是添加一个虚拟的最后一列:

\documentclass{article}
\usepackage{array}
\newcolumntype{Z}{>{\foo}c<{\bar}}
\def\foo#1\bar{foo #1 bar}
\begin{document}
\begin{tabular}{ZZZZ@{}c}
A & B & C & D &\\
E & F & G & H &
\end{tabular}
\end{document}

结果

相关内容