2

I would like to use the X column type from the tabularx with the collcell package.

In the MWE below, I define:

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

where the T applies the X column type (does not compile and thus is commented out in the MWE) and the R (for illustrative purposes) applies the r column type (which works).

The output has three tables:

  1. R column type (to ensure syntax is correct)
  2. Desired T column type (commented out)
  3. Desired Result of T column type

The error message if table 2 (which uses the T columntype) is uncommented is:

Package array Error: Illegal pream-token (X): `c' used.

enter image description here

Notes:

  • MWE is greatly simplified such that there is no need to actually use collcell here, but needed in actual use case where the \TitleColumn and \ColumnL macros have more code.

Code:

\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}
2

Removing the braces around the X and it works:

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.