2

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

Tex code generated from Stargazer does not adjust the table's width, so it is often too long or narrow.

A short example of the original Stargazer code is as follows. You could offer advice applied to the code. That is, how to slightly revise the code so that the table width fits the page width in a way not scaling the table.

\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}}lD{.}{.}{-3} } 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
\\[-1.8ex] & \multicolumn{1}{c}{Duration Gap in 0.5M} \\ 
\hline \\[-1.8ex] 
  Constant & 8.496^{***} \\ 
  & (0.060) \\ 
  & \\ 
\hline \\[-1.8ex] 
Observations & \multicolumn{1}{c}{2,241} \\ 
R$^{2}$ & \multicolumn{1}{c}{0.820} \\ 
Adjusted R$^{2}$ & \multicolumn{1}{c}{0.820} \\ 
\hline 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table} 

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

Other solutions using different packages (i.e. tabularX), seem to need many changes in the code generated from Stargazer. Is there a way to get page-width tables using the Stargazer code without making many changes?

starball
  • 20,030
  • 7
  • 43
  • 238

1 Answers1

0

Start from here

library(stargazer)
linear.1 <- lm(mpg ~ cyl + disp + hp, data=mtcars)
linear.2 <- lm(mpg ~ log(cyl) + disp + hp, data=mtcars)
stargazer(linear.1, linear.2, type="latex")

% Table created by stargazer v.5.2.3 by Marek Hlavac, Social Policy Institute. E-mail: marek.hlavac at gmail.com
% Date and time: Mi, Jan 11, 2023 - 17:06:46
\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}}lcc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & \multicolumn{2}{c}{\textit{Dependent variable:}} \\ 
\cline{2-3} 
\\[-1.8ex] & \multicolumn{2}{c}{mpg} \\ 
\\[-1.8ex] & (1) & (2)\\ 
\hline \\[-1.8ex] 
 cyl & $-$1.227 &  \\ 
  & (0.797) &  \\ 
  & & \\ 
 log(cyl) &  & $-$7.833$^{*}$ \\ 
  &  & (4.136) \\ 
  & & \\ 
 disp & $-$0.019$^{*}$ & $-$0.018$^{*}$ \\ 
  & (0.010) & (0.010) \\ 
  & & \\ 
 hp & $-$0.015 & $-$0.014 \\ 
  & (0.015) & (0.014) \\ 
  & & \\ 
 Constant & 34.185$^{***}$ & 40.165$^{***}$ \\ 
  & (2.591) & (5.139) \\ 
  & & \\ 
\hline \\[-1.8ex] 
Observations & 32 & 32 \\ 
R$^{2}$ & 0.768 & 0.777 \\ 
Adjusted R$^{2}$ & 0.743 & 0.753 \\ 
Residual Std. Error (df = 28) & 3.055 & 2.996 \\ 
F Statistic (df = 3; 28) & 30.877$^{***}$ & 32.488$^{***}$ \\ 
\hline 
\hline \\[-1.8ex] 
\textit{Note:}  & \multicolumn{2}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
\end{tabular} 
\end{table} 

and change

\begin{tabular}{@{\extracolsep{5pt}} 

to

\begin{tabular*}{\textwidth}{c @{\extracolsep{\fill}}

Note that you have an option inside stargazer to adjust column width, I get a very similar result within stargazer via:

stargazer(linear.1, linear.2, type="latex", column.sep.width = "120pt")
Marco
  • 2,368
  • 6
  • 22
  • 48
  • Thank you very much, Marco. This is very helpful. Appreciate taking the time to write the answer. (I couldn't vote because of no enough reputation...) – Qianlong Liu Jan 19 '23 at 01:17