适合其余纸张宽度的表格

适合其余纸张宽度的表格

这是我的表格。我想调整它的宽度,以便它能适应我其余的纸张对齐。也许需要将标题分成两行或以任何其他方式适应其余文本的宽度。

\documentclass{article}
\usepackage{float}
\usepackage{showframe}

 \begin{document}
 \begin{abstract}
 zzz
 \end{abstract}

 \section{Introduction}
 This is my table that its width is too big:
    \begin{table}[H]
        \centering
        \begin{tabular}{||c c c c c ||} 
            \hline
            ID & UCI Dataset Name & No. of samples & No. of attributes & No. of classes \\ [0.5ex] 
            \hline\hline
            DS1 & Cardiotocography & 2126 & 23  & 3 \\ 
            \hline
            DS2 & Wall-Following Robot Navigation Data & 5456 & 24  & 4 \\ 
            \hline
            DS3 & Spambase & 4601 & 57  & 2 \\
            \hline
            DS4 & MAGIC Gamma Telescope & 19020 & 11 & 2 \\
            \hline
            DS5 & Letter Recognition & 20000 & 16 & 26 \\
            \hline
            DS6 & MiniBooNE particle identification & 130065 & 50 & 2 \\ [1ex] 
            \hline
        \end{tabular}
    \caption{Datasets used for empirical evaluation}
    \label{table:1}
 \end{table}
 \end{document}

在此处输入图片描述

答案1

还有一个解决方案。在其中,我借用了 Alenanno 的专栏文章,并介绍了一些新包:

  • booktabs(作为 Alenanno)制定餐桌规则
  • makecell用于设置柱头
  • tabularx确定表格宽度\textwidth
  • siunitx使用S列类型提供良好的数字格式

为了将行限制为仅一行文本,我建议将字体大小减小到\small。通过此措施,表格变为:

在此处输入图片描述

完整代码(仅包含表格)是:

\documentclass{article}
    \usepackage{booktabs,makecell,tabularx}
    \renewcommand\theadfont{\small}
    \newcolumntype{L}{>{\raggedright\arraybackslash}X}
    \usepackage{siunitx}

\begin{document}
    \begin{table}[h]
    \centering
    \setlength{\tabcolsep}{5pt}
    \small
\begin{tabularx}{\textwidth}{c L S[table-format=6.0]*{2}{S[table-format=2.0]}}
    \toprule
\thead{ID}  &   \thead{UCI Dataset Name} 
                &   {\thead{Samples\\(numbers)}}
                    &   {\thead{Attributes\\(numbers)}} 
                        &   {\thead{Classes\\number}}           \\
    \midrule
DS1 & Cardiotocography                      & 2126  & 23    & 3     \\
DS2 & Wall-Following Robot Navigation Data  & 5456  & 24    & 4     \\
DS3 & Spambase                              & 4601  & 57    & 2     \\
DS4 & MAGIC Gamma Telescope                 & 19020 & 11    & 2     \\
DS5 & Letter Recognition                    & 20000 & 16    & 26    \\
DS6 & MiniBooNE particle identification     & 130065& 50    & 2     \\
    \bottomrule
\end{tabularx}
    \caption{Datasets used for empirical evaluation}
    \label{table:1}
    \end{table}
 \end{document}

编辑:哎呀,我没有上传最后的代码和图像版本......现在已经纠正了。

答案2

您可以剪掉一些多余的长度,接上(number)一条新线,然后使用adjustbox

输出

在此处输入图片描述

代码

\documentclass{article}
\usepackage{adjustbox}
\usepackage{array,booktabs}

\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
\begin{table}[!htb]
    \centering
\begin{adjustbox}{max width=\textwidth}
\begin{tabular}{ccC{1.5cm}C{1.8cm}C{1.5cm}}
 \toprule
ID & UCI Dataset Name & Samples {\footnotesize (number)} & Attributes {\footnotesize (number)} & Classes {\footnotesize (number)} \\
\midrule
DS1 & Cardiotocography & 2126 & 23  & 3 \\ 
DS2 & Wall-Following Robot Navigation Data & 5456 & 24  & 4 \\ 
DS3 & Spambase & 4601 & 57  & 2 \\
DS4 & MAGIC Gamma Telescope & 19020 & 11 & 2 \\
DS5 & Letter Recognition & 20000 & 16 & 26 \\
DS6 & MiniBooNE particle identification & 130065 & 50 & 2 \\
\bottomrule
\end{tabular}
\end{adjustbox}    
\caption{Datasets used for empirical evaluation}
\label{table:1}
\end{table}
\end{document}

相关内容