Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 991
user2338823 Avatar asked May 26 '18 06:05

user2338823


People also ask

What font does RMarkdown use?

The RMarkdown output is based on LaTeX, and the default LaTeX font is Computer Modern. You will need to download and install the font on your computer. The CMU Serif version of this font is probably the one you want.

How do I change the font in RMarkdown?

To change the font size, you don't need to know a lot of html for this. Open the html output with notepad ++. Control F search for "font-size". You should see a section with font sizes for the headers (h1, h2, h3,...).

What fonts are available in LaTeX?

Computer Modern (default in standard LaTeX classes): CM Roman, CM Sans Serif, CM Typewriter. Latin Modern: LM Roman, LM Sans Serif, LM Typewriter, LM Dunhill. Post Script Fonts: Times, Utopia/Fourier, Palatino, Bookman, Helvetica, Courier. TeX Gyre.

How do you change the font color on RMarkdown?

The Markdown syntax has no built-in method for changing text colors. We can use HTML and LaTeX syntax to change the formatting of words: For HTML, we can wrap the text in the <span> tag and set color with CSS, e.g., <span style="color: red;">text</span> . For PDF, we can use the LaTeX command \textcolor{}{} .


1 Answers

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.

like image 65
Ralf Stubner Avatar answered Nov 05 '22 23:11

Ralf Stubner