我有一张表格,其中一列标题很长,因此需要将其分成几行。当我尝试这样做时,列中的条目都很短(实际上它们都是单个字符),而且都是左对齐的。这看起来很奇怪。
\documentclass{amsart}
\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}
\begin{table}[htbp]
\centering
\caption{ }
\def\tabularxcolumn#1{m{#1}}
\begin{tabularx}{0.5\textwidth}{cc p{0.23\linewidth}}
\toprule
{A} & {B}& {Blah blah longword shortword}\\
Moon& Earth& 3 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
如何才能使条目居中而不拉长列标题?
答案1
这里有三种不同的方法可以在较长的列标题内实现换行,同时保持列的内容水平居中:
\documentclass{amsart}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{makecell}
\renewcommand{\theadfont}{\normalsize}
\begin{document}
\begin{table}[htbp]
\centering
\caption{Regular tabular with makecell's thead command, manual linebreak}
\begin{tabular}{ccc}
\toprule
\thead{A} & \thead{B}&\thead {Blah blah longword\\ shortword}\\
Moon& Earth& 3 \\
\bottomrule
\end{tabular}
\end{table}
\begin{table}[htbp]
\centering
\caption{Regular tabular and centered m type column, automatic line break}
\begin{tabular}{cc >{\centering\arraybackslash}m{0.23\linewidth}}
\toprule
{A} & {B}& {Blah blah longword shortword}\\
Moon& Earth& 3 \\
\bottomrule
\end{tabular}
\end{table}
\begin{table}[htbp]
\centering
\caption{Tabularx and centered X type column, automatic line break}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\begin{tabularx}{0.5\textwidth}{cc >{\centering\arraybackslash}X}
\toprule
{A} & {B}& {Blah blah longword shortword}\\
Moon& Earth& 3 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
左对齐列标题:
\documentclass{amsart}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{makecell}
\renewcommand{\theadfont}{\normalsize}
\begin{document}
\begin{table}[htbp]
\centering
\caption{Regular tabular with makecell's thead command, manual linebreak}
\begin{tabular}{ccc}
\toprule
\thead{A} & \thead{B}&\thead[l]{Blah blah longword\\ shortword}\\
Moon& Earth& 3 \\
\bottomrule
\end{tabular}
\end{table}
\begin{table}[htbp]
\centering
\caption{Regular tabular and centered m type column, automatic line break}
\begin{tabular}{ccc}
\toprule
{A} & {B}& \multicolumn{1}{>{\raggedright\arraybackslash}m{0.25\linewidth}}{Blah blah longword shortword}\\
Moon& Earth& 3 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}