¿Cómo alinear horizontalmente las filas entre columnas tipo X?

¿Cómo alinear horizontalmente las filas entre columnas tipo X?

Tengo una tabla con títulos cortos y largos. Estoy usando el tipo de columna Xdel tabularxpaquete. Los títulos están alineados en la parte superior, ¿cómo puedo alinear los títulos horizontalmente en el medio?

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

\begin{document}

\begin{table}[htb]
\centering
\caption{My caption}
\label{my-label}
\begin{tabularx}{\linewidth}{*2{>{\centering\arraybackslash}X}}
\toprule
\textbf{Short heading\newline(units)} &
\textbf{This is a long long, very very long heading\newline(units)}\\%
\midrule
416,09 & 194,30 \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

ingrese la descripción de la imagen aquí

Respuesta1

Suponiendo que desea lograr entradas centradas horizontal y verticalmente, puede utilizar la siguiente redefinición de Xcolumnas de tipo. Normalmente se derivan de pcolumnas de tipo alineadas en la parte superior. Con \renewcommand{\tabularxcolumn}[1]{m{#1}}podemos cambiar eso para que xlas columnas de tipo se deriven de las mcolumnas de tipo centradas verticalmente.

ingrese la descripción de la imagen aquí

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

\renewcommand{\tabularxcolumn}[1]{m{#1}}

\begin{document}

\begin{table}[htb]
\centering
\caption{My caption}
\label{my-label}
\begin{tabularx}{\linewidth}{*2{>{\centering\arraybackslash}X}}
\toprule
\textbf{Short heading\newline(units)} &
\textbf{This is a long long, very very long heading\newline(units)}\\%
\midrule
416,09 & 194,30 \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

Si desea utilizar Xcolumnas tipográficas centradas horizontal y verticalmente con más frecuencia en todo el documento, también puede agregarlas \newcolumntype{Y}{>{\centering\arraybackslash}X}al preámbulo y usar \begin{tabularx}{\linewidth}{*2{Y}}.


Si desea mantener la definición original de Xcolumnas de tipo alineadas superiormente y tener un tipo de columna separada derivada y centrada verticalmente, es posible que desee echar un vistazo aCopie la columna tabularx X como una nueva columna centrada verticalmente


Si solo desea cambiar la alineación vertical de una sola tabla, puede utilizar la siguiente redefinición local de la Xcolumna de tipo. Aquí he añadido \renewcommand{\tabularxcolumn}[1]{m{#1}}justo después \begin{table}de la tabla correspondiente.

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


\begin{document}

\begin{table}[htb]
\centering
\caption{My caption}
\label{my-label}
\begin{tabularx}{\linewidth}{*2{>{\centering\arraybackslash}X}}
\toprule
\textbf{Short heading\newline(units)} &
\textbf{This is a long long, very very long heading\newline(units)}\\%
\midrule
416,09 & 194,30 \\
\bottomrule
\end{tabularx}
\end{table}

\begin{table}[htb]
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\centering
\caption{My caption}
\label{my-label}
\begin{tabularx}{\linewidth}{*2{>{\centering\arraybackslash}X}}
\toprule
\textbf{Short heading\newline(units)} &
\textbf{This is a long long, very very long heading\newline(units)}\\%
\midrule
416,09 & 194,30 \\
\bottomrule
\end{tabularx}
\end{table}

\begin{table}[htb]
\centering
\caption{My caption}
\label{my-label}
\begin{tabularx}{\linewidth}{*2{>{\centering\arraybackslash}X}}
\toprule
\textbf{Short heading\newline(units)} &
\textbf{This is a long long, very very long heading\newline(units)}\\%
\midrule
416,09 & 194,30 \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

información relacionada