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.


7
  • 3
    Welcome! The question could be improved by adding one example of how such a table might look like.
    – mickep
    Commented Dec 21, 2022 at 9:38
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Dec 21, 2022 at 9:51
  • If I guess you use dcolumn package to define D and guess a document size in which to fit the table, your example makes ! Extra alignment tab has been changed to \cr. so no real output. Please provide an error free example that makes a table to be resized Commented Dec 21, 2022 at 12:14
  • 1
    oh did you intend Spread (10Y\&3M) with a literal & there? Commented Dec 21, 2022 at 12:29
  • 1
    I added the \& fix to your table but it easily fits, what do you want to change? Or are you on a smaller page? Please edit the example Commented Dec 21, 2022 at 12:33

2 Answers 2

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

1
  • Thank you very much for your comments. Commented Dec 29, 2022 at 5:53
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .