# How to vertically (and horizontally) center text of cells using tabularx?

My question is very similar to this one: previous question but when I vary the example a little (as below), my cells are no longer vertically aligned

\documentclass[12pt]{article}

\usepackage{tabularx,booktabs}

\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{Z}[0]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}X}%
\newcolumntype{s}[0]{>{\centering\arraybackslash\hsize=.4\hsize}Z}%
\newcolumntype{n}[0]{>{\centering\arraybackslash\hsize=.8\hsize}Z}%

\begin{document}

\begin{table}
\begin{tabularx}{7cm}{s|Z|s|n}
& really really really long\linebreak heading 1 & very very narrow column & \\
\toprule
row 1 & 2314 & 2134 & medium medium column \\ \hline
row 1 & 2314 & 2134 & medium medium  column
\end{tabularx}
\end{table}

\end{document}


• Your column widths are inconsistent (and lead to underfull box warnings) you have 4 X columns so the total width has to be 4\hsize but you have 2.6\hsize: tabularx can not do anything sensible with such a specification, anything it does is accidental:-) – David Carlisle Jun 27 '16 at 23:08

Beware that for resized X cells, the coefficients are like barycentric coordinates and their sum has to be equal to the columns number.

Also, I managed horizontal rules meet vertical rules, and set all rules (vertical as well as horizontal rules but one) to have the width of \midrules, and I simplified your code.

\documentclass[12pt]{article}

\usepackage{tabularx,booktabs}

\renewcommand\tabularxcolumn[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{Z}[0]{>{\hsize=1.55\hsize}X}%
\newcolumntype{s}[0]{>{\hsize=.6\hsize}X}%
\newcolumntype{n}[0]{>{\centering\arraybackslash\hsize=1.25\hsize}X}%

\begin{document}

\begin{table}
\setlength\aboverulesep{0pt} \setlength\belowrulesep{0pt}
\setlength\extrarowheight{2pt}
\begin{tabularx}{7cm}{s!{\vrule width\lightrulewidth}Z!{\vrule width\lightrulewidth}s!{\vrule width\lightrulewidth}n}
& really really really long\linebreak heading 1 & very very narrow column & \\
\toprule
row 1 & 2314 & 2134 & medium medium column \\ \midrule
row 1 & 2314 & 2134 & medium medium column
\end{tabularx}
\end{table}

\end{document}


Try

\documentclass[12pt]{article}
\usepackage{booktabs,tabularx}

\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{Z}[0]{>{\centering\arraybackslash}X}%
\newcolumntype{s}[0]{>{\hsize=.4\hsize}Z}%
\newcolumntype{n}[0]{>{\hsize=.8\hsize}Z}%

\begin{document}
\begin{table}
\begin{tabularx}{7cm}{s|Z|s|n}
& really really really long heading 1 & very very narrow column & \tabularnewline
\toprule
row 1 & 2314 & 2134 & medium medium column \\
\hline
row 1 & 2314 & 2134 & medium medium  column
\end{tabularx}
\end{table}
\end{document}


This code gives:

As you see, the above code is quite simplified your code. Omit are all surplus \centering in your column type definition (in one place it is enough) and also is omit redefinition for \arraybackslash and \hspace{0pt} which seem to be one of causes of your problem (for second one see comment of David Carlisle).

• see note about column widths on the question – David Carlisle Jun 27 '16 at 23:09