如何在不使用小字体的情况下适应表格内容?

如何在不使用小字体的情况下适应表格内容?

我正在尝试使用 STATA 的 esttab 选项来处理表格内容。首先,表格太宽,行数不相加。根据之前的帖子,我解决了一些问题。但是,尽管使用了 \centering 和 \tiny 选项,表格仍不符合页面宽度。

LaTeX 代码:

\begin{table}[htbp]
\usepackage{tabulary}
\usepackage{booktabs}
\usepackage{adjustbox}
\setlength{\pdfpagewidth}{8.5in}\setlength{\pdfpageheight}{11in}
\centering
\tiny
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{Number of Pills Taken}
\begin{tabulary}{1.0\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{5}{c}}
\toprule
                                &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}&\multicolumn{1}{c}{(5)}\\
                                &\multicolumn{1}{c}{Pills Taken}&\multicolumn{1}{c}{Pills Taken}&\multicolumn{1}{c}{Pills Taken}&\multicolumn{1}{c}{Took over 500 mg iron}&\multicolumn{1}{c}{Took over 500 mg iron}\\
\midrule
Student in Treatment Group      &    4.732\sym{***}&                  &    4.128\sym{***}&    0.177\sym{**} &    0.217\sym{**} \\
                                &  (1.515)         &                  &  (1.463)         & (0.0715)         & (0.0931)         \\
\addlinespace
treatment\_soccer                &                  &    3.949\sym{**} &                  &                  &                  \\
                                &                  &  (1.760)         &                  &                  &                  \\
\addlinespace
treatment\_phy                   &                  &    5.484\sym{***}&                  &                  &                  \\
                                &                  &  (1.742)         &                  &                  &                  \\
\addlinespace
Student is Male                 &                  &                  &   -2.392\sym{*}  &                  &                  \\
                                &                  &                  &  (1.392)         &                  &                  \\
\addlinespace
Household Monthly Income (100 S/.)&                  &                  & -0.00732         &                  &                  \\
                                &                  &                  & (0.0805)         &                  &                  \\
\addlinespace
Time (hours) to School (recoded for missing values)&                  &                  &   -3.821\sym{***}&                  &                  \\
                                &                  &                  &  (1.013)         &                  &                  \\
\addlinespace
Age of Student (month)          &                  &                  &   -0.129\sym{**} &                  &                  \\
                                &                  &                  & (0.0589)         &                  &                  \\
\addlinespace
Anemic at Baseline x Treatment Indicator&                  &                  &                  &                  &  -0.0960         \\
                                &                  &                  &                  &                  &  (0.146)         \\
\addlinespace
Anemia Status at Follow-Up Survey (recoded for missing values)&                  &                  &                  &                  &-0.000553         \\
                                &                  &                  &                  &                  &  (0.121)         \\
\midrule
N. of obs.                      &      215         &      215         &      215         &      215         &      215         \\
Control Mean                    &    5.944         &    5.944         &    5.944         &    0.417         &    0.417         \\
\bottomrule
\multicolumn{6}{l}{\footnotesize Standard errors in parentheses}\\
\multicolumn{6}{l}{\footnotesize Full sample from cajamarca study.}\\
\multicolumn{6}{l}{\footnotesize \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\
\end{tabulary}
\end{table}

答案1

我建议的两个主要更改是 (1) 强制在数据列 4 和 5 的标题中换行,以及 (2) 对第一列使用环境tabularxX列类型。实施这些更改后,就没有必要减小表格中的字体大小(假设主文档字体大小为 10pt)。

在此处输入图片描述

\documentclass{article} % or some other suitable document class
\usepackage[T1]{fontenc}
\usepackage[letterpaper,margin=1in]{geometry} % set page parameters as needed
\setlength{\pdfpagewidth}{8.5in}
\setlength{\pdfpageheight}{11in}
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}

