多列对齐

多列对齐

以下代码生成该表:

在此处输入图片描述

\begin{table}[]
\centering
\caption{Descriptive Statistics by Size and ESG Consensus Quartiles}
\label{tab:my-table}
\begin{tabular}{lrrrrlrrrr}
\hline
 & \multicolumn{4}{c}{} & \multicolumn{4}{c}{} \\ % Empty line
\multirow{2}{*}{\textbf{\begin{tabular}[c]{@{}l@{}}Size\\ Quartile\end{tabular}}} &
  \multicolumn{9}{c}{\textbf{ESG Consensus Quartile}} \\ \cline{2-10} 
 &
  \multicolumn{1}{c}{\textbf{Low}} &
  \multicolumn{1}{c}{\textbf{2}} &
  \multicolumn{1}{c}{\textbf{3}} &
  \multicolumn{1}{c}{\textbf{High}} &
  \multicolumn{1}{r}{\textbf{}} &
  \multicolumn{1}{c}{\textbf{Low}} &
  \multicolumn{1}{c}{\textbf{2}} &
  \multicolumn{1}{c}{\textbf{3}} &
  \textbf{High} \\ \hline
 &
  \multicolumn{4}{c}{\textbf{Average Firm Size}} &
   &
  \multicolumn{4}{c}{\textbf{Average ESG Disagreement}} \\ \cline{2-5} \cline{7-10} 
\textbf{Small} &
  655 &
  885 &
  870 &
  322 &
   &
  16 &
  17 &
  14 &
  11 \\
\textbf{2} &
  2197 &
  2022 &
  2059 &
  3489 &
   &
  13 &
  20 &
  20 &
  23 \\
\textbf{3} &
  5799 &
  5389 &
  5830 &
  5265 &
   &
  15 &
  24 &
  23 &
  27 \\
\textbf{Big} &
  13543 &
  10511 &
  20058 &
  27884 &
   &
  20 &
  23 &
  32 &
  29 \\
 &
  \multicolumn{4}{c}{\textbf{Average ESG Consensus}} &
  \textbf{} &
  \multicolumn{4}{c}{\textbf{Number of Observations}} \\ \cline{2-5} \cline{7-10} 
\textbf{Small} &
  21 &
  35 &
  48 &
  59 &
   &
  966 &
  839 &
  307 &
  86 \\
\textbf{2} &
  21 &
  36 &
  49 &
  64 &
   &
  653 &
  688 &
  649 &
  170 \\
\textbf{3} &
  22 &
  34 &
  50 &
  68 &
   &
  487 &
  501 &
  621 &
  540 \\
\textbf{Big} &
  20 &
  37 &
  53 &
  69 &
   &
  66 &
  132 &
  572 &
  1318 \\ \hline
\end{tabular}
\end{table}

最后两列之间的间隙太大了。我尝试了各种方法来对齐列宽(例如,使用 siunitx 包中的 S 列类型:S[table-format=5.0],或使用 tabularx 包),但都不起作用。我还认为一定有一种“更简单”的方法来解决这个问题。

答案1

我建议使用booktabs提供缩短行的方法的包\cmidrule。这样,您无需添加无用的附加列:

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

\begin{document}
\begin{table}
    \centering\small
    \caption{Descriptive Statistics by Size and ESG Consensus Quartiles}
    \label{tab:my-table}
    \begin{tabular}{ l *{8}{>{\raggedleft\arraybackslash}p{0.85cm}} }
    \toprule
        \\ % Empty line
        &
        \multicolumn{8}{c}{\textbf{ESG Consensus Quartile}} \\ 
    \cmidrule{2-9} 
    \multirow{-2}{*}{\parbox{1.45cm}{\textbf{Size \\ Quartile}}} &
        \multicolumn{1}{c}{\textbf{Low}} &
        \multicolumn{1}{c}{\textbf{2}} &
        \multicolumn{1}{c}{\textbf{3}} &
        \multicolumn{1}{c}{\textbf{High}} &
        \multicolumn{1}{c}{\textbf{Low}} &
        \multicolumn{1}{c}{\textbf{2}} &
        \multicolumn{1}{c}{\textbf{3}} &
        \textbf{High} \\ 
    \midrule
        &
        \multicolumn{4}{c}{\textbf{Average Firm Size}} &
        \multicolumn{4}{c}{\textbf{Average ESG Disagreement}} \\ 
    \cmidrule(r){2-5} \cmidrule(l){6-9} 
    \textbf{Small} &   655 &   885 &   870 &   322 &    16 &    17 &    14 &    11 \\
    \textbf{2}     &  2197 &  2022 &  2059 &  3489 &    13 &    20 &    20 &    23 \\
    \textbf{3}     &  5799 &  5389 &  5830 &  5265 &    15 &    24 &    23 &    27 \\
    \textbf{Big}   & 13543 & 10511 & 20058 & 27884 &    20 &    23 &    32 &    29 \\
        &
        \multicolumn{4}{c}{\textbf{Average ESG Consensus}} &
        \multicolumn{4}{c}{\textbf{Number of Observations}} \\ 
    \cmidrule(r){2-5} \cmidrule(l){6-9} 
    \textbf{Small} &    21 &    35 &    48 &    59 &   966 &   839 &   307 &    86 \\
    \textbf{2}     &    21 &    36 &    49 &    64 &   653 &   688 &   649 &   170 \\
    \textbf{3}     &    22 &    34 &    50 &    68 &   487 &   501 &   621 &   540 \\
    \textbf{Big}   &    20 &    37 &    53 &    69 &    66 &   132 &   572 &  1318 \\ 
      \bottomrule
    \end{tabular}
