
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 tabularx
de X
apenas 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}
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 m
coluna em vez de uma p
coluna 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 makecell
pacote e seu \thead
comando:
\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}
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.