1

Is there a way to get page-width tables by slightly revising the code below generated from Stargazer?

The table generated from the code is longer than the page-width. Is there a way to make the table automatically fit the page-width ? By automatically, I mean I don't need to manually set a specific value for the column separate width, or column width, or other width.


\documentclass{article}
\usepackage{dcolumn}

\begin{document}

\begin{table}[!htbp] \centering \caption{Table for test} \label{} \small \begin{tabular}{@{\extracolsep{5pt}}lD{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3} } \[-1.8ex]\hline \hline \[-1.8ex] \[-1.8ex] & \multicolumn{1}{c}{Gap in 1M} & \multicolumn{1}{c}{Gap in 3M} & \multicolumn{1}{c}{Gap in 6M} & \multicolumn{1}{c}{Gap in 9M} & \multicolumn{1}{c}{Gap in 1Y} \ \[-1.8ex] & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} & \multicolumn{1}{c}{(4)} & \multicolumn{1}{c}{(5)}\ \hline \[-1.8ex] Level & -1.521^{*} & -1.414^{} & -1.186^{} & -1.027^{} & -1.348^{} \ & (0.034) & (0.043) & (0.058) & (0.071) & (0.108) \ & & & & & \ Spread (10Y&3M) & -0.711^{} & -0.614^{} & -0.405^{} & -0.203^{} & 0.635^{} \ & (0.024) & (0.031) & (0.043) & (0.053) & (0.086) \ & & & & & \ Constant & 8.265^{} & 7.821^{} & 6.861^{} & 6.082^{} & 5.169^{**} \ & (0.070) & (0.084) & (0.107) & (0.120) & (0.136) \ & & & & & \ \hline \[-1.8ex] Observations & \multicolumn{1}{c}{2,230} & \multicolumn{1}{c}{2,209} & \multicolumn{1}{c}{2,168} & \multicolumn{1}{c}{2,126} & \multicolumn{1}{c}{2,001} \ R$^{2}$ & \multicolumn{1}{c}{0.760} & \multicolumn{1}{c}{0.644} & \multicolumn{1}{c}{0.417} & \multicolumn{1}{c}{0.256} & \multicolumn{1}{c}{0.100} \ Adjusted R$^{2}$ & \multicolumn{1}{c}{0.760} & \multicolumn{1}{c}{0.644} & \multicolumn{1}{c}{0.417} & \multicolumn{1}{c}{0.255} & \multicolumn{1}{c}{0.099} \ \hline \hline \[-1.8ex] \end{tabular} \end{table}

\end{document}

Output of the code above

I found the solutions, such as using \resizebox, but the scales of the table are changed, which is not favourable.

Is there a way to get a page-width table from the code with limited changes in the code, hopefully? Thanks.


2 Answers2

3

Without many changes? Sorry, no.

All those \\[-1.8ex] commands should be removed; double rules are evil; \extracolsep{5pt} does nothing at all.

The & that stands for a printed ampersand should be escaped.

I'd not widen such a table, because it becomes unreadable, unless you're typesetting in two-column format: see the output

enter image description here

Assuming two-column format, I present two realizations. The latter requires more changes, but the output is much more elegant, in my opinion.

\documentclass[twocolumn]{article}
\usepackage{dcolumn,booktabs,caption}

\usepackage{siunitx}

\newcommand{\foo}[1]{\makebox[0pt][l]{$^{#1}$}} % find a better name

\begin{document}

\begin{table}[!htbp] \centering

\caption{A meaningful caption}\label{abcdef}

\begin{tabular}{\columnwidth}{ @{\extracolsep{\fill}} l D{.}{.}{-3} @{} } \toprule & \multicolumn{1}{c@{}}{Duration Gap in 0.5M} \ \midrule Level & -1.588^{} \ & (0.029) \ \addlinespace Spread (10Y&3M) & -0.747^{} \ & (0.021) \ \addlinespace Constant & 8.496^{*} \ & (0.060) \ \midrule Observations & \multicolumn{1}{c}{2,241} \ R$^{2}$ & \multicolumn{1}{c}{0.820} \ Adjusted R$^{2}$ & \multicolumn{1}{c}{0.820} \ \bottomrule \end{tabular}

\end{table}

\begin{table}[!htbp] \centering

\sisetup{ input-open-uncertainty={}, input-close-uncertainty={}, table-align-text-before=false, }

\caption{A meaningful caption}\label{ghij}

\begin{tabular}{\columnwidth}{ @{\extracolsep{\fill}} l S[table-format=(-1.3)] @{} } \toprule & {Duration Gap in 0.5M} \ \midrule Level & -1.588\foo{} \ & (0.029) \ \addlinespace Spread (10Y&3M) & -0.747\foo{} \ & (0.021) \ \addlinespace Constant & 8.496\foo{*} \ & (0.060) \ \midrule Observations & {\hphantom{$-$}2,241} \ R$^{2}$ & {\hphantom{$-$}0.820} \ Adjusted R$^{2}$ & {\hphantom{$-$}0.820} \ \bottomrule \end{tabular}

\end{table}

\end{document}

enter image description here

egreg
  • 1,121,712
0
  • It seems that you will have define table specification manually.
  • According to use marker ^*** at some numbers, I assume, that to table will be added explanation about meaning of those markers. This means, that table be of three part tyle as are threeparttable and talltblr of tabularray package
  • For table I would use tabularray packages with its libraries booktabs and siunitx:
\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}
\NewTableCommand\TN[1]{\TblrNote{#1}}

\begin{document} \begin{table}[ht] \sisetup{ input-open-uncertainty, input-close-uncertainty, table-align-text-pre=false } \begin{talltblr}[ ]{cells = {font=\small}, colsep = 4pt, colspec = { @{} l {5}{X[c, si={table-format=-1.3{\TN{}}}]} @{}}, row{1} = {guard}, row{2-X}= {rowsep=0pt}, row{odd[3-X]} = {belowsep=1ex} } \toprule & {Gap in 1M\(1)} & {Gap in 3M\(2)} & {Gap in 6M\(3)} & {Gap in 9M\(4)} & {Gap in 1Y\(5)} \ \midrule Level
& -1.521\TN{
} & -1.414\TN{} & -1.186\TN{} & -1.027\TN{} & -1.348\TN{} \ & (0.034) & (0.043) & (0.058) & (0.071) & (0.108) \ Spread (10Y&3M) & -0.711\TN{} & -0.614\TN{} & -0.405\TN{} & -0.203\TN{} & 0.635\TN{} \ & (0.024) & (0.031) & (0.043) & (0.053) & (0.086) \ Constant & 8.265\TN{} & 7.821\TN{} & 6.861\TN{} & 6.082\TN{} & 5.169\TN{*} \ & (0.070) & (0.084) & (0.107) & (0.120) & (0.136) \ \midrule Observations & {2,230} & {2,209} & {2,168} & {2,126} & {2,001} \ R$^{2}$ & 0.760 & 0.644 & 0.417 & 0.256 & 0.100 \ Adjusted R$^{2}$ & 0.760 & 0.644 & 0.417 & 0.255 & 0.099 \ \bottomrule \end{talltblr} \end{table} \end{document}

enter image description here

Zarko
  • 296,517