Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

r - Fonts for Rmarkdown document

Here is a minimal working example.

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

My query is the following: What are the default fonts for an Rmarkdown document and how can I change them ?

While researching this I came across this page :

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

Is it correct that there are 4 fonts (mainfont/sansfont/monofont/mathfont) for describing 4 categories of output in Rmarkdown ? What are their default values and how can I change them ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

LaTeX is used when you create a PDF file. And the default font used in LaTeX is Computer Modern. There are various ways to change the fonts used n LaTeX, but the required names are often not intuitive if one does not know LaTeX. An easier solution is to use mainfont etc. together with xelatex or lualatex as engine. You can define these options at the top level in the yml header using standard font names for your platform. Here your example document using Liberation Serif as main font:

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

The first table uses the mono font, i.e. Liberation Mono, since it is normal R output. The second table uses the main font again. See the documentation for more details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...