如何在 PDF 中为 xtable 获取无衬线字体?

How to get sans serif font for xtable in PDFs?

是否有任何直接的解决方案可以在 rmarkdown PDF tables 中获得无衬线字体?普通文本、代码和方程式中的字体应保持不变。我要求 xtables

示例:

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

table 中的文字应以无衬线字体显示。

我试过 this solution but it didn't work for me. The fonts were not found, albeit the powershell font check 结果不同。但是,字体被命名为例如"Liberation Sans" 而不是 "LiberationSans"。因此我试图指定 sansfont: "Liberation Sans" 但没有成功。使用 xelatex.

时似乎也存在包冲突 asmathmathtools

您可以为字体定义一个新的环境,并将其与 print.xtablelatex.environments 选项一起使用。

\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"
      )
```