![使用带有 tabularx X 列类型的 collcell 包](https://linux22.com/image/384525/%E4%BD%BF%E7%94%A8%E5%B8%A6%E6%9C%89%20tabularx%20X%20%E5%88%97%E7%B1%BB%E5%9E%8B%E7%9A%84%20collcell%20%E5%8C%85.png)
我想使用包X
中的列类型。tabularx
collcell
在下面的 MWE 中,我定义:
\newcolumntype{R}{>{\collectcell\TitleColumn}{r}<{\endcollectcell}}% works
\newcolumntype{T}{>{\collectcell\TitleColumn}{X}<{\endcollectcell}}% fails
其中T
应用X
列类型(无法编译因此在 MWE 中被注释掉)并且R
(为了说明目的)应用r
列类型(作品)。
输出有三个表:
R
列类型(确保语法正确)- 所需的
T
列类型(注释掉) T
列类型的期望结果
如果表 2(使用T
列类型)未注释,则错误消息为:
包数组错误:非法前置标记(X):使用了‘c’。
笔记:
- MWE 被大大简化了,因此这里实际上不需要使用
collcell
,但在实际使用情况下需要,其中\TitleColumn
和\ColumnL
宏有更多代码。
代码:
\documentclass{article}
\usepackage{collcell}
\usepackage{tabularx}
\usepackage{showframe}
\newcommand{\ColumnL}[1]{#1}
\newcommand{\TitleColumn}[1]{#1}
\newcolumntype{R}{>{\collectcell\TitleColumn}{r}<{\endcollectcell}}% works
\newcolumntype{T}{>{\collectcell\TitleColumn}{X}<{\endcollectcell}}% fails
\newcommand*{\TabularContent}{%
Label 1 Very Long& 62 & 79 \\
Label 2 Empty Row & \\
Label 3 Short& 732 & 31 \\
}
\begin{document}
\noindent
1. Column type \verb|R| (equivalent to centered \verb |r| column) works fine:
\par\smallskip\noindent
\begin{tabularx}{\linewidth}{@{}Rll@{}}
\TabularContent
\end{tabularx}
\bigskip
\noindent
2. Column type \verb|T| (equivalent to centered \verb |c| column) FAILS
\par(commented out):
\par\smallskip\noindent
%\begin{tabularx}{\linewidth}{@{}Tll@{}}%% <--- How to get this to work
% \TabularContent
%\end{tabularx}
\bigskip
\noindent
3. Desired result of above \verb|T| column type is the \verb|X| column type::
\par\smallskip\noindent
\begin{tabularx}{\linewidth}{@{}Xll@{}}
\TabularContent
\end{tabularx}
\end{document}
答案1
移除周围的括号X
就可以了:
\documentclass{article}
\usepackage{collcell}
\usepackage{tabularx}
\usepackage{showframe}
\newcommand{\ColumnL}[1]{#1}
\newcommand{\TitleColumn}[1]{#1}
\newcolumntype{R}{>{\collectcell\TitleColumn}r<{\endcollectcell}}% works
\newcolumntype{T}{>{\collectcell\TitleColumn}X<{\endcollectcell}}% doesn't fail
\newcommand*{\TabularContent}{%
Label 1 Very Long& 62 & 79 \\
Label 2 Empty Row & \\
Label 3 Short& 732 & 31 \\
}
\begin{document}
\noindent
1. Column type \verb|R| (equivalent to centered \verb |r| column) works fine:
\par\smallskip\noindent
\begin{tabularx}{\linewidth}{@{}Rll@{}}
\TabularContent
\end{tabularx}
\bigskip
\noindent
2. Column type \verb|T| (equivalent to centered \verb |c| column) FAILS no longer
\par(commented out):
\par\smallskip\noindent
\begin{tabularx}{\linewidth}{@{}Tll@{}}%% <--- Now we got this to work
\TabularContent
\end{tabularx}
\bigskip
\noindent
3. Desired result of above \verb|T| column type is the \verb|X| column type::
\par\smallskip\noindent
\begin{tabularx}{\linewidth}{@{}Xll@{}}
\TabularContent
\end{tabularx}
\end{document}