\end{table}
\end{document} 

在此处输入图片描述

您还可以简单地使用@{5pt}列定义在列之间添加一些空间:l rrrr @{5pt} rrrr而无需引入额外的列。

但是,请注意,r列的宽度只能与其内容一样宽,这可能不是最佳选择,因为在我看来,同样宽度的列在此表中看起来更好。

答案2

  • 看起来您使用某些表格生成工具来生成表格代码,因此表格代码非常混乱\multicolumn
  • 使用tabularray包表后,代码变得更加简洁和清晰。
  • 对于列格式,我将使用siunitx。使用数字可以在单元格中居中但右对齐:
\documentclass{article}
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%
\usepackage{lipsum}                             % for dummy text
%---------------------------------------------------------------%
\usepackage[skip=1ex]{caption}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
    \begin{table}[htb]
\caption{Descriptive Statistics by Size and ESG Consensus Quartiles}
\label{tab:my-table}

\begin{tblr}{colspec = {@{} Q[r, font=\bfseries]
                            *{4}{X[c,si={table-format=5.0}]}
                            *{3}{X[c,si={table-format=3.0}]}
                            X[c,si={table-format=4.0}]
                        @{}},
             colsep  = 4pt, 
             cell{1}{1} = {r=2}{},
             cell{1}{2} = {c=8}{font=\bfseries},
             cell{3,8}{2,6} = {c=4}{guard, font=\bfseries},
             row{1,2}   = {guard, font=\bfseries}
             }
    \toprule
{Size \\ Quartile}
        &   ESG Consensus Quartile
                &   &   &       &       &   &   &           \\
    \midrule
        & Low   & 2 & 3 & High  & Low   & 2 & 3 & Hig       \\
    \midrule
        &   Average Firm Size
                &   &   &       & Average ESG Disagreement  
                                        &   &   &           \\
    \cmidrule[r]{2-5} \cmidrule[l]{6-9}
Small   &   655 &   885 &   870 &   322 & 16 & 17 & 14 & 11 \\
2       &  2197 &  2022 &  2059 &  3489 & 13 & 20 & 20 & 23 \\
3       &  5799 &  5389 &  5830 &  5265 & 15 & 24 & 23 & 27 \\
Big     & 13543 & 10511 & 20058 & 27884 & 20 & 23 & 32 & 29 \\
        &   Average ESG Consensus
                &   &   &       &   Number of Observations
                                    &   &   &                   \\
    \cmidrule[r]{2-5} \cmidrule[l]{6-9}
Small   &    21 &    35 &    48 &    59 & 966 & 839 & 307 &   86 \\
2       &    21 &    36 &    49 &    64 & 653 & 688 & 649 &  170 \\
3       &    22 &    34 &    50 &    68 & 487 & 501 & 621 &  540 \\
Big &    20 &    37 &    53 &    69 &    66 &   132 & 572 & 1318 \\
      \bottomrule
\end{tblr}
    \end{table}
\end{document}

在此处输入图片描述

(红线表示页面布局)

答案3

两个建议:将八个数据列中的数字与其隐含的小数位标记对齐,并去掉大胆的在标题行和左侧列中。

在此处输入图片描述

\documentclass{article} % or some other suitable document class
\usepackage{booktabs} % for well-spaced horizontal rules
\usepackage{amsmath}  % for '\smash[b]' macro
\usepackage[group-digits=false]{siunitx} % for 'S' column type

\begin{document}

\begin{table}[ht]
\centering
\caption{Descriptive statistics by size and ESG consensus quartiles}
\label{tab:my-table}

\smallskip
\begin{tabular}{@{} 
                l 
                *{4}{S[table-format=5.0]} 
                *{3}{S[table-format=3.0]}
                     S[table-format=4.0]
                @{}}
