Tabela: Largura automática da coluna centralizada verticalmente e horizontalmente

Tabela: Largura automática da coluna centralizada verticalmente e horizontalmente

Estou tentando encontrar um método que me permita ter colunas com largura automática que também sejam centralizadas nas direções horizontal e vertical, assim como o tipo de coluna tabularxde Xapenas centralizado.

Então aqui está o que tentei até agora:

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

insira a descrição da imagem aqui insira a descrição da imagem aqui

A segunda já é quase o que eu quero, só falta a centralização vertical da terceira coluna. Como consertar isto?

Responder1

para centralização vertical, você deseja uma mcoluna em vez de uma pcoluna atrásX

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

Responder2

Basta usar o makecellpacote e seu \theadcomando:

        \documentclass{article}
        \usepackage{tabularx,booktabs, makecell}
        \newcolumntype{Z}[0]{>{\centering\arraybackslash\let\newline\\\hspace{0pt}}X}%

        \begin{document}

        \begin{table}
        \begin{tabularx}{7cm}{l|X|X}
        &\thead{ long\\ heading 1} & \thead{short heading}\\
        \toprule
        row 1 & 2314 & 2134
        \end{tabularx}
        \end{table}

        \begin{table}
        \begin{tabularx}{7cm}{l|Z|Z}
        &\thead{long\\ heading 1} & \thead{short heading}\\
        \toprule
        row 1 & 2314 & 2134
        \end{tabularx}
        \end{table}

        \end{document} 

insira a descrição da imagem aqui

Observe que com makecell, você pode formatar facilmente o conteúdo das células principais. Deixe-me salientar que você não deve usar linhas verticais com booktabs: como você pode ver, elas não cortam as horizontais.

informação relacionada