Rmarkdown 文档的字体

use*_*823 7 pdf yaml r pandoc r-markdown

这是一个最小的工作示例。

---
date : 2018-May-26
output:
    pdf_document
title: "Testing Rmarkdown"
---

```{r,comment = NA}

Gender <- gl(2,1000,labels = c("Men","Women"))
SmokerM <- sample(c("Y","N"),1000,replace = T , prob = c(.3,.7))
SmokerW <- sample(c("Y","N"),1000,replace = T , prob = c(.5,.5))
Smoker <- c(SmokerM,SmokerW)

mydata  <- data.frame(Gender,Smoker)
table(mydata$Gender,mydata$Smoker)

```

This is a text in the body of the document.What font is this ? What is
font for the output of table ? How can we change these 2 fonts ? What 
other categories of items are there in an Rmarkdown which have different
fonts ?       
Run Code Online (Sandbox Code Playgroud)

我的查询如下: Rmarkdown 文档的默认字体是什么以及如何更改它们?

在研究这个时,我遇到了这个页面:

[Pandoc 变量][1] http://pandoc.org/MANUAL.html#variables-for-latex

是否有 4 种字体(mainfont/sansfont/monofont/mathfont)用于描述 Rmarkdown 中的 4 类输出?它们的默认值是什么,我该如何更改它们?

Ral*_*ner 9

创建 PDF 文件时使用 LaTeX。LaTeX 中使用的默认字体是 Computer Modern。有多种方法可以更改 LaTeX 中使用的字体,但如果不了解 LaTeX,所需的名称通常不直观。一个更简单的解决方案是将mainfontetc. 与引擎一起使用xelatexlualatex作为引擎使用。您可以yml使用平台的标准字体名称在标题的顶层定义这些选项。这里使用 Liberation Serif 作为主要字体的示例文档:

---
date : 2018-May-26
output:
    pdf_document:
        latex_engine: xelatex
mainfont: LiberationSerif
sansfont: LiberationSans
monofont: LiberationMono
title: "Testing Rmarkdown"
---

```{r,comment = NA}

Gender <- gl(2,1000,labels = c("Men","Women"))
SmokerM <- sample(c("Y","N"),1000,replace = T , prob = c(.3,.7))
SmokerW <- sample(c("Y","N"),1000,replace = T , prob = c(.5,.5))
Smoker <- c(SmokerM,SmokerW)

mydata  <- data.frame(Gender,Smoker)
table(mydata$Gender,mydata$Smoker)
knitr::kable(table(mydata$Gender,mydata$Smoker))
```

This is a text in the body of the document.What font is this ? What is
font for the output of table ? How can we change these 2 fonts ? What 
other categories of items are there in an Rmarkdown which have different
fonts ?   
Run Code Online (Sandbox Code Playgroud)

第一个表使用mono字体,即Liberation Mono,因为它是正常的R输出。第二个表再次使用主字体。有关更多详细信息,请参阅文档

  • 亲爱的拉尔夫, 非常感谢您的回答。似乎mainfont是PDF中的主要字体,monofont是代码块的输出字体。我猜 mathfont 是用于输出数学方程的字体。这样对吗 ?sansfont 对应什么?你能指点我列出这个术语的文档吗?另外,我在这里发布了一个后续查询。[链接](/sf/ask/3538497301/ ) (3认同)
  • @ user2338823 你是对的。Sansfont 用于*sans serif* 部分​​,例如通过`\textsf{...}`。有关详细信息,您可能需要 LaTeX 包 fontspec(带有 TeXlive `texdoc fontspec`)的文档以及 LaTeX 介绍。 (2认同)