2

I am working with LaTeX and I am trying to fit a large width table into a singular page width. It would appear that the names of the variables are too long and thus causing the table to be 'pushed' out of bounds. Barring the solution of dumbing down the names of the variables themselves, how would I be able to use LaTeX coding to fit the table into the page? Would paragraphing or font changes work in this case?


\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} 
4
  • 1
    Welcome to TeX.SE!
    – Mensch
    May 18 at 21:26
  • 2
    Welcome to SE. Please provide a complete (compilable) code. The answer may depend on the packages you're using, some of them could interact with tabular. Also, it helps when people can just copy-paste your code to test it.
    – Miyase
    May 18 at 21:28
  • To prescribe table width you should use adequate table environments as are for example tabular* or tabularx tables (\begin{tabular*}{\textwidth}{<column specifications>}, \begin{taularx}{\textwidth}{X columns}). Another way way is use p{width} columns and calculate their widths.
    – Zarko
    May 18 at 21:40

2 Answers 2

3

For your table I suggest to use tabularray package:

\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}

If you like to annotate some valu in table, than instead \sym, defined in your MWE, rather use TblrNote command from tabularray package.

enter image description here

1

There are two reasons for why the table is too wide to fit inside the textblock. First, you're not allowing automatic line breaking in the first column. Second, the text in the legend is too long to fit inside a single line within the textblock. To remedy the former issue, I suggest you employ a tabularx environment instead of a tabular environment and use the X column type for the first column. To remedy the second problem, I suggest you switch from the l column type to the p column type, with a width of \textwidth. In addition, I would align the numbers in the data columns on their (explicit or implicit) decimal markers.

enter image description here

\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} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.