[Tex/LaTex] Vertically centered text in tabularx (again)

line-breakingtabularxvertical alignment

I have seen Table with 100 % width and vertical / horizontal alignment, Vertically center text in tabularx table, Vertical alignment in tabularx X column type – and I'm trying what they suggest, but it doesn't work.

Consider this MWE:

\documentclass{article}
\usepackage{array,booktabs}
\usepackage{tabularx}
\usepackage{ragged2e}
\usepackage{hhline}
\newcolumntype{x}[1]
{>{\raggedright}p{#1}}
\newcolumntype{z}[1]
{>{\centering}p{#1}}
\newcommand{\tn}{\tabularnewline}

\renewcommand\tabularxcolumn[1]{>{\Centering}m{#1}}  %% COMMENT

\begin{document}

\begin{table}[!h]%t1
\caption{A rather long explanation of what this table is supposed to be}
\small%
{%
\begin{tabularx}{\textwidth}%
  {|x{2.6cm}|X|}  %% COMMENT
%   {|x{2.6cm}|>{\centering}X|} %% UNCOMMENT
\hline
Something something & something else \tn
\hhline{|=|=|}
\textbf{Item one} \\ {\small\it item ref 1} &%
  param 01 / param 02 \\/ param3\tn
\hline
\textbf{Item two} \\ {\small\it item ref 2} &%
  param 03 / param 04 \\/ param5\tn
\hline
\textbf{Item two} \\ {\small\it item ref 2} &%
  param 03 / param 04 / param5\tn
\hline
\end{tabularx}}
\label{tab:sometable}
\end{table}

\end{document}

As is, this produces:

testc2.png

… so clearly something is broken (the \\ are seen as new table lines, I guess)?

But if you uncomment/comment the lines as marked with %% in the MWE, it compiles almost fine:

testc1.png

… except there is no vertical centering – as evidenced by the 3rd row, 2nd column.

So how can I have a column that stretches, that allows line breaking inside a cell – and centers the text vertically?

Best Answer

\Centering tries to be kind and avoids redefining \\ so \\ is safe to use in the first column but not the second. this just tells \Centering not to worry about the tabular version of \\. Unrelated, but don't use [h!].

\documentclass{article}
\usepackage{array,booktabs}
\usepackage{tabularx}
\usepackage{ragged2e}
\usepackage{hhline}
\newcolumntype{x}[1]
{>{\raggedright}p{#1}}
\newcolumntype{z}[1]
{>{\centering}p{#1}}
\newcommand{\tn}{\tabularnewline}

\makeatletter
\renewcommand\tabularxcolumn[1]{>{%
\let\@raggedtwoe@savedcr\\%
\Centering}m{#1}}  %% COMMENT
\makeatother

\begin{document}

\begin{table}[htp]%t1
\caption{A rather long explanation of what this table is supposed to be}
\small%
{%
\begin{tabularx}{\textwidth}%
  {|x{2.6cm}|X|}  %% COMMENT
%   {|x{2.6cm}|>{\centering}X|} %% UNCOMMENT
\hline
Something something & something else \tn
\hhline{|=|=|}
\textbf{Item one} \\ {\small\it item ref 1} &%
  param 01 / param 02 {\show\\}\\/ param3\tn
\hline
\textbf{Item two} \\ {\small\it item ref 2} &%
  param 03 / param 04 \\/ param5\tn
\hline
\textbf{Item two} \\ {\small\it item ref 2} &%
  param 03 / param 04 / param5\tn
\hline
\end{tabularx}}
\label{tab:sometable}
\end{table}

\end{document}
Related Question