我希望將第二列和第三列等長並放在中間。
\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
: width :
\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}