如何调整 LaTeX 中表格的宽度?

如何调整 LaTeX 中表格的宽度?

我正在使用 LaTeX,并试图将一个大宽度的表格放入单个页面宽度中。似乎变量的名称太长,从而导致表格“超出”范围。除非采用简化变量名称的解决方案,否则我如何使用 LaTeX 编码将表格放入页面中?在这种情况下,段落或字体更改是否有效?


\begin{document}

{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{1}{cccccc}}
\hline\hline
                    &\multicolumn{6}{c}{}                                                         \\
                    &       count&        mean&         p50&          sd&         min&         max\\
\hline
Number of working days listed for the factory&       17099&      215.70&      240.00&       80.25&         1.0&         365\\
Total machine power in the factory (horsepower)&       17534&       35.16&        3.00&      194.12&         0.0&        8242\\
Total number of workers&       17333&       62.49&       14.00&      264.01&         0.0&       10219\\
Factory age         &       14120&       20.25&       14.00&       20.73&         0.0&         262\\
Factory is located in a city, according to the indicated administrative division&       17472&        0.44&        0.00&        0.50&         0.0&           1\\
Number of factories in the same district and industry, raw and unscaled&       17468&       34.94&       17.00&       45.51&         1.0&         202\\
Employed Women      &       17333&        0.25&        0.00&        0.43&         0.0&           1\\
Employed Children   &       17333&        0.09&        0.00&        0.29&         0.0&           1\\
Number of adult women in the Factory&       17333&       13.96&        0.00&      104.33&         0.0&        3643\\
Number of children in the factory, whether male or female&       17333&        1.16&        0.00&        9.01&         0.0&         341\\
\hline
Observations        &       17534&            &            &            &            &            \\
\hline\hline
\multicolumn{7}{l}{\footnotesize The outcome variable is the log of the factory’s number of working days. The Tobit model in all regressions is right censored at 6, because log(365) is approximately equal to 6.}\\
\end{tabular}
}
\end{document} 

答案1

对于您的表我建议使用tabularray包:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
    \begin{table}[ht]
    \centering
\begin{talltblr}[
caption = {unknown},
  label = {tab:???},
remark{Note} = {The outcome variable is the log of the factory’s number of working days. The Tobit model in all regressions is right censored at 6, because $\log(365)$ is approximately equal to 6.}
                ]{
    colspec = {@{} X[l, cmd={\hangafter=1\hangindent=1em}] 
                        Q[c, si={table-format=5.0}]
                   *{3}{Q[c, si={table-format=3.2}]}
                        Q[c, si={table-format=1.1}]
                        Q[c, si={table-format=5.0}]
               @{}},
     rowsep = 3pt
                }
    \toprule
    & {{{count}}}   & {{{mean}}}    & {{{p50}}} & {{{sd}}}  & {{{min}}} & {{{max}}} \\
    \midrule
Number of working days listed for the factory
    & 17099         & 215.70        & 240.00    &  80.25    & 1.0       &   365 \\
Total machine power in the factory (horsepower)
    & 17534         &  35.16        &   3.00    & 194.12    &  0.0      &  8242 \\
Total number of workers
    & 17333         & 62.49         &  14.00    & 264.01    &  0.0      & 10219 \\
Factory age         
    & 14120         & 20.25         &  14.00    &  20.73    &  0.0      &   262 \\
Factory is located in a city, according to the indicated administrative division
    & 17472         &  0.44         &   0.00    &   0.50    &  0.0      &     1 \\
Number of factories in the same district and industry, raw and unscaled
    & 17468         & 34.94         & 17.00     &  45.51    & 1.0       &   202 \\
Employed Women      
    & 17333         &  0.25         &  0.00     &   0.43    & 0.0       &     1 \\
Employed Children   
    & 17333         &  0.09         &  0.00     &   0.29    & 0.0       &     1 \\
Number of adult women in the Factory
    & 17333         & 13.96         &  0.00     & 104.33    & 0.0       &  3643 \\
Number of children in the factory, whether male or female
    & 17333         &  1.16         &  0.00     &   9.01    & 0.0       &   341 \\
    \midrule
Observations        
    & 17534&        &           &           &            &              &       \\
    \bottomrule
\end{talltblr}
    \end{table}
\end{document}

如果您喜欢在表中注释某些值,而不是\sym在 MWE 中定义,而是使用包TblrNote中的命令tabularray

在此处输入图片描述

答案2

表格太宽而无法放入文本块有两个原因。首先,您不允许第一列自动换行。其次,图例中的文本太长,无法放入文本块中的一行。为了解决前一个问题,我建议您使用环境tabularx而不是tabular环境,并使用X第一列的列类型。为了解决第二个问题,我建议您从l列类型切换到p列类型,宽度为\textwidth。此外,我会将数据列中的数字与其(显式或隐式)小数标记对齐。

在此处输入图片描述

\documentclass{article} % or some other suitable document class
\usepackage[T1]{fontenc}
\usepackage{tabularx,ragged2e,booktabs,siunitx}
\newcolumntype{L}{>{\RaggedRight\hangafter=1\hangindent=1em}X}
\newcolumntype{T}[1]{S[table-format=#1]}
\begin{document}

%\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} %% not needed
\setlength\tabcolsep{4pt} % default value: 6pt  %% optional
\noindent %<-- important
\begin{tabularx}{\textwidth}{@{} L c 
    *{3}{T{3.2}} 
    T{1.0,round-precision=0,round-mode=places} 
    T{5.0,group-digits=false} @{}}
\toprule
 & count & {mean} & {p50} & {sd} & {min} & {max}\\
\midrule
Number of working days listed for the factory& 17099& 215.70& 240.00& 80.25& 1.0& 365\\
Total machine power in the factory (horsepower)& 17534& 35.16& 3.00& 194.12& 0.0& 8242\\
Total number of workers& 17333& 62.49& 14.00& 264.01& 0.0& 10219\\
Factory age & 14120& 20.25& 14.00& 20.73& 0.0& 262\\
Factory is located in a city, according to the indicated administrative division& 17472& 0.44& 0.00& 0.50& 0.0& 1\\
Number of factories in the same district and industry, raw and unscaled& 17468& 34.94& 17.00& 45.51& 1.0& 202\\
Employed Women & 17333& 0.25& 0.00& 0.43& 0.0& 1\\
Employed Children  & 17333& 0.09& 0.00& 0.29& 0.0& 1\\
Number of adult women in the Factory& 17333& 13.96& 0.00& 104.33& 0.0& 3643\\
Number of children in the factory, whether male or female& 17333& 1.16& 0.00& 9.01& 0.0& 341\\
\addlinespace
Observations & 17534& & & & & \\
\bottomrule
\addlinespace
\multicolumn{7}{@{}p{\textwidth}@{}}{\footnotesize The outcome variable is the (natural) log of the factory’s number of working days. The Tobit model in all regressions is right-censored at 6, because $\ln(365)\approx5.6<6$.}\\
\end{tabularx}

\end{document} 

相关内容