如何优化/减少表格每个单元格的空间?

如何优化/减少表格每个单元格的空间?

我为表格编写了以下代码:

\documentclass[12pt,a4paper,portrait]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amssymb}
\usepackage[margin=2cm]{geometry}
\usepackage[paper=portrait,pagesize]{typearea}
\usepackage{mathtools}% http://ctan.org/pkg/mathtools
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{float}
    
    \begin{center}
    {\renewcommand{\arraystretch}{3}%
    \begin{tabular}{ |c|c|c|c|c|c|c|c|c| } 
    \hline
    Accuracy\\
     \& Training Time\\
      (mins) & IEMG & MAV & MAV1 & VAR & RMS & DASDV & AAC & WL \\
    \hline
    
    \multirow{1}{4em}{Stacking Models\\
    (Random Forest,\\
    KNN, SVM) } & 97.92\% & 97.92\% &  97.92\%  & 97.08\% & 97.92\% & 96.67\% & 96.67\% & 96.67\%  \\ 
    & 14.82 & 15.96 & 16.51 & 15.3 & 15.18 & 14.85 & 15.77 & 15.6 \\ 
    \hline
    
    \multirow{1}{4em}{Stacking Models\\
    (SVM)} & 97.50\% & 97.50\% &  97.08\%  & 96.67\% & 97.50\% & 96.67\% & 97.08\% & 96.67\%  \\ 
    & 2.02 & 1.13 & 1.13 & 2.13 & 2.07 & 2.2 & 2.23 & 2.21 \\ 
    \hline
    
    \multirow{1}{4em}{Bagging\\
    (Decision\\
    Tree)} & 97.08\% & 97.08\% &  94.58\%  & 96.25\% & 96.25\% & 95.42\% & 94.58\% & 94.58\%  \\ 
    & 39.11 & 40.94 & 44.53 & 39.64 & 40.04 & 39.65 & 39.87 & 40.9 \\ 
    \hline
    
    \multirow{1}{4em}{Bagging\\
    (SVM)} & 97.92\% & 97.92\% & 97.50\%  & 96.67\% & 98.33\% & 96.67\% & 96.67\% & 96.25\%  \\ 
    & 2.78 & 0.99 & 1.34 & 2.16 & 1.29 & 1.28 & 1.31 & 8.33 \\ 
    \hline
    
    \multirow{1}{4em}{XGBoost\\
    (GBTree)} & 94.58\% & 94.58\% & 92.92\%  & 93.75\% & 93.75\% & 93.33\% & 95\% & 95\%  \\ 
    & 1.66 & 1.7 & 1.68 & 1.68 & 1.69 & 1.76 & 1.75 & 1.75 \\ 
    \hline
    
    \multirow{1}{4em}{XGBoost\\
    (GBLinear)} & 98.33\% & 98.33\% & 98.33\%  & 97.92\% & 98.33\% & 95.83\% & 96.67\% & 97.92\%  \\ 
    & 3.46 & 3.57& 3.61 & 3.51 & 3.58 & 3.45 & 3.44 & 3.61 \\ 
    \hline
    
    \end{tabular}}
    \end{center}

输出 :

在此处输入图片描述

我想要 :

  1. 减少/优化每个单元的空间。
  2. 将表格对齐到页面中间。
  3. 更正表头中缺失的垂直线。

编辑

嗨,Leandriis,

我收到以下构建错误:

在此处输入图片描述

一个要求 :

您能否将表格格式中的分类器列做得像这样:

在此处输入图片描述

答案1

根据booktabs您已加载的包,布局略有不同,字体较小,tabular*并使表格适合可用空间。

在此处输入图片描述

\documentclass[12pt,a4paper]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{booktabs}
\usepackage{multirow}

\begin{document}

\begin{table}
\caption{Caption text here}
\label{teb:example}
\small
\setlength{\tabcolsep}{0pt}
\begin{tabular*}{\textwidth}{@{\hspace{5pt}} p{3cm}@{\extracolsep{\fill}}cccccccc } 
\toprule
& \multicolumn{8}{c}{Accuracy \& Training Time (mins)}\\
\cmidrule{2-9} 
& IEMG & MAV & MAV1 & VAR & RMS & DASDV & AAC & WL \\
\midrule
\multicolumn{9}{@{}l}{Stacking Models:}\\

\multirow{2}{=}{Random Forest, KNN, SVM} 
& 97.92\% & 97.92\% &  97.92\%  & 97.08\% & 97.92\% & 96.67\% & 96.67\% & 96.67\%  \\ 
& 14.82 & 15.96 & 16.51 & 15.3 & 15.18 & 14.85 & 15.77 & 15.6 \\ 
\cmidrule{2-9}

\multirow{2}{=}{SVM} 
& 97.50\% & 97.50\% &  97.08\%  & 96.67\% & 97.50\% & 96.67\% & 97.08\% & 96.67\%  \\ 
& 2.02 & 1.13 & 1.13 & 2.13 & 2.07 & 2.2 & 2.23 & 2.21 \\ 
\midrule