\usepackage{booktabs,tabularx,amsmath}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
    
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\newlength\mylen % width of data columns 4 and 5
\newcommand\mytab[1]{\smash[b]{%
   \begin{tabular}[t]{@{} >{\raggedright}p{\hsize} @{}} 
   #1 \end{tabular}}}

\begin{document}

\begin{table}[htbp]
\settowidth\mylen{500\,mg iron}

\caption{Number of Pills Taken\strut}

\begin{tabularx}{\textwidth}{@{} X ccc *{2}{C{\mylen}} @{}}
\toprule
 & (1) & (2) & (3) & (4) & (5) \\
 & Pills Taken & Pills Taken & Pills Taken 
 & Took over 500\,mg iron & Took over 500\,mg iron \\
\midrule
\mytab{Student in treatment group}
     & 4.732\sym{***}& & 4.128\sym{***} & 0.177\sym{**} &  0.217\sym{**} \\
     & (1.515)       & & (1.463)        & (0.0715)      & (0.0931)       \\
\addlinespace
treatment\_soccer   & &   3.949\sym{**} \\
                    & &  (1.760)        \\
\addlinespace
treatment\_phy      & &   5.484\sym{***}\\
                    & &  (1.742)        \\
\addlinespace
Student is male     & & & $-2.392$\sym{*}  \\
                    & & & (1.392)          \\
\addlinespace
\mytab{Household monthly income (100 S/.)}
                    & & & $-0.00732$ \\
                    & & & (0.0805)   \\
\addlinespace
\mytab{Time (hours) to school (recoded for missing values)}
                    & & & $-3.821$\sym{***} \\
                    & & & (1.013)           \\
\addlinespace
Age of student (month) 
                    & & & $-0.129$\sym{**} \\
                    & & & (0.0589)         \\
\addlinespace
\mytab{Anemic at baseline $\times$ Treatment Indicator}
                    & & & & &  $-0.0960$ \\
                    & & & & &  (0.146)   \\
\addlinespace
\mytab{Anemia status at follow-up survey (recoded for missing values)} 
                    & & & & & $-0.000553$ \\
                    & & & & & (0.121)     \\
\midrule
No.\ of obs.  & \mc{215}   & \mc{215}   & \mc{215}   & \mc{215}   & \mc{215}   \\
Control Mean  & \mc{5.944} & \mc{5.944} & \mc{5.944} & \mc{0.417} & \mc{0.417} \\
\bottomrule
\end{tabularx}

\smallskip
{\footnotesize 
Standard errors in parentheses. Full sample from cajamarca study. 
$\sym{*}\ p<0.10,\ \sym{**}\ p<0.05,\ \sym{***}\ p<0.01$}
\end{table}

\end{document}

答案2

对于起点:

  • 假设文档使用 10pt 字体大小,表格使用\small大小
  • 在第一列中启用文本断行
  • 重新设计了列标题
\documentclass{article}
\setlength{\pdfpagewidth}{8.5in}\setlength{\pdfpageheight}{11in}

\usepackage[skip=1ex]{caption}
\usepackage{tabulary}
\usepackage{multirow}
\usepackage{booktabs, siunitx}

\begin{document}
    \begin{table}[htbp]
\caption{Number of Pills Taken}
\centering
\small\linespread{0.9}\selectfont
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\setlength\tabcolsep{3pt}
\begin{tabulary}{\hsize}{@{} L*{5}{c} @{}}
    \toprule
    & \multicolumn{3}{c}{Pills Taken}
                             & \multicolumn{2}{c}{Took over \qty{500}{mg} iron} \\
    \cmidrule(r){2-4}
    \cmidrule(l){5-6}
    & (1)   & (2)   & (3)   & (4)   & (5)                                       \\
    \midrule
\multirow[t]{2}{=}{Student in Treatment Group}      
    & 4.732\sym{***}
            &       & 4.128\sym{***}
                            & 0.177\sym{**} 
                                    & 0.217\sym{**}                             \\
    &(1.515)&       &(1.463)&(0.0715)& (0.0931)                                 \\
    \addlinespace
\multirow[t]{2}{=}{treatment\_soccer}                
    &       & 3.949\sym{**} 
                    &       &       &                                           \\
    &       &(1.760)&       &       &                                           \\
    \addlinespace
\multirow[t]{2}{=}{treatment\_phy}                  
    &       & 5.484\sym{***}
                    &       &       &                                           \\
    &       &(1.742)&       &       &                                           \\
    \addlinespace
\multirow[t]{2}{=}{Student is Male}                 
    &       &       & -2.392\sym{*}  
                            &       &                                           \\
    &       &       &(1.392)&       &                                           \\
    \addlinespace
\multirow[t]{2}{=}{Household Monthly Income (100 S/.)}
    &       &       & -0.00732  
                            &       &                                           \\
    &       &       &(0.0805)         
                            &       &                                           \\
\addlinespace
\multirow[t]{2}{=}{Time (hours) to School (recoded for missing values)}
    &       &       & -3.821\sym{***}
                            &       &                                           \\
    &       &       &(1.013)&       &                                           \\
    \addlinespace
\multirow[t]{2}{=}{Age of Student (month)}          
    &       &       & -0.129\sym{**} 
                            &       &                                           \\
    &       &       & (0.0589)         
                            &       &                                           \\
    \addlinespace
\multirow[t]{2}{=}{Anemic at Baseline x Treatment Indicator}
    &       &       &       &       &  -0.0960                                  \\
    &       &       &       &       &  (0.146)                                  \\
    \addlinespace
\multirow[t]{2}{=}{Anemia Status at Follow-Up Survey (recoded for missing val.)}
    &       &       &       &       &-0.000553                                  \\
    &       &       &       &       & (0.121)                                   \\
    \midrule
\multirow[t]{2}{=}{N. of obs.}                      
    & 215   & 215   & 215   &  215  & 215                                       \\
\multirow[t]{2}{=}{Control Mean}                    
    & 5.944 & 5.944 & 5.944 & 0.417 & 0.417                                     \\
    \bottomrule
\multicolumn{6}{l}{\footnotesize Standard errors in parentheses}\\
\multicolumn{6}{l}{\footnotesize Full sample from cajamarca study.}\\
\multicolumn{6}{l}{\footnotesize \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\
\end{tabulary}
    \end{table}
\end{document}

在此处输入图片描述

进一步可能的改进:

  • S对带有数字的列使用列类型
  • 用于餐桌使用talltblrtabularray包装
  • 由于小数点处数字的水平对齐,如果表格中字体的 si 没有减少,则列宽会增加,因此使用footnotesize
\documentclass{article}
\setlength{\pdfpagewidth}{8.5in}\setlength{\pdfpageheight}{11in}

\usepackage[skip=1ex]{caption}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
    \begin{table}[htbp]
\footnotesize\linespread{0.9}\selectfont
\sisetup{
    table-align-text-after = false, % <---
    table-align-text-before= false, % <---
    input-open-uncertainty =,
    input-close-uncertainty=,
    round-mode=places,
    }

\begin{talltblr}[
caption = {Number of Pills Taken},
  label = {tab:statistic},
remark{Note} = {Standard errors in parentheses. Full sample from cajamarca study.\\
                *: \(p<0.10\),  **: \(p<0.05\),  ***: \(p<0.01\)}
                ]{colsep=3pt,
                  colspec={X[l]
                           *{2}{Q[c, si={table-format=-1.3{**},
                                        round-precision=3}]} 
                           *{2}{Q[c, si={table-format=-1.3{**},
                                         round-precision=4}]}
                                Q[c, si={table-format=-1.6{**},
                                         round-precision=6}] @{}},
                  cell{odd[3]}{1} = {r=2}{},               % <--
                  vspan=even,   
                  row{1,2,Y} = {guard},
                  row{4-X} ={rowsep=0pt},
                  row{odd[4]} = {abovesep=1ex}
                  }
    \toprule
    & \SetCell[c=3]{c}  Pills Taken 
            &       &       & \SetCell[c=2]{c}  Took over \qty[round-precision=0]{500}{mg} iron    \\
    \cmidrule[r]{2-4}
    \cmidrule[l]{5-6}
    & (1)   & (2)   & (3)   & (4)   & (5)                                       \\
    \midrule
Student in Treatment Group      
    & 4.732\TblrNote{***}
            &       & 4.128\TblrNote{***}
                            & 0.177\TblrNote{**}
                                    & 0.217\TblrNote{**}                             \\
    &(1.515)&       &(1.463)&(0.0715)& (0.0931)                                 \\
treatment\_soccer                
    &       & 3.949\TblrNote{**}
                    &       &       &                                           \\
    &       &(1.760)&       &       &                                           \\
treatment\_phy                 
    &       & 5.484\TblrNote{***}
                    &       &       &                                           \\
    &       &(1.742)&       &       &                                           \\
    \addlinespace
Student is Male                 
    &       &       & -2.392\TblrNote{*}
                            &       &                                           \\
    &       &       &(1.392)&       &                                           \\
Household Monthly Income (100 S/.)
    &       &       & -0.00732  
                            &       &                                           \\
    &       &       &(0.0805)         
                            &       &                                           \\
Time (hours) to School (recoded for missing values
    &       &       & -3.821\TblrNote{***}
                            &       &                                           \\
    &       &       &(1.013)&       &                                           \\
Age of Student (month)          
    &       &       & -0.129\TblrNote{**}
                            &       &                                           \\
    &       &       & (0.0589)         
                            &       &                                           \\
Anemic at Baseline x Treatment Indicator
    &       &       &       &       &  -0.0960                                  \\
    &       &       &       &       &  (0.146)                                  \\
Anemia Status at Follow-Up Survey (recoded for missing val.
    &       &       &       &       &-0.000553                                  \\
    &       &       &       &       & (0.121)                                   \\
    \midrule
N. of obs.                     
    & 215   & 215   & 215   &  215  & 215                                       \\
Control Mean                   
    & 5.944 & 5.944 & 5.944 & 0.417 & 0.417                                     \\
    \bottomrule
\end{talltblr}
    \end{table}
\end{document}

在此处输入图片描述

相关内容