\toprule 
\smash[b]{\begin{tabular}[t]{@{}l@{}} Size \\ quartile \end{tabular}} &
\multicolumn{8}{c@{}}{ESG consensus quartile} \\ 
\cmidrule(lr){2-5} \cmidrule(l){6-9}
 & {Low} & {2} & {3} & {High} & {Low} & {2} & {3} & {High} \\
\midrule
 & \multicolumn{4}{c}{Avg.\ firm size} 
 & \multicolumn{4}{c@{}}{Avg.\ ESG disagreement} \\ 
\cmidrule(lr){2-5} \cmidrule(l){6-9} 
Small &   655 &   885 &   870 &   322 & 16 & 17 & 14 & 11 \\
2     &  2197 &  2022 &  2059 &  3489 & 13 & 20 & 20 & 23 \\
3     &  5799 &  5389 &  5830 &  5265 & 15 & 24 & 23 & 27 \\
Big   & 13543 & 10511 & 20058 & 27884 & 20 & 23 & 32 & 29 \\
\addlinespace
 & \multicolumn{4}{c}{Avg.\ ESG consensus} 
 & \multicolumn{4}{c@{}}{Number of obs.} \\ 
\cmidrule(lr){2-5} \cmidrule(l){6-9}
Small &   21 &   35 &   48 &   59 & 966 & 839 & 307 &   86 \\
2     &   21 &   36 &   49 &   64 & 653 & 688 & 649 &  170 \\
3     &   22 &   34 &   50 &   68 & 487 & 501 & 621 &  540 \\
Big   &   20 &   37 &   53 &   69 &  66 & 132 & 572 & 1318 \\ 
\bottomrule
\end{tabular}
\end{table}

\end{document}

答案4

显然,“平均 ESG 分歧”太宽了。将这么长的副标题分成两行。使用 可获得相等的大小siunitx

还请注意我如何定义\splitcell以便于输入。

\documentclass{article}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}

\begin{table}[htp]

% a local command
\newcommand{\splitcell}[2][c]{\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}
\sisetup{group-digits=none}

\centering

\caption{Descriptive Statistics by Size and ESG Consensus Quartiles}
\label{tab:my-table}

\begin{tabular}{
  @{}
  l
  *{4}{S[table-format=5.0]}
  @{\qquad}
  *{4}{S[table-format=4.0]}
  @{}
}
\toprule
\bfseries\smash{\splitcell[t]{Size \\ Quartile}}
 & \multicolumn{8}{c@{}}{\textbf{ESG Consensus Quartile}} \\
\cmidrule(l){2-9} 
 &
  \multicolumn{1}{c}{\textbf{Low}} &
  \multicolumn{1}{c}{\textbf{2}} &
  \multicolumn{1}{c}{\textbf{3}} &
  \multicolumn{1}{c@{\qquad}}{\textbf{High}} &
  \multicolumn{1}{c}{\textbf{Low}} &
  \multicolumn{1}{c}{\textbf{2}} &
  \multicolumn{1}{c@{}}{\textbf{3}} &
  \textbf{High} \\
\midrule
 &
  \multicolumn{4}{c@{\qquad}}{\bfseries\splitcell{Average \\ Firm Size}} &
  \multicolumn{4}{c@{}}{\bfseries\splitcell{Average ESG \\ Disagreement}} \\
\cmidrule(lr{2em}){2-5} \cmidrule(l){6-9} 
\textbf{Small} &
  655 &
  885 &
  870 &
  322 &
  16 &
  17 &
  14 &
  11 \\
\textbf{2} &
  2197 &
  2022 &
  2059 &
  3489 &
  13 &
  20 &
  20 &
  23 \\
\textbf{3} &
  5799 &
  5389 &
  5830 &
  5265 &
  15 &
  24 &
  23 &
  27 \\
\textbf{Big} &
  13543 &
  10511 &
  20058 &
  27884 &
  20 &
  23 &
  32 &
  29 \\
\midrule
 &
  \multicolumn{4}{c@{\qquad}}{\bfseries\splitcell{Average ESG \\ Consensus}} &
  \multicolumn{4}{c@{}}{\bfseries\splitcell{Number of \\ Observations}} \\
\cmidrule(lr{2em}){2-5} \cmidrule(l){6-9} 
\textbf{Small} &
  21 &
  35 &
  48 &
  59 &
  966 &
  839 &
  307 &
  86 \\
\textbf{2} &
  21 &
  36 &
  49 &
  64 &
  653 &
  688 &
  649 &
  170 \\
\textbf{3} &
  22 &
  34 &
  50 &
  68 &
  487 &
  501 &
  621 &
  540 \\
\textbf{Big} &
  20 &
  37 &
  53 &
  69 &
  66 &
  132 &
  572 &
  1318 \\
\bottomrule
\end{tabular}

\end{table}

\end{document}

在此处输入图片描述

相关内容