2017-04-17 57 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 。 –