3

I want to print a latex table generated with stargazer() in monospaced font, and I want to do it in a reproducible way with knitr (i.e., no manual latex coding). I tried to define an environment called mymono and then wrap the knitr chunk in this environment via \begin{} and \end{}. It does not work; the table prints in the default font style.

\documentclass{article} 
\newenvironment{mymono}{\ttfamily}{\par}
\begin{document}   
<<lm, echo=FALSE>>=  
df <- data.frame(x=1:10, y=rnorm(10))
library(stargazer)  
lm1 <- lm(y ~ x ,data=df)  
@

% reproducible
\begin{mymono}
<<table_texstyle, echo=FALSE, results='asis', message=FALSE>>=  
stargazer(lm1, label="test")  
@  
\end{mymono}

\end{document}

I don't think there is a font setting in stargazer() except for font.size.

# > sessionInfo()
# R version 3.0.2 (2013-09-25)
# Platform: x86_64-apple-darwin10.8.0 (64-bit)
# other attached packages:
# [1] stargazer_5.1

Even better than wrapping the entire table{} in the new font style would be to wrap just tabular{} so that the caption remains the default style. I don't know if there is a way to insert latex code into the stargazer() output programmatically.

Eric Green
  • 7,385
  • 11
  • 56
  • 102
  • You could `capture.output()` the stargazer result and then edit it, inserting the lines you want where you want them. – Gregor Thomas Oct 08 '14 at 18:55
  • Yes, but that would not be reproducible. Or are you thinking of some other way to automate this? – Eric Green Oct 08 '14 at 21:11
  • Why wouldn't it be reproducible? Write a little function that does it. – Gregor Thomas Oct 08 '14 at 21:15
  • `capture.output()` is clear. It's not obvious to me how to write the function to then take this character vector and automatically add the latex code in the correct spots. Maybe something that replaces `\begin{tabular}` with `\begin{mymono}\begin{tabular}` and replaces `\end{tabular}` with `\end{tabular}\end{mymono}`. I'll have to think more about this. I'm still curious as to why the approach in my original post does not work. It would be easier in some ways to tweak this if I've made some small mistake. – Eric Green Oct 08 '14 at 21:24

1 Answers1

2

Too long for a comment, so here's a start of an answer. Using the model from the question:

\documentclass{article} 
\begin{document}  

<<lm, echo=FALSE, message=FALSE, include=FALSE>>=
df <- data.frame(x=1:10, y=rnorm(10))
library(stargazer)  
lm1 <- lm(y ~ x ,data=df)  

tabular_tt <- function(orig.table) {
    library(stringr)
    tabular.start <- which(str_detect(orig.table, "begin\\{tabular\\}"))
    tabular.end <- which(str_detect(orig.table, "end\\{tabular\\}"))
    new.table <- c(orig.table[1:(tabular.start - 1)],
                   "\\texttt{",
                   orig.table[tabular.start:tabular.end],
                   "}",
                   orig.table[(tabular.end + 1):length(orig.table)])
    return(new.table)
}
@

<<print, results='asis', echo=FALSE>>=
cat(tabular_tt(capture.output(stargazer(lm1, label="test"))), sep="\n")
@

\end{document}

You can easily adjust it as necessary. If you're having trouble, I would make sure your target LaTeX syntax is correct by playing with a small toy table in LaTeX only, maybe playing with the tex file generated when you knit.

You may need to make the function cat(new.table, sep = "\n") to get it to get the output correctly into the knitr document.

Eric Green
  • 7,385
  • 11
  • 56
  • 102
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thanks, @Gregor. I think `browser()` is making the code break for me. Running the following will give me the correct output to pass to your function: `cat(capture.output(stargazer(lm1, label="test")), sep="\n")`. With `browser()`, it breaks. Without it, I get `Error in 1:(tabular.start - 1) : argument of length 0`. – Eric Green Oct 08 '14 at 21:56
  • Whoops, had `browser()` in there for debugging. I haven't tried in a Rnw document, but in my R console it works as-is in my question now. – Gregor Thomas Oct 08 '14 at 22:02
  • I had to make a small change (adding third `t` to `\textt`) to get it working. Once it did, I added the knitr chunk code to produce the pdf. Looks great. Thanks a lot. – Eric Green Oct 08 '14 at 22:20
  • @EricGreen Glad to hear it helped! `\texttt` vs `\textt`, that's a mistake I've made many times before :( – Gregor Thomas Oct 08 '14 at 22:45