簡體   English   中英

使用knitr時stargazer乳膠表的新字體樣式

[英]new font style for stargazer latex table when using knitr

我想用monospaced字體打印用stargazer()生成的乳膠表,我想用knitr以可重復的方式進行(即沒有手動乳膠編碼)。 我嘗試定義一個名為mymono的環境,然后通過\\begin{}\\end{}在這個環境中包裝knitr chunk。 這是行不通的; 表格以默認字體樣式打印。

\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}

除了font.size之外,我認為stargazer()沒有字體設置。

# > 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

甚至比在新的字體樣式中包裝整個table{}更好的方法是只包裝tabular{}以便標題保持默認樣式。 我不知道是否有辦法以編程方式將乳膠代碼插入stargazer()輸出。

評論太長了,所以這是一個答案的開始。 使用問題中的模型:

\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}

您可以根據需要輕松調整它。 如果你遇到麻煩,我會通過在LaTeX中玩一個小玩具桌來確保你的目標LaTeX語法是正確的,也許玩你編織時生成的tex文件。

您可能需要創建函數cat(new.table, sep = "\\n")才能使輸出正確地輸入到knitr文檔中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM