1

I would like to change the font in a texreg produced table. I'm knitting the table RStudio's Rmarkdown, so modifying the LaTeX directly is not an option.

Here's an example. The headings, coefficient names, and some of the results are printed in roboto. Other results are not. I'd like to make all numbers roboto or inconsolata. Suggestions?

I'd also like to make the table notes 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

Eric Green
  • 7,385
  • 11
  • 56
  • 102

1 Answers1

0

I'm not familiar enough with manipulating fonts in LaTeX to give you a complete answer, but hopefully this will get you much closer to your goal.

The basic idea is to manipulate the input/output of texreg to give you what you want since texreg itself lacks these capabilities.

In your case, I think you can accomplish what you need by only manipulating the input, but the way to manipulate the output is to use capture.output like:

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

And use regex/whatever to fix up the output there.

I'm just going to use texttt to exemplify the approach:

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

Will get the coefficient name column font to be customized:

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

If you want to manipulate the font of the table notes, use the custom.note argument:

texreg(list(model.1, model.2), custom.note ='\\texttt{Block font note}')
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
  • Thanks, @MichaelChirico. Very informative. Your idea about modifying the output got me thinking of another approach: saving the tex table to a file and then calling `\input{}` in the Rmd file. [This answer on Tex](https://tex.stackexchange.com/a/286772/30017) seems promising... – Eric Green Apr 25 '17 at 23:06