2017-04-17 60 views
1

我想更改texreg生成表中的字體。我正在編織表格RStudio的Rmarkdown,因此不能直接修改LaTeX。更改texreg表中的字體

下面是一個例子。標題,係數名稱和一些結果以roboto打印。其他結果不是。我想製作所有的數字roboto或inconsolata。建議?

我也想讓桌子的筆記roboto。

--- 
title: "Untitled" 
header-includes: 
    - \usepackage{fontspec} 
    - \setmonofont[Mapping=tex-text]{inconsolata} 
    - \usepackage[sfdefault]{roboto} 
    - \renewcommand{\familydefault}{\sfdefault} 
output: 
    pdf_document: 
    latex_engine: xelatex 
--- 

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo = F) 
library(nlme) 
library(texreg) 
``` 

```{r, results='asis', echo=F} 
model.1 <- lme(distance ~ age, data = Orthodont, random = ~ 1) 
model.2 <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1) 
texreg(list(model.1, model.2)) 
``` 

enter image description here

回答

0

我不熟悉不夠用乳膠處理字體給你一個完整的答案,但希望這將讓你更接近你的目標。

基本的想法是操縱輸入/輸出texreg給你你想要的,因爲texreg本身缺乏這些功能。

在你的情況,我想你可以完成你所需要的只是操縱輸入,但爲管理輸出的方式是使用capture.output像:

tbl = capture.output(texreg(list(model.1, model.2))) 

並使用正則表達式/無論來修復那裏的產量。

我只是將使用texttt來舉例說明方法:

rename_coef = function(reg) { 
    names(reg$coefficients$fixed) = 
    paste0('\\texttt{', names(reg$coefficients$fixed), '}') 
    reg 
} 

model.1 <- rename_coef(lme(distance ~ age, data = Orthodont, random = ~ 1)) 
model.2 <- rename_coef(lme(distance ~ age + Sex, data = Orthodont, random = ~ 1)) 

texreg(list(model.1, model.2)) 

將得到係數name列字體進行定製:

# \begin{table} 
# \begin{center} 
# \begin{tabular}{l c c } 
# \hline 
# & Model 1 & Model 2 \\ 
# \hline 
# \texttt{(Intercept)} & $16.76^{***}$ & $17.71^{***}$ \\ 
#      & $(0.80)$  & $(0.83)$  \\ 
# \texttt{age}   & $0.66^{***}$ & $0.66^{***}$ \\ 
#      & $(0.06)$  & $(0.06)$  \\ 
# \texttt{SexFemale} &    & $-2.32^{**}$ \\ 
#      &    & $(0.76)$  \\ 
# \hline 
# AIC     & 455.00  & 447.51  \\ 
# BIC     & 465.66  & 460.78  \\ 
# Log Likelihood  & -223.50  & -218.76  \\ 
# Num. obs.   & 108   & 108   \\ 
# Num. groups   & 27   & 27   \\ 
# \hline 
# \multicolumn{3}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}} 
# \end{tabular} 
# \caption{Statistical models} 
# \label{table:coefficients} 
# \end{center} 
# \end{table} 

如果你想操作的字體表注意,使用custom.note參數:

texreg(list(model.1, model.2), custom.note ='\\texttt{Block font note}') 
+0

謝謝,@MichaelChirico。非常豐富。您關於修改輸出的想法讓我想到了另一種方法:將tex表保存到文件中,然後在Rmd文件中調用'\ input {}'。 [在Tex上的這個答案](https://tex.stackexchange.com/a/286772/30017)似乎很有希望... –

+0

這個答案解決了我的問題:https://tex.stackexchange.com/a/366745/30017 。 –