在 Latex 中排版整洁的表格

在 Latex 中排版整洁的表格

我如何排版下表以使其看起来整洁且专业?

\begin{table}[!h]
\caption{A summary of classification results on the free flowing traffic dataset}
\begin{tabular}{|p{1.7 cm}|p{1.7 cm}|p{1.7 cm}|p{1.7 cm}|p{1.5 cm}|p{1.5 cm}|p{1.5 cm}|p{1.6 cm}|p{1.5 cm}|}
\hline
\textbf{Total no. of vehicles used for classification} & \textbf{No. of small vehicles used for classification} & \textbf{No. of medium vehicles used for classification} & \textbf{No. of large vehicles used for classification} & \multicolumn{5}{|c|}{\textbf{Classification accuracy}}\\ 
\cline{5-9}
& & & & K-nearest neighbor classifier & Nearest centroid classifier & Naïve Bayes Classifier & Multilayer neural network classifier & Ensemble classifier\\
\hline
952 & 594 & 199 & 159 & 90.9\% & 89.4\% & 91.1\% & 89.3\% & 91.8\% \\
\hline
\end{tabular}
\label{tab 4.2} 

答案1

我建议你从 (a) 为标题提供更多结构和 (b) 消除重复元素开始。另外,你可能要考虑以横向模式排版表格,尤其是因为表格有九列,每列都包含大量信息。

这是我的建议,它利用了包的规则绘制宏booktabs

在此处输入图片描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{rotating,caption,tabularx,booktabs}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
\begin{sidewaystable}[!h]
\setlength\tabcolsep{4pt} % default: 6pt

\caption{A summary of classification results on the free flowing traffic dataset} \label{tab 4.2}

\begin{tabularx}{\textwidth}{@{} c *{7}{C} c @{}}
\toprule
\multicolumn{4}{@{}c}{No.\ of vehicles used for classication, by size} &
\multicolumn{5}{c@{}}{Classification accuracy, by classifier}\\
\cmidrule(r){1-4} \cmidrule(l){5-9}
Total & Small & Medium & Large & 
$K$-nearest neighbor & Nearest centroid & Naïve Bayes & Multilayer neural network & Ensemble\\
\midrule
952 & 594 & 199 & 159 & 90.9\% & 89.4\% & 91.1\% & 89.3\% & 91.8\% \\
\bottomrule
\end{tabularx}

\end{sidewaystable}
\end{document}

附录:上述建议基于表格最终将包含更多行的假设。如果情况并非如此,您可能需要考虑将表格分成两部分,每部分仅包含两列。通过这样的设置,就无需在横向模式下渲染材料。

在此处输入图片描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{caption,booktabs}

\begin{document}
\begin{table}[!h]
\setlength\tabcolsep{4pt} % default: 6pt

\caption{Summary of classification results on the free flowing traffic dataset} \label{tab 4.2}
\centering
\begin{tabular}{@{}lr@{}}
\toprule
\multicolumn{2}{@{}l@{}}{No.\ of vehicles used for}\\ \multicolumn{2}{@{}l@{}}{classication, by size} \\[0.25ex]
\midrule
Small  & 594 \\
Medium & 199 \\
Large  & 159 \\[1ex]
Total  & 952 \\
\bottomrule
\end{tabular}

\bigskip
\begin{tabular}{@{}lr@{}}
\toprule
\multicolumn{2}{@{}l@{}}{Classification accuracy, by classifier}\\[0.25ex]
\midrule
$K$-nearest neighbor & 90.9\% \\
Nearest centroid     & 89.4\% \\ 
Naïve Bayes          & 91.1\% \\
Multilayer neural network & 89.3\% \\[1ex]
Ensemble             & 91.8\% \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

答案2

也许像这样?我已经添加了

  • booktabs提供\toprule\midrule\bottomrule的包\cmidrule{}
  •  为环境tabularx提供支持多行内容的列的包(严格来说,在我删除了单元格中的所有解释性文字后就没有必要了,tabularxX
  • multirow中稍微水平对齐了车辆变量,
  • siunitx用于正确格式化单位的包(在您的情况下为百分比)
  • caption对标题进行一些样式化,并在(现在相当庞大的)标题和表格之间提供更好的间距,并且
  • geometry,因为我不知道你使用什么样的页面布局,并且表格需要更多的页面宽度:p

梅威瑟:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{booktabs,tabularx,siunitx,multirow}
\usepackage[hang,bf,small]{caption}
\usepackage{geometry}

\newcommand{\totNumV}{N_{V,\mathrm{tot.}}}
\newcommand{\smallV}{N_{V,\mathrm{s}}}
\newcommand{\medV}{N_{V,\mathrm{m}}}
\newcommand{\largeV}{N_{V,\mathrm{l}}}

\begin{document}

\begin{table}[!h]
  \centering
  \caption{A summary of classification results on the free flowing traffic dataset, where $\totNumV$ is the total number of vehicles used for classification, $\smallV{}$, $\medV{}$ and $\largeV$ are the number of small, medium and large vehicles used for classification, respectively. $K$, $C$, $\mathit{NBC}$, $M$ and $E$ are the K-nearest neighbor classifier,  Nearest centroid classifier, Naïve Bayes Classifier, Multilayer neural network classifier and Ensemble classifier, respectively. }
  \label{tab 4.2}
  \begin{tabularx}{\textwidth}{*{9}{X}}
  \toprule
  \multirow{2}{*}{$\totNumV{}$} & \multirow{2}{*}{$\smallV{}$} & \multirow{2}{*}{$\medV{}$} & \multirow{2}{*}{$\largeV{}$} & \multicolumn{5}{c}{\textbf{Classification accuracy}}\\  
  \cmidrule{5-9}
  & & & & $K$ & $C$ & $\mathit{NBC}$ & $M$ & $E$ \\
  \midrule
  952 & 594 & 199 & 159 & \SI{90.9}{\percent} & \SI{89.4}{\percent} & \SI{91.1}{\percent} & \SI{89.3}{\percent} & \SI{91.8}{\percent} \\
  \bottomrule
  \end{tabularx}
\end{table}


\end{document}

生产

在此处输入图片描述

相关内容