我有一張帶有短標題和長標題的表格。我正在使用包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}