# Out of the box solution to make tables fit page

Hi I am attempting to write my thesis in Latex with no latex experience. I have generally handled the text and math functions in latex, I have really struggled with the tables.

I am outputting regressions from python using the stargazer package for python.

The latex table outputed from the stargazer package is no good. The tables output are too wide. And there appears to be a bug in the package where an incorrect number of columns are called for. I would like to correct both the columns and formatting of the table with a function called above the main table.

I have five regressions to report. But I have performed the same analysis on a range of datasets.

I would like a way to write one function that adjusts all possible tables of the same format. I.e. with the same exact equations just different results. This would enable me to quickly report many tables of results.

here is an example of the code that i would like to rework in an iterable fashion:

Is there a good way to do this.

\begin{table}[!htbp] \centering
\caption{Merton Selection Algorithm Results}
\label{}
\begin{tabular}{@{\extracolsep{5pt}}lcc} %%% THIS IS WRONG
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{5}{c}{\textit{Dependent variable:}} \
\cr \cline{5-6}
\\[-1.8ex] & (1) & (2) & (3) & (4) & (5) \\
\hline \\[-1.8ex]
Constant or Alpha & 0.00368$^{***}$ & 0.00235$^{**}$ & 0.00463$^{***}$ & 0.00331$^{***}$ & 0.00332$^{***}$ \\
& (0.00092) & (0.00092) & (0.00104) & (0.00096) & (0.00088) \\
& & & & & \\
Index & 0.24083$^{***}$ & & & & 0.20465$^{***}$ \\
& (0.03294) & & & & (0.05731) \\
& & & & & \\
Best Random & & 0.30741$^{***}$ & & & 0.18873$^{***}$ \\
& & (0.03621) & & & (0.04733) \\
& & & & & \\
Worst Random & & & 0.08992$^{**}$ & & -0.17774$^{***}$ \\
& & & (0.04332) & & (0.05362) \\
& & & & & \\
Mean Randoms & & & & 0.37352$^{***}$ & 0.07392$^{}$ \\
& & & & (0.05704) & (0.11988) \\
& & & & & \\
\hline \\[-1.8ex]
Observations & 156.0 & 156.0 & 156.0 & 156.0 & 156.0 \\
R${2}$ & 0.25768 & 0.31885 & 0.02721 & 0.21783 & 0.40213 \\
Adjusted R${2}$ & 0.25286 & 0.31442 & 0.02089 & 0.21275 & 0.3863 \\
Residual Std. Error & 0.01132(df = 154.0) & 0.01084(df = 154.0) & 0.01296(df = 154.0) & 0.01162(df = 154.0) & 0.01006(df = 151.0)  \\
F Statistic & 53.45841$^{***}$ (df = 1.0; 154.0) & 72.0871$^{***}$ (df = 1.0; 154.0) & 4.30756$^{**}$ (df = 1.0; 154.0) & 42.88879$^{***}$ (df = 1.0; 154.0) & 25.39116$^{***}$ (df = 4.0; 151.0) \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{5}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}

• someone posted stargazer table output here the other day, it really is poor tex markup and should be pushed back upstream to see if the generator can be improved. \hline \\  is always wrong there should never be \\  after \hline and then it puts in negative spacing adding further horrors to try to cover up the initial issue. – David Carlisle Apr 5 '20 at 20:09
• all your rows have 5 & so that is 6 columns, but you have only declared three lcc so at the very least use lccccc to get things working (centred columsn aren't best for numeric entries but you need something – David Carlisle Apr 5 '20 at 20:25

I propose these improvements: S column type for the last 5 columns, to have an alignment of numbers of the decimal dot, rules from booktabs to have some vertical padding around them, loading of caption to control the spacing between caption and table, and ofmakecell to enable linebreaking in standard cells.

I took the liberty to centre the Dependent variable text w.r.t. the last two cells, as it was consistent with the short line below, but I'm not sure it was judicious. Anyway, it is easy to modify.

\documentclass{article}
\usepackage{geometry}
\usepackage{array, makecell, booktabs, caption}
\usepackage{siunitx}
\captionsetup{skip=6pt}

\begin{document}

\begin{table}[!htbp]
\sisetup{table-format=-1.5, table-number-alignment=center, table-space-ext-pre=(),%
table-align-text-pre=false, table-space-text-post=***}
\setlength{\tabcolsep}{3pt}
\renewcommand{\cellalign}{tc}
\centering
\caption{Merton Selection Algorithm Results}
\label{}
\small
\begin{tabular}{@{}l*6{S}} %
\toprule\midrule[0.4pt]
& & & & \multicolumn{2}{c}{\textit{Dependent variable:}} \\
\cmidrule{5-6}
& {(1) }& {(2)} & {(3)} & {(4)} & {(5)} \\
\midrule
Constant or Alpha & 0.00368$^{***}$ & 0.00235$^{**}$ & 0.00463$^{***}$ & 0.00331$^{***}$ & 0.00332$^{***}$ \\
& {(}0.00092{)} & {(}0.00092{)} &{(}0.00104{)} & {(}0.00096{)} & {(}0.00088{)} \\
& & & & & \\
Index & 0.24083$^{***}$ & & & & 0.20465$^{***}$ \\
& {(}0.03294{)} & & & & {(}0.05731{)} \\
& & & & & \\
Best Random & & 0.30741$^{***}$ & & & 0.18873$^{***}$ \\
& & {(}0.03621{)} & & & {(}0.04733{)} \\
& & & & & \\
Worst Random & & & 0.08992$^{**}$ & & -0.17774$^{***}$ \\
& & & {(}0.04332{)} & & {(}0.05362{)} \\
& & & & & \\
Mean Randoms & & & & 0.37352$^{***}$ & 0.07392$^{}$ \\
& & & & {(}0.05704{)} & {(}0.11988{)} \\
\midrule
Observations & {156.0} & {156.0} & {156.0} & {156.0} & {156.0} \\
R${2}$ & {0.25768} & {0.31885} & {0.02721} & {0.21783} & {0.40213} \\
Adjusted R${2}$ & {0.25286} & {0.31442} & {0.02089} & {0.21275} & {0.3863\phantom{0}} \\
\makecell[l]{F Statistic\\ df = } &{\makecell{53.45841$^{***}$\\ (1.0; 154.0)}} & {\makecell{72.0871$^{***}$\\ (1.0; 154.0)}} & {\makecell{4.30756$^{**}$\\ (1.0; 154.0)}} & {\makecell{42.88879$^{***}$ \\(1.0; 154.0)}} & {\makecell{25.39116$^{***}$\\ (4.0; 151.0)}} \\
\multicolumn{6}{@{}>{$}l<{$}}{\textit{Note:}\quad^{*} p<0.1;\enspace ^{**}p < 0.05;\enspace ^{***}p < 0.01} \\
`