如何水平对齐 X 型列之间的行?

如何水平对齐 X 型列之间的行?

X我有一张包含短标题和长标题的表格。我使用包中的列类型tabularx。标题与顶部对齐,我如何才能将标题水平对齐在中间?

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

在此处输入图片描述

答案1

假设您想要实现水平和垂直居中的条目,您可以使用以下X类型列的重新定义。通常,它们源自p顶部对齐的类型列。\renewcommand{\tabularxcolumn}[1]{m{#1}}我们可以对其进行更改,以便x类型列源自垂直居中的m类型列。

在此处输入图片描述

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

如果您想X在整个文档中更频繁地使用水平和垂直居中的类型列,您还可以添加\newcolumntype{Y}{>{\centering\arraybackslash}X}到序言中并使用\begin{tabularx}{\linewidth}{*2{Y}}


如果你希望保留顶部对齐X类型列的原始定义,并拥有单独的派生和垂直居中的列类型,你可能需要查看将 tabularx X 列复制为新的垂直居中列


如果您只想更改单个表的垂直对齐方式,则可以使用以下类型列的局部重新定义。这里我已在相应表的后面X添加了。\renewcommand{\tabularxcolumn}[1]{m{#1}}\begin{table}

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

相关内容