1

Is there any straightforward solution to get a sans serif font in rmarkdown PDF tables? The font in normal text, code, and equations should stay unchanged. I'm demanding on xtables.

Example:

---
title: ''
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
```

```{r ranking tradeoff, results='asis', message=FALSE, echo=FALSE}
library(xtable)
print(xtable(mtcars[1:2, ])
      , include.rownames=TRUE
      , comment=FALSE
      )
```

The text in the table should appear in sans serif.

rif

I tried this solution but it didn't work for me. The fonts were not found, albeit the powershell font check turned out differently. However the fonts are named e.g. "Liberation Sans" and not "LiberationSans". Hence I tried to specify sansfont: "Liberation Sans" without success. Also there appears to be a package clash asmath vs mathtools when using xelatex.

jay.sf
  • 60,139
  • 8
  • 53
  • 110

1 Answers1

2

You could define a new environment for the font and use it with the latex.environments option of print.xtable.

\newenvironment{myfont}{\sffamily}{}

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
```

```{r ranking tradeoff, results='asis', message=FALSE, echo=FALSE}
library(xtable)
print(xtable(mtcars[1:2, ])
      , include.rownames=TRUE
      , comment=FALSE
      , latex.environments = "myfont"
      )
```
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • This works great with minimum effort, thanks! However, one of my tables are two xtables side by side, where this solution won't work. Do you know a way to extend it to [this solution](https://stackoverflow.com/a/25414213/6574038), or perhaps something that would work? – jay.sf Feb 08 '19 at 11:28
  • 1
    @jay.sf I would try to put `\sffamily` just after `\begin{table}[ht]`. – Stéphane Laurent Feb 08 '19 at 11:40