答案1
一些建议和意见:
您的屏幕截图中显示的桌子非常“传统”,也就是说,相当拥挤、不美观且不吸引人。整体印象就像监狱牢房的窗户:有很多水平和垂直的栅栏。它的“外观”显示在下面的第一个表格中。
请考虑让表格看起来更开放。一个很好的方法是 (a) 省略所有垂直线和 (b) 使用较少但间距适当的水平线。请查看下面第二个表格中的结果。
作为第二种方法的变体,
tabular*
可以使用环境来代替环境tabular
,以允许预先指定表格的整体宽度。通常(但不一定)将整体宽度设置为\textwidth
,即文本块的宽度。
最后一点:这三种方法实际上都不能保证表格适合文本块。如果表格有很多列,则尤其容易出现此问题。如果您遇到此问题,则应发布一个新查询,在其中列出您迄今为止尝试过的方法。
\documentclass{article}
\usepackage{array,multirow}
\usepackage{booktabs} % for \toprule, \midrule, \cmidrule, and \bottomrule macros
\usepackage{newtxtext} % optional: load Times Roman clone text font
\begin{document}
\begin{table}
\centering
%% A. For the "traditional", i.e., cramped and rather unattractive "look":
\setlength\extrarowheight{2pt} % optional
\begin{tabular}{|l|*{8}{c|}}
\hline
\multirow{2}{*}{\textbf{Mapping Method}} &
\multicolumn{4}{c|}{\textbf{SVM (\%)}} &
\multicolumn{4}{c|}{\textbf{kNN (\%)}} \\
\cline{2-9}
& \textbf{AC} & \textbf{SP} & \textbf{SN} & \textbf{AUC}
& \textbf{AC} & \textbf{SP} & \textbf{SN} & \textbf{AUC} \\
\hline
\dots & & & & & & & & \\ \hline
\dots & & & & & & & & \\ \hline
\dots & & & & & & & & \\ \hline
\dots & & & & & & & & \\
\hline
\end{tabular}
\bigskip\bigskip
%% B. A much more open "look" (no vertical rules, few but well-spaced horizontal rules)
\setlength\extrarowheight{0pt} % reset to default value
\begin{tabular}{@{}l*{8}{c}@{}}
\toprule
\textbf{Mapping Method} &
\multicolumn{4}{c}{\textbf{SVM (\%)}} &
\multicolumn{4}{c@{}}{\textbf{kNN (\%)}} \\
\cmidrule(lr){2-5} \cmidrule(l){6-9}
& \textbf{AC} & \textbf{SP} & \textbf{SN} & \textbf{AUC}
& \textbf{AC} & \textbf{SP} & \textbf{SN} & \textbf{AUC} \\
\midrule
\dots & & & & & & & & \\
\dots & & & & & & & & \\
\dots & & & & & & & & \\
\dots & & & & & & & & \\
\bottomrule
\end{tabular}
\bigskip\bigskip
%% C. Same as "B" , but with overall width set to \textwidth
\setlength{\tabcolsep}{0pt}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{8}{c} }
\toprule
\textbf{Mapping Method} &
\multicolumn{4}{c}{\textbf{SVM (\%)}} &
\multicolumn{4}{c}{\textbf{kNN (\%)}} \\
\cmidrule{2-5} \cmidrule{6-9}
& \textbf{AC} & \textbf{SP} & \textbf{SN} & \textbf{AUC}
& \textbf{AC} & \textbf{SP} & \textbf{SN} & \textbf{AUC} \\
\midrule
\dots & & & & & & & & \\
\dots & & & & & & & & \\
\dots & & & & & & & & \\ %\hline
\dots & & & & & & & & \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}
答案2
以下解决方案最初由@Mico 提出https://tex.stackexchange.com/a/192332/197451,应该适合你的目的:
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{multirow,tabularx}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\renewcommand{\arraystretch}{2}
\begin{document}
\emph{Original form: All columns are equally wide.}
\noindent
\begin{tabularx}{\textwidth}{|*{4}{Y|}}
\hline
\multirow{2}{*}{State of Health}
&\multicolumn{2}{c|}{Fasting Value}&After Eating\\
\cline{2-4}
&Minimum &Maximum &2 hours after eating\\
\hline
Healthy &70 &100 &Less than 140\\
\hline
Pre-Diabetes &101 &126 &140 to 200\\
\hline
Diabetes &More than 126 &N/A &More than 200\\
\hline
\end{tabularx}
\bigskip
\emph{Modified form: Columns 1 and 4 are 50\% wider than columns 2 and 3.}
\smallskip\noindent
\begin{tabularx}{\textwidth}{|
>{\hsize=1.2\hsize}Y|
>{\hsize=0.8\hsize}Y|
>{\hsize=0.8\hsize}Y|
>{\hsize=1.2\hsize}Y|}
\hline
\multirow{2}{*}{State of Health}
&\multicolumn{2}{c|}{Fasting Value}&After Eating\\
\cline{2-4}
&Minimum &Maximum &2 hours after eating\\
\hline
Healthy &70 &100 &Less than 140\\
\hline
Pre-Diabetes &101 &126 &140 to 200\\
\hline
Diabetes &More than 126 &N/A &More than 200\\
\hline
\end{tabularx}
\end{document}