使用带有 tabularx X 列类型的 collcell 包

使用带有 tabularx X 列类型的 collcell 包

我想使用包X中的列类型。tabularxcollcell

在下面的 MWE 中,我定义:

\newcolumntype{R}{>{\collectcell\TitleColumn}{r}<{\endcollectcell}}% works
\newcolumntype{T}{>{\collectcell\TitleColumn}{X}<{\endcollectcell}}% fails

其中T应用X列类型(无法编译因此在 MWE 中被注释掉)并且R(为了说明目的)应用r列类型(作品)。

输出有三个表:

  1. R列类型(确保语法正确)
  2. 所需的T列类型(注释掉)
  3. 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}

相关内容