调整表格以适应文本宽度

调整表格以适应文本宽度

我有一个表格,它占据了我页面的整个水平空间。但是,我希望它的长度等于文本宽度。我尝试了在类似问题中找到的一些方法,但似乎没有任何效果。我在下面添加了一个 MWE。

\documentclass{report}

\usepackage{caption}
\usepackage{float}
\usepackage{graphicx}

\begin{document}

In order to get the most complete view of the different methods, their computation times are printed in the table below:

\begin{table}[h]
\centering
\caption{Computation time for the different bootstrap methods\label{comptimes}}
\footnotesize
\begin{tabular}{l|llll}
&    \textbf{Classical method} & \textbf{SBS robustification}     &  \textbf{Winsorized bootstrap} & \textbf{IFB bootstrap} \\ \hline
\textbf{\begin{tabular}[c]{@{}l@{}}Computation time \\ (in  seconds)\end{tabular}} & 10.28                 & 155.97                     &  40.67                     & 54.31             
\end{tabular}
\end{table}

\end{document}

答案1

一般答案是tabularx。但对于您的用例,我建议您重新考虑表格的呈现方式。您如何看待这样的解决方案:

\documentclass{report}

\usepackage{caption}
\usepackage{booktabs,siunitx}

\begin{document}

In order to get the most complete view of the different methods, their
computation times are printed in the table below:
\begin{table}[h]
  \centering
  \footnotesize
  \caption{Computation time for the different bootstrap
    methods\label{comptimes}}
  \begin{tabular}{@{}lS[table-format = 3.2]@{}}
    \toprule
    Method               & {Time (sec.)} \\\midrule
    Classical method     & 10.28  \\
    SBS robustification  & 155.97 \\
    Winsorized bootstrap & 40.67  \\
    IFB bootstrap        & 54.31  \\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

在此处输入图片描述

答案2

尝试这个:

\documentclass{report}

\usepackage{caption}
\usepackage{float}
\usepackage{graphicx}

\begin{document}

In order to get the most complete view of the different methods, their computation times are printed in the table below:

\begin{table}[h]
\centering
\caption{Computation time for the different bootstrap methods\label{comptimes}}
\footnotesize
 \begin{tabular}{@{}p{.19\textwidth}|p{.19\textwidth}@{}p{.19    \textwidth}@{}p{.19\textwidth}@{}p{.19\textwidth}}
&\textbf{Classical method} & \textbf{SBS robustification} &      \textbf{Winsorized bootstrap} & \textbf{IFB bootstrap} \\ \hline
Computation time (in  seconds)& 10.28 &155.97 & 40.67 & 54.31             
\end{tabular}
\end{table}

\end{document}

相关内容