![使用表格将表格内容居中对齐](https://linux22.com/image/400091/%E4%BD%BF%E7%94%A8%E8%A1%A8%E6%A0%BC%E5%B0%86%E8%A1%A8%E6%A0%BC%E5%86%85%E5%AE%B9%E5%B1%85%E4%B8%AD%E5%AF%B9%E9%BD%90.png)
我希望将第二列和第三列的长度相等并置于中心。
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}[!ht]
%\begin{tabularx}{\textwidth}{@{}Y*{3}{W}@{}}
\centering
\begin{tabular}{ccc}
\toprule
& \multicolumn{2}{c}{\textbf{Testing Testing Testin}} \\
& \multicolumn{2}{c}{\textbf{Making into Secondline (\%)}} \\
\cmidrule{2-3}
& \small {\textbf{AA}}
& \small {\textbf{BB}} \\
\midrule
Test 1 & 33 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
结果:
我希望将第二列和第三列的长度相等并置于中心。
我曾尝试过:
\begin{tabular}{ccc}
答案1
这是借助parbox
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}[!ht]
%\begin{tabularx}{\textwidth}{@{}Y*{3}{W}@{}}
\centering
\begin{tabular}{ccc}
\toprule
& \multicolumn{2}{c}{\textbf{Testing Testing Testin}} \\
& \multicolumn{2}{c}{\textbf{Making into Secondline (\%)}} \\
\cmidrule{2-3}
& {\parbox{3cm}{\small\hfil\textbf{AA}\hfill}}
& {\parbox{3cm}{\small\hfil\textbf{BB}\hfill}} \\
\midrule
Test 1 & 33 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案2
这是一个可能的解决方案,基于siunitx
来获得良好的数字对齐(居中右对齐)。S
列中的非数字必须用一对括号括起来并自动居中。此外,可以选择 S 列的宽度。此解决方案还依赖于,它允许在标准单元格中换行,以及和命令makecell
的参数的通用格式。\makecell
\thead
\documentclass{article}
\usepackage{array}
\usepackage{booktabs, makecell}
\renewcommand{\theadfont}{\small\bfseries}
\usepackage{siunitx}
\begin{document}
\begin{table}[!ht]
\centering
\sisetup{table-format=3.0, table-number-alignment=center, table-column-width=1.5cm}
\begin{tabular}{cSS}
\toprule
& \multicolumn{2}{c}{\small\bfseries Testing Testing Testin} \\
& \multicolumn{2}{c}{\thead{\makebox[0pt]{Making into Secondline}\\ (\%)}} \\
\cmidrule{2-3}
&{\small \textbf{AA}}
& {\small\textbf{BB}} \\
\midrule
Test 1 & 33 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案3
这样会使列呈“Z”形,居中对齐最长标题行的一半宽度。您只需告诉 LateX 该标题行的宽度是多少,在每个表格之前,宽度为\settowidth
:
\documentclass{article}
\usepackage{booktabs,array}
\newlength{\xxx} % suggestion: change "xxx" for something meaningful
\newcolumntype{Z}{>{\hfil}p{.5\xxx}}
\begin{document}
\settowidth{\xxx}{\textbf{Making into Secondline (\%)}}
\begin{tabular}{cZZ} \toprule
& \multicolumn{2}{c}{\textbf{Testing Testing Testin}} \\
& \multicolumn{2}{c}{\textbf{Making into Secondline (\%)}} \\\cmidrule{2-3}
& \small {\textbf{AA}} & \small {\textbf{BB}} \\\midrule
Test 1 & 33 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\
Test 2 & 57 & 73 \\\bottomrule
\end{tabular}
\end{document}