修复表格中的列间空格

修复表格中的列间空格

我输入了太多空格,导致值无法保持对齐,溢出到边距中。我需要做什么来解决这个问题?

\documentclass[12pt,notitlepage]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{booktabs} % For better looking tables
\usepackage{natbib}
\usepackage{amsmath}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage{enumitem}                 
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{array}
\usepackage[english]{babel}
\usepackage{siunitx}
 \usepackage{sub caption}
 \usepackage{dcolumn}
\newcolumntype{d}[1]{D..{#1}}
 \newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro


  \begin{document}


  \begin{table}[ht!]   
\small
  \captionsetup{font={small,bf}}
  % We use 'tabular*' to auto-determine intercolumn whitespace:
   \setlength\tabcolsep{0pt} 

  \caption{Descriptive statistics}

   \begin{tabular*}{\textwidth}{l @{\extracolsep{\fill}}d{1.0} d{1.0} *{4}{d{2.0}} d{1.0} d{1.0} *{5}{d{1.0}} }
    \multicolumn{5}{l}{Panel A. Sub-sample: Descriptive statistics} \\
  \toprule
    & \multicolumn{6}{c}{Before Covid-19 outbreak} & \multicolumn{7}{c}{After Covid-19 outbreak} 
  \\
      \cmidrule{2-7} \cmidrule{8-14} 
  & \mc{No of obs} & \mc{Mean} & \mc{Min} & \mc{Max} & \mc{Std.\ Dev.} & 
  & \mc{No of obs} & \mc{Mean} & \mc{Min} & \mc{Max} & \mc{Std.\ Dev} &  \\
 \midrule
Price & 2853 & 75.22 & 26.21 & 145.29 & 23.88 &  & 549 & 54.04 & -37.63 & 84.65 & 17.01   \\
Open  & 2853 & 75.25 & 27.30 & 145.19& 23.85 &  & 549 & 54.05 & -14.00 & 84.53 & 16.77   \\
High  & 2853 & 76.32 & 27.48 & 147.27& 24.01 &  & 549 & 55.10 & 13.69 & 85.41 & 16.54   \\
Low   & 2853 & 74.07 & 26.05 & 143.22 &23.66 &  & 549 & 52.91 & -40.32 & 83.39 & 17.21   \\
\bottomrule
\end{tabular*}
\bigskip

 \begin{tabular*}{\textwidth}{l@{\extracolsep{\fill}} *{8}{d{2.3}} *{2}{d{1.3}}}
\multicolumn{6}{l}{Panel B. Dickey-Fuller Test} \\
  \toprule
   & \mc{Test Statistic} & \mc{($p$-value)} & \mc{Lags} & \mc{No of obs} & \mc{ (1\%)} & \mc{ (5\%)} & \mc{(10\%)} & 
    \mc{D-type: float}   \\
    \midrule
     Price & 0.216 & (0.366) & 2853 & 0.094 & 0.865 & -0.234  & -0.164  &Yes \\
     \bottomrule
      \end{tabular*}

  \bigskip
  Here's a description for the table
 \end{table}
 \end{document}

我当前的输出:

在此处输入图片描述

答案1

以下代码使用常规tabular,通过设置数字元素siunits' S-列规范,而不是使用dcolumn。使用标准\tabcolsep,两者都tabulars适合您指定的边距

在此处输入图片描述

\documentclass{article}

\usepackage[margin=2cm,showframe]{geometry}
\usepackage{booktabs} % For better looking tables
\usepackage{caption}
\usepackage{siunitx}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro

\begin{document}

\begin{table}
  \small
  \begingroup\centering
  \captionsetup{font={small,bf}}
  \caption{Descriptive statistics}
  %\setlength{\tabcolsep}{0.5\tabcolsep}% Shrink \tabcolsep by 50%
  \begin{tabular}{
    l % Description
    % Before Covid-19 outbreak
    S[table-format=4.0] % No of obs
    S[table-format=2.2] % Mean
    S[table-format=2.2] % Min
    S[table-format=3.2] % Max
    S[table-format=2.2] % Std Dev
    % After Covid-19 outbreak
    S[table-format=3.0] % No of obs
    S[table-format=2.2] % Mean
    S[table-format=-2.2] % Min
    S[table-format=2.2] % Max
    S[table-format=2.2] % Std Dev
  }
    \multicolumn{11}{l}{Panel A. Sub-sample: Descriptive statistics} \\
    \toprule
      & \multicolumn{5}{c}{Before Covid-19 outbreak} & \multicolumn{5}{c}{After Covid-19 outbreak} \\
    \cmidrule(lr){2-6} \cmidrule(lr){7-11}
      & \mc{Obs} & \mc{Mean} & \mc{Min} & \mc{Max} & \mc{Std.\ Dev.} & 
        \mc{Obs} & \mc{Mean} & \mc{Min} & \mc{Max} & \mc{Std.\ Dev.} \\
    \midrule
    Price & 2853 & 75.22 & 26.21 & 145.29 & 23.88 & 549 & 54.04 & -37.63 & 84.65 & 17.01 \\
    Open  & 2853 & 75.25 & 27.30 & 145.19 & 23.85 & 549 & 54.05 & -14.00 & 84.53 & 16.77 \\
    High  & 2853 & 76.32 & 27.48 & 147.27 & 24.01 & 549 & 55.10 &  13.69 & 85.41 & 16.54 \\
    Low   & 2853 & 74.07 & 26.05 & 143.22 & 23.66 & 549 & 52.91 & -40.32 & 83.39 & 17.21 \\
    \bottomrule
  \end{tabular}

  \bigskip

  \begin{tabular}{ l *{8}{c} }
    \multicolumn{9}{l}{Panel B. Dickey-Fuller Test} \\
    \toprule
    & \mc{Test Statistic} & \mc{($p$-value)} & \mc{Lags} & \mc{No of obs} & \mc{ (1\%)} & \mc{ (5\%)} & \mc{(10\%)} & \mc{D-type: float} \\
    \midrule
    Price & $0.216$ & $(0.366)$ & $2853$ & $0.094$ & $0.865$ & $-0.234$ & $-0.164$ & Yes \\
    \bottomrule
  \end{tabular}
  
  \bigskip
  \endgroup
  
  Here's a description for the table
\end{table}

\end{document}

相关内容