[Tex/LaTex] How to vertically (and horizontally) center text of cells using tabularx

horizontal alignmenttablestabularxvertical alignment

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} 

output

Best Answer

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} 

enter image description here

Related Question