# Vertically centered text in tabularx (again)?

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:

... 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:

... 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?

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

• I would change the definition of x to \newcolumntype{x}[1]{>{\raggedright}m{#1}} to get the cells vertically centered. – esdd May 10 '15 at 20:00
• Many thanks @DavidCarlisle - just tried your code as is, but the cells in second column are still not vertically centered (img); by adding the command by @esdd right before the \makeatletter, I finally get vertically centered columns (img2). Thanks again both - cheers! – sdaau May 10 '15 at 20:09
• @sdaau oh sorry, was concentrating on the | the m column was centering the second column, so aligning its centre with the reference point of the cell in first column (its first line), using m in both puts the reference points of both cells in their centre. – David Carlisle May 10 '15 at 20:22
• @sdaau ragged2e is unrelated to vertical centering. – David Carlisle May 10 '15 at 20:23
• @sdaau it only gives that warning if you define x twice. It does not by default or in this MWE – David Carlisle May 10 '15 at 20:26

A variant using makecell to have vertically centred cells with line breaks, and cellspace to have less tight vertical spacing between rows:

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

\renewcommand\tabularxcolumn[1]{>{\centering\arraybackslash}m{#1}} %% COMMENT
\usepackage{makecell}
\renewcommand\cellalign{lc}
\usepackage{cellspace}
\setlength\cellspacetoplimit{2pt}
\setlength\cellspacebottomlimit{2pt}
\usepackage{caption}

\begin{document}

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

\end{document}