问题
Is it possible to incorporate resizebox into stargazer in R? I'm trying to create a table that is too wide to fit, even on landscape perspective. I manually added resizebox{\textwidth}{!} { \begin{tabular} \end{tabular} } to the .tex file, and I like how it looks. However, I'd like for my .Rnw file to be complete, so that I can generate pdf perfectly without additional changes to the .tex file.
On a related note, stargazer causes pdflatex to output multiple warnings due to it including \label{} when no label is specified. These don't affect the pdf's creation, but they cause confusion when there are other errors present.
Again, I can manually delete these lines from the .tex file, or assign label names. However, I would like to simply tell stargazer not to include this line at all.
回答1:
It's not ideal, but you can manually manipulate the LaTeX code output from stargazer with capture.output()
and gsub()
.
table <- capture.output({ # Store the stargazer output in a string
stargazer(iris, header=F) # e.g.
})
table <- gsub("\\begin{tabular}","\\resizebox{0.9\\textwidth}{!}{\\begin{tabular}", table,fixed=T)
table <- gsub("\\end{tabular}","\\end{tabular}}", table,fixed=T)
cat(table)
You can also extract that procedure into a method if you need it in more than one place.
回答2:
To answer part 2 of your question, you can use label
to label tables. This way you don't have to manually delete the empty \label{} from the .tex
file. You will also be able to reference your tables using \ref{your.table.label}.
stargazer(df, title = "Statistical Summary", label="your.table.label", table.placement = "H")
回答3:
The best way I've found is to use set the stargazer option float to FALSE and then use cat() to manually put the scalebox in the float environment; for example:
<< results='asis', echo = FALSE>>=
cat("\\begin{table}[!htbp]")
cat("\\centering")
cat("\\caption{OLS Regression Results by Metal Level}")
cat("\\label{OLS}")
cat("\\scalebox{.8}{")
stargazer(models$model1OLS,
float = FALSE)
cat("}") # for the end of the scalebox
cat("\\end{table}")
@
Note that you will also have to manually label, center, and caption the table. This will almost surely work with using resizebox instead of scalebox, but I haven't tried.
来源:https://stackoverflow.com/questions/28662394/stargazer-options-resizebox-and-label