Tables Input – Fit Tables Read in with \input to Page-Width

inputtables

I'm trying to fit tables to page-width that I include in the .tex document via the /input method. I'm doing this because I'm exporting tables from Stata to Latex with the estout methods, and include these tables into my document. However, some tables are too wide and I'd like to scale them down to make them fit.

I think this question is different from many questions that ask to fit tables because I'm calling the table through the /input{} method. However, I'm a beginner in Latex and might just not have tried the right things yet.

Ideally, i don't want to change the stata latex output manually but rather have either a) some option in Stata estout or b) some latex code in the main document.

Thanks in advance for any help!

The main document:

\documentclass[•]{article}
\usepackage{booktabs}

\begin{document}

\input{tables/desstat_behav_corr}

\end{document}

And the input document:

\begin{table}[htbp]
\centering 
\caption{Pairwise correlations of behavioural variables}
\begin{tabular*}{\textwidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{8}{c}} 
\toprule &\multicolumn{8}{c}{}   \\
      &  ceo\_age&   cfo\_age&CapIQ\_CEOfixed\_to\_total&CapIQ\_CEObonus\_to\_total&CapIQ\_CEOlongterm\_to\_total&CapIQ\_CFOfixed\_to\_total&CapIQ\_CFObonus\_to\_total&CapIQ\_CFOlongterm\_to\_total\\
\midrule
ceo\_age   &        1&         &         &         &         &         &         &          \\
cfo\_age   &    0.260&        1&         &         &         &         &         &         \\
CapIQ\_CEOfixed\_to\_total&   0.0202&  -0.0563&        1&         &         &         &         &         \\
CapIQ\_CEObonus\_to\_total&   0.0323&   0.0844&   -0.442&        1&         &         &         &         \\
CapIQ\_CEOlongterm\_to\_total&  0.00911&   0.0740&   -0.316&   0.0130&        1&         &         &         \\
CapIQ\_CFOfixed\_to\_total&  -0.0748&  -0.0879&    0.673&   -0.308&   -0.279&        1&         &         \\
CapIQ\_CFObonus\_to\_total&   0.0708&   0.0534&   -0.380&    0.794&  -0.0360&   -0.361&        1&         \\
CapIQ\_CFOlongterm\_to\_total&   0.0766&   0.0395&   -0.318&  -0.0245&    0.814&   -0.325&  -0.0415&        1\\
\bottomrule
\end{tabular*}
\end{table}

Best Answer

This answer is meant to address the follow-up question in the comments section on "is there a method to scale the table down without touching the code in the /input section? 'tabular*' is generated automatically by the Stata export command". It may or may not be compatible with what the OP is trying to accomplish.

First, let me reiterate the disclaimers:

1) The table is poorly conceptualized, because of the excessive width of the column headers;

2) if one wishes to scale a table, one should use tabular, rather than tabular* because the tabular* has no idea, in advance, if the table is oversized, and will not provide an acceptable output is scaling is required;

3) Any scaling can only be done on the inner tabular part of the table, and not on the table itself, because table is a float and cannot be scaled as an object.

That said, what I do here is wrap the tabular in a macro that I call \totextwidth which will scale the tabular (either scale down or leave alone) to fit \textwidth. I use the floating point fp package to calculate the scale factor, and use \scalebox of the graphicx package to resize the tabular. It uses an egreg trick to place lengths into count registers.

I have verified that this approach works if you stick the part between \begin{table} and \end{table} into a separate file and use \input. EDITED to not scale narrow tables up to \textwidth, but only to scale wide tables down to \textwidth.

\documentclass[•]{article}
\usepackage{fp}
\usepackage{graphicx}
\usepackage{booktabs}
\newsavebox\mytabularbox
\newcount\figwidthc
\newcount\textwidthc

\newcommand\totextwidth[1]{%
  \sbox{\mytabularbox}{#1}%
  \figwidthc=\wd\mytabularbox%
  \textwidthc=\textwidth%
  \FPdiv\scaleratio{\the\textwidthc}{\the\figwidthc}%
  \FPmin\scaleratio{\scaleratio}{1}%
  \scalebox{\scaleratio}{\usebox{\mytabularbox}}%
}
\begin{document}

%\input{tables/desstat_behav_corr}
\begin{table}[htbp]
\centering 
\caption{Pairwise correlations of behavioural variables}
\totextwidth{%
\begin{tabular}{@{\hskip\tabcolsep\extracolsep\fill}l*{8}{c}} 
\toprule &\multicolumn{8}{c}{}   \\
      &  ceo\_age&   cfo\_age&CapIQ\_CEOfixed\_to\_total&CapIQ\_CEObonus\_to\_total&CapIQ\_CEOlongterm\_to\_total&CapIQ\_CFOfixed\_to\_total&CapIQ\_CFObonus\_to\_total&CapIQ\_CFOlongterm\_to\_total\\
\midrule
ceo\_age   &        1&         &         &         &         &         &         &          \\
cfo\_age   &    0.260&        1&         &         &         &         &         &         \\
CapIQ\_CEOfixed\_to\_total&   0.0202&  -0.0563&        1&         &         &         &         &         \\
CapIQ\_CEObonus\_to\_total&   0.0323&   0.0844&   -0.442&        1&         &         &         &         \\
CapIQ\_CEOlongterm\_to\_total&  0.00911&   0.0740&   -0.316&   0.0130&        1&         &         &         \\
CapIQ\_CFOfixed\_to\_total&  -0.0748&  -0.0879&    0.673&   -0.308&   -0.279&        1&         &         \\
CapIQ\_CFObonus\_to\_total&   0.0708&   0.0534&   -0.380&    0.794&  -0.0360&   -0.361&        1&         \\
CapIQ\_CFOlongterm\_to\_total&   0.0766&   0.0395&   -0.318&  -0.0245&    0.814&   -0.325&  -0.0415&        1\\
\bottomrule
\end{tabular}%
}
\end{table}

\begin{table}[htbp]
\centering
\caption{Narrow table}
\totextwidth{%
\begin{tabular}{|c|}
\hline
Narrow table\\
\hline
\end{tabular}
}%
\end{table}

\end{document}

enter image description here