我有这个代码:
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{center}
\begin{tabular}{c|lccc|c|}
& \multicolumn{5}{c}{\textbf{Political Party Preference}}\\\toprule
& & \textbf{Democrat} & \textbf{Republican} & \textbf{Independent} & \textbf{Total}\\\midrule
\textbf{Gender} & \textbf{Male} & 11 & 7 & 2 & 20\\
& \textbf{Female} & 7 & 8 & 1 & 16\\\midrule
& \textbf{Total} & 18 & 15 & 3 & 36\\\bottomrule
\end{tabular}
\end{center}
\end{document}
这使
但我想制作如下表格:
有什么建议么?
答案1
我建议您根本不要使用垂直规则(booktabs 包文档有一些支持此建议的论据);如果您添加垂直规则,它们将无法与包很好地交互booktabs
(正如您所注意到的),并且尝试使规则正常工作会破坏表格布局:
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
\begin{center}
\begin{tabular}{@{}c>{\bfseries}lcccc@{}}
\toprule
& \multicolumn{5}{c}{\textbf{Political Party Preference}} \\
& & \textbf{Democrat} & \textbf{Republican} & \textbf{Independent} &
\textbf{Total} \\
\cmidrule(r){1-2}\cmidrule(lr){3-3}\cmidrule(lr){4-4}\cmidrule(lr){5-5}\cmidrule(l){6-6}
\textbf{Gender} & Male & 11 & 7 & 2 & 20 \\
& Female & 7 & 8 & 1 & 16\\
\midrule
& Total & 18 & 15 & 3 & 36\\
\bottomrule
\end{tabular}
\end{center}
\end{document}
或者像这样:
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
\begin{center}
\begin{tabular}{@{}>{\bfseries}lcccc@{}}
\toprule
Gender & \multicolumn{4}{c}{\textbf{Political Party Preference}} \\
\cmidrule(l){2-5}
& \textbf{Democrat} & \textbf{Republican} & \textbf{Independent} &
\textbf{Total} \\
\cmidrule(r){1-1}\cmidrule(lr){2-2}\cmidrule(lr){3-3}\cmidrule(lr){4-4}\cmidrule(l){5-5}
Male & 11 & 7 & 2 & 20 \\
Female & 7 & 8 & 1 & 16\\
\midrule
Total & 18 & 15 & 3 & 36\\
\bottomrule
\end{tabular}
\end{center}
\end{document}
答案2
@user16203 您可以尝试以下代码:
\documentclass{article}
\usepackage{tabls,pxfonts}
\begin{document}
\begin{center}
\begin{tabular}{c|lccc|c|}
\multicolumn{1}{c}{} & \multicolumn{5}{c}{\textbf{Political Party Preference}} \\\cline{2-6}
& & \textsf{Democrat} & \textsf{Republican}
& \textsf{Independent} & \textsf{Total} \\ \cline{2-6}
\multicolumn{1}{c|}{\textbf{Gender}}& \textbf{Male} & 11 & 7 & 2 & 20 \\
& \textbf{Female} & 7 & 8 & 1 & 16\\ \cline{2-6}
& \textbf{Total} & 18 & 15 & 3 & 36\\ \cline{2-6}
\end{tabular}
\end{center}
\end{document}