\multicolumn{9}{@{}l}{Bagging:}\\

\multirow{2}{=}{Decision Tree} 
& 97.08\% & 97.08\% &  94.58\%  & 96.25\% & 96.25\% & 95.42\% & 94.58\% & 94.58\%  \\ 
& 39.11 & 40.94 & 44.53 & 39.64 & 40.04 & 39.65 & 39.87 & 40.9 \\ 
\cmidrule{2-9}

\multirow{2}{=}{SVM} 
& 97.92\% & 97.92\% & 97.50\%  & 96.67\% & 98.33\% & 96.67\% & 96.67\% & 96.25\%  \\ 
& 2.78 & 0.99 & 1.34 & 2.16 & 1.29 & 1.28 & 1.31 & 8.33 \\ 
\midrule

\multicolumn{9}{@{}l}{XGBoost:}\\

\multirow{2}{=}{GBTree} 
& 94.58\% & 94.58\% & 92.92\%  & 93.75\% & 93.75\% & 93.33\% & 95\% & 95\%  \\ 
& 1.66 & 1.7 & 1.68 & 1.68 & 1.69 & 1.76 & 1.75 & 1.75 \\ 
\cmidrule{2-9}

\multirow{2}{=}{GBLinear} 
& 98.33\% & 98.33\% & 98.33\%  & 97.92\% & 98.33\% & 95.83\% & 96.67\% & 97.92\%  \\ 
& 3.46 & 3.57& 3.61 & 3.51 & 3.58 & 3.45 & 3.44 & 3.61 \\ 
\bottomrule

\end{tabular*}
\end{table}

\end{document}

答案2

在此处输入图片描述

\documentclass[12pt,a4paper]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{makecell}

\begin{document}
    
    \begin{table}
        \caption{Caption text here}
        \label{teb:example}
        \small
        \setlength{\tabcolsep}{0pt}
        \begin{tabular*}{\textwidth}{@{\hspace{5pt}} p{3cm}@{\extracolsep{\fill}}cccccccc } 
            \toprule
            & \multicolumn{8}{c}{Accuracy \& Training Time (mins)}\\
            \cmidrule{2-9} 
            & IEMG & MAV & MAV1 & VAR & RMS & DASDV & AAC & WL \\
            \midrule
            \multicolumn{9}{@{}l}{\makecell[l]{Stacking\\ Models:}}\\
            
            \multirow{2}{=}{\makecell[l]{Random \\Forest, \\KNN,}} 
            & 97.92\% & 97.92\% &  97.92\%  & 97.08\% & 97.92\% & 96.67\% & 96.67\% & 96.67\%  \\ 
            & 14.82 & 15.96 & 16.51 & 15.3 & 15.18 & 14.85 & 15.77 & 15.6 \\ 
            \cmidrule{2-9}
            
            \multirow{2}{=}{SVM} 
            & 97.50\% & 97.50\% &  97.08\%  & 96.67\% & 97.50\% & 96.67\% & 97.08\% & 96.67\%  \\ 
            & 2.02 & 1.13 & 1.13 & 2.13 & 2.07 & 2.2 & 2.23 & 2.21 \\ 
            \midrule
            
            \multicolumn{9}{@{}l}{Bagging:}\\
            
            \multirow{2}{=}{\makecell[l]{Decision\\ Tree}} 
            & 97.08\% & 97.08\% &  94.58\%  & 96.25\% & 96.25\% & 95.42\% & 94.58\% & 94.58\%  \\ 
            & 39.11 & 40.94 & 44.53 & 39.64 & 40.04 & 39.65 & 39.87 & 40.9 \\ 
            \cmidrule{2-9}
            
            \multirow{2}{=}{SVM} 
            & 97.92\% & 97.92\% & 97.50\%  & 96.67\% & 98.33\% & 96.67\% & 96.67\% & 96.25\%  \\ 
            & 2.78 & 0.99 & 1.34 & 2.16 & 1.29 & 1.28 & 1.31 & 8.33 \\ 
            \midrule
            
            \multicolumn{9}{@{}l}{XGBoost:}\\
            
            \multirow{2}{=}{GBTree} 
            & 94.58\% & 94.58\% & 92.92\%  & 93.75\% & 93.75\% & 93.33\% & 95\% & 95\%  \\ 
            & 1.66 & 1.7 & 1.68 & 1.68 & 1.69 & 1.76 & 1.75 & 1.75 \\ 
            \cmidrule{2-9}
            
            \multirow{2}{=}{GBLinear} 
            & 98.33\% & 98.33\% & 98.33\%  & 97.92\% & 98.33\% & 95.83\% & 96.67\% & 97.92\%  \\ 
            & 3.46 & 3.57& 3.61 & 3.51 & 3.58 & 3.45 & 3.44 & 3.61 \\ 
            \bottomrule
            
        \end{tabular*}
    \end{table}
    
\end{document}

相关内容