Copy tabularx X column as new vertically centered column

My question is very similar to Vertical alignmnent in tabularx X column type and there seems to be many similar questions so hopefully I haven't missed the answer somewhere.

However, I would like to create a new column that is a copy of tabularx X column, with the difference being the new column Y is an m column compared to a p column (which X is by default as described in the docs).

\tabularxcolumn - The default denition of X is p{#1}.

\def\tabularxcolumn#1{p{#1}}


So I would like to define a new column type like

\newcolumntype{Y}{>{\centering\arraybackslash}X}


and then convert the Y column type to m instead of p as it would currently be. That way I can leave X as it's original definition. If I use

\renewcommand{\tabularxcolumn}[1]{>{\small}m{#1}}


Then X is changed to an m column which is not desired. I would need something like

\renewcommand{Y}[1]{>{\small}m{#1}}


But that doesn't work.

Thanks for any help,

• Do you want/need both X and Y columns in a single table?
– Werner
Oct 11, 2018 at 18:00
• Potentially, I would like to be able to have a p type X column and a m type X column if possible. Oct 11, 2018 at 18:02

you need to patch in a second X-like columntype, this just duplicates the definition of X so you can separately specify \tabularxcolumn for X and \tabularxycolumn for Y

\documentclass[a4paper]{article}
\usepackage{etoolbox,tabularx}
\tracingtabularx
\makeatletter
\newcolumntype{Y}{}
\def\tabularxycolumn#1{m{#1}}
\def\TX@newycol{\newcol@{Y}[0]}
\patchcmd\TX@endtabularx
{\expandafter\TX@newcol}%
{\expandafter\TX@newycol\expandafter{\tabularxycolumn{\TX@col@width}}%
\expandafter\TX@newcol}
{}
{}
\patchcmd\TX@endtabularx
{\def\NC@rewrite@X}%
{\def\NC@rewrite@Y{\NC@rewrite@X}%
\def\NC@rewrite@X}
{}
{}
\makeatother
\begin{document}

\begin{tabularx}{6cm}{XXc}
aa aaa aaa aaa aaa aaa&
bb bb bb bb bb bb bb bb bb bb bb bb bb bb b &
aa aaa
\end{tabularx}
\begin{tabularx}{6cm}{XYc}
aa aaa aaa aaa aaa aaa&
bb bb bb bb bb bb bb bb bb bb bb bb bb bb b &
aa aaa
\end{tabularx}
\begin{tabularx}{6cm}{YYc}
aa aaa aaa aaa aaa aaa&
bb bb bb bb bb bb bb bb bb bb bb bb bb bb b &
aa aaa
\end{tabularx}
\end{document}

• I thought it worked but I was looking at the PDF (which compiled fine) but there were errors. I am using > pdfLaTeX Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6500 64-bit). My original use was in knitr, but it failed there, and then I went to TexStudio, but it failed there as well. I started with a blank tex document and copied and pasted your code. Any thoughts? Thanks! Oct 19, 2018 at 23:02
• @Prevost sorry I had omitted another place to patch in the Y version of X columns, the original wasn't counting Y columns in its count of the number of active X columns. code and result image updated Oct 20, 2018 at 0:02
• That's great compiles no errors! Could that code be theoretically created from just your docs? Oct 20, 2018 at 0:19
• @Prevost not from the user level documentation, it's definitely an extension of the design not just "using tabularx". Perhaps it could be derived from the documented code, although actually I only looked at the docstripped code with comments removed, perhaps that's why I missed one place the first time:-) Oct 20, 2018 at 8:12