[Tex/LaTex] Table: Column auto-width vertically and horizontally centered

tables

I'm trying to find a method which allows me to have columns with automatic width which are also centered in horizontal and vertical direction, just like tabularx's X columntype just centered.

So here is was I tried so far:

\documentclass{article}
\usepackage{tabularx,booktabs}
\newcolumntype{Z}[0]{>{\centering\arraybackslash\let\newline\\\hspace{0pt}}X}%
\begin{document}
\begin{table}
\begin{tabularx}{7cm}{l|X|X}
& long\newline heading 1 & short heading\\
\toprule
row 1 & 2314 & 2134
\end{tabularx}
\end{table}
\begin{table}
\begin{tabularx}{7cm}{l|Z|Z}
& long\linebreak heading 1 & short heading\\
\toprule
row 1 & 2314 & 2134
\end{tabularx}
\end{table}
\end{document}

enter image description here
enter image description here

The second is already almost what I want, just the vertical centering of the third column is missing. How to fix this?

Best Answer

for vertical centering you want an m column rather than a p column to sit behind X

\documentclass{article}
\usepackage{tabularx,booktabs}
\newcolumntype{Z}[0]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}X}%
\renewcommand\tabularxcolumn[1]{m{#1}}
\begin{document}
\begin{table}
\begin{tabularx}{7cm}{l|X|X}
& long\newline heading 1 & short heading\\
\toprule
row 1 & 2314 & 2134
\end{tabularx}
\end{table}
\begin{table}
\begin{tabularx}{7cm}{l|Z|Z}
& long\linebreak heading 1 & short heading\\
\toprule
row 1 & 2314 & 2134
\end{tabularx}
\end{table}
\end{document}
Related Question