调整表格列标题以适合页面宽度

调整表格列标题以适合页面宽度

我想适合我的两个包含 7 个列标题的表格,以下是我的代码:

\documentclass[preprint,12pt,authoryear]{elsarticle}


      %------------------------------------------------------------------------------------------------------------------------------------------
 \usepackage[margin=2cm]{geometry}
 \usepackage[figurename=Fig.]{caption}
 \usepackage{tabulary}
 \usepackage{tabularx}
 \usepackage{varioref}
 \usepackage{commath}
  \usepackage{array,multirow}
  \usepackage{threeparttable}  
  \usepackage{longtable}
  \usepackage{tabulary}
  \usepackage{booktabs}
  \usepackage{float}
  \usepackage{caption}
 \usepackage{subcaption}
 \usepackage[table]{xcolor}

  \begin{document}
    \begin{table}[H]
 \rowcolors{2}{}{lightgray}
 \centering
 \caption{Balance information for outcome variables at baseline.}
 \label{tab:multi row}
 \begin{tabular}{p{3cm} p{3cm} p{1cm}p{3cm}p{1cm}p{2cm} p{2cm}p{2cm}}
  \hline
&Mean control & SD & Mean treatment & SD & Difference & SD & N \\
\hline
\cellcolor{lightgray} WHO&1.53 & 0.55 & 1.50&0.58&-0.03&(0.04)&856 \\
&  \\
 \cellcolor{lightgray}Hopkins & 0.93& 0.60& 0.99& 0.62& 0.06& (0.04)& 856\\
 &  \\
 \cellcolor{lightgray}Satisfied &3.07& 1.67& 3.17& 1.71& 0.10& (0.12)& 856 \\
 &  \\
 \cellcolor{lightgray}Problems &0.14& 0.37& 0.13& 0.35& -0.01& (0.03)& 559\\
&  \\
      \hline
      \end{tabular}
      \end{table}%


\begin{table}[H]
 \centering
  \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
  \caption{Chiefdom Level Analysis}
   \begin{tabular}{l*{7}{c}}
      \hline\hline
                  &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}&\multicolumn{1}{c}{(5)}&\multicolumn{1}{c}{(6)}&\multicolumn{1}{c}{(7)}\\
                 &\multicolumn{1}{c}{Total} &\multicolumn{1}{c}{Parent talks loudly}&\multicolumn{1}{c}{Friendship helping}&\multicolumn{1}{c}{online courses}&\multicolumn{1}{c}{Trust mean}&\multicolumn{1}{c}{Learning remotely}&\multicolumn{1}{c}{Mean Effects index}\\
          \hline
           Treatment           &      -0.116         &      -0.091         &      -0.032         &      -0.171         &      -0.473\sym{***}&      -0.068         &      -0.325         \\
                &     (0.368)         &     (0.106)         &     (0.103)         &     (0.103)         &     (0.176)         &     (0.109)         &     (0.278)         \\
          Total               &       0.146         &                     &                     &                     &                     &                     &                     \\
                &     (0.112)         &                     &                     &                     &                     &                     &                     \\
      Parent talks loudly &                     &       0.409\sym{***}&                     &                     &                     &                     &                     \\
                &                     &     (0.124)         &                     &                     &                     &                     &                     \\
      Friendship helping  &                     &                     &       0.458\sym{***}&                     &                     &                     &                     \\
                &                     &                     &     (0.126)         &                     &                     &                     &                     \\
      Online courses      &                     &                     &                     &       1.027\sym{***}&                     &                     &                     \\
                &                     &                     &                     &     (0.068)         &                     &                     &                     \\
 Trust mean          &                     &                     &                     &                     &       0.017         &                     &                     \\
                &                     &                     &                     &                     &     (0.100)         &                     &                     \\
  Learning remotely   &                     &                     &                     &                     &                     &       1.096\sym{***}&                     \\
                &                     &                     &                     &                     &                     &     (0.088)         &                     \\
   Constant            &       5.288\sym{***}&       0.277\sym{**} &       0.272\sym{**} &      -0.130         &       4.239\sym{***}&      -0.315\sym{**} &       0.165         \\
                &     (0.767)         &     (0.113)         &     (0.126)         &     (0.130)         &     (0.412)         &     (0.136)         &     (0.205)         \\
   \hline
    N                   &     106.000         &     100.000         &     100.000         &     104.000         &      81.000         &      98.000         &     106.000         \\
     R^{2}               &       0.205         &       0.345         &       0.330         &       0.809         &       0.360         &       0.756         &       0.255         \\
\hline\hline
 \multicolumn{8}{l}{\footnotesize t-statistics are between parentheses. Statistical significance at \enspace *** $p < 0.01$; \enspace ** $p < 0.05$;\enspace * $p < 0.10$}\\
  \end{tabular}
  \end{table}
 \end{document}

我的输出:

在此处输入图片描述

答案1

嗯,如果考虑@Bernard 的评论,那么您的表格将变成:

在此处输入图片描述

\documentclass[preprint,12pt,authoryear]{elsarticle}
%------------------------------------------------------------------------------------------------------------------------------------------
\usepackage[margin=2cm]{geometry}
\usepackage[figurename=Fig.]{caption}
\usepackage{tabulary}
\usepackage{tabularx}
\usepackage{varioref}
\usepackage{commath}
\usepackage{array, makecell, multirow}
\usepackage{threeparttable}
\usepackage{longtable}
\usepackage{tabulary}
\usepackage{booktabs}
\usepackage{float}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[table]{xcolor}

  \begin{document}
    \begin{table}[H]
 \rowcolors{2}{}{lightgray}
 \centering
 \caption{Balance information for outcome variables at baseline.}
 \label{tab:multi row}
 \begin{tabular}{ >{\columncolor{lightgray}}l *{7}{c} } 
    \hline
     &  \thead{Mean\\ control}
            & SD
                &   \thead{Mean\\ treatment}
                    &   SD
                        &   Difference
                            &   SD
                                &   N               \\
    \hline
WHO & 1.53 & 0.55 & 1.50 & 0.58 & -0.03 & (0.04) & 856 \\
    &  \\
Hopkins & 0.93& 0.60& 0.99& 0.62& 0.06& (0.04)& 856\\
    &  \\
Satisfied &3.07& 1.67& 3.17& 1.71& 0.10& (0.12)& 856 \\
    &  \\
Problems &0.14& 0.37& 0.13& 0.35& -0.01& (0.03)& 559\\
    &  \\
    \hline
      \end{tabular}
      \end{table}%


\begin{table}[H]
 \centering
  \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
  \caption{Chiefdom Level Analysis}
   \begin{tabular}{l*{7}{c}}
      \hline\hline
    & (1)   & (2) & (3) & (4) & (5) & (6) & (7)  \\
    & Total & \thead{Parent\\ talks\\ loudly}
                &  \thead{Friendship\\ helping}
                    &  \thead{online\\ courses}
                        & \thead{Trust\\ mean} 
                            & \thead{Learning\\ remotely}
                                & \thead{Mean\\ Effects\\ index} \\
          \hline
           Treatment           &      -0.116         &      -0.091         &      -0.032         &      -0.171         &      -0.473\sym{***}&      -0.068         &      -0.325         \\
                &     (0.368)         &     (0.106)         &     (0.103)         &     (0.103)         &     (0.176)         &     (0.109)         &     (0.278)         \\
          Total               &       0.146         &                     &                     &                     &                     &                     &                     \\
                &     (0.112)         &                     &                     &                     &                     &                     &                     \\
      Parent talks loudly &                     &       0.409\sym{***}&                     &                     &                     &                     &                     \\
                &                     &     (0.124)         &                     &                     &                     &                     &                     \\
      Friendship helping  &                     &                     &       0.458\sym{***}&                     &                     &                     &                     \\
                &                     &                     &     (0.126)         &                     &                     &                     &                     \\
      Online courses      &                     &                     &                     &       1.027\sym{***}&                     &                     &                     \\
                &                     &                     &                     &     (0.068)         &                     &                     &                     \\
 Trust mean          &                     &                     &                     &                     &       0.017         &                     &                     \\
                &                     &                     &                     &                     &     (0.100)         &                     &                     \\
  Learning remotely   &                     &                     &                     &                     &                     &       1.096\sym{***}&                     \\
                &                     &                     &                     &                     &                     &     (0.088)         &                     \\
   Constant            &       5.288\sym{***}&       0.277\sym{**} &       0.272\sym{**} &      -0.130         &       4.239\sym{***}&      -0.315\sym{**} &       0.165         \\
                &     (0.767)         &     (0.113)         &     (0.126)         &     (0.130)         &     (0.412)         &     (0.136)         &     (0.205)         \\
   \hline
    N                   &     106.000         &     100.000         &     100.000         &     104.000         &      81.000         &      98.000         &     106.000         \\
    $R^{2}$               &       0.205         &       0.345         &       0.330         &       0.809         &       0.360         &       0.756         &       0.255         \\
\hline\hline
 \multicolumn{8}{p{\linewidth}}{\footnotesize t-statistics are between parentheses. Statistical significance at \enspace *** $p < 0.01$; \enspace ** $p < 0.05$;\enspace * $p < 0.10$}\\
  \end{tabular}
  \end{table}
 \end{document}

不过,我会R 中的长表超出了页面宽度作为如何编写类似表格的示例。

相关内容