11

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 ?

user2338823
  • 501
  • 1
  • 3
  • 16
  • Check out this post [https://stackoverflow.com/a/65853007/15060591](https://stackoverflow.com/a/65853007/15060591) Maybe it can work – andres_995 Jan 22 '21 at 21:35

1 Answers1

15

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.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • 4
    Dear Ralf, Many thanks for your answer. It seems to be that mainfont is the main font in the PDF, monofont is the font of output of code chunks. I guess mathfont is the font used to output mathematical equations. Is this correct ? What does sansfont correspond to ? Can you please point me to the documentation where this terminology is listed? Also, I have posted a follow up query here. [link](https://stackoverflow.com/questions/50549961/making-the-output-of-table-in-the-same-font-as-that-of-the-default-font-for-the ) – user2338823 May 27 '18 at 07:27
  • 3
    @user2338823 You are correct. Sansfont is used for *sans serif* portions, e.g. via `\textsf{...}`. For the details you probably need the documentation for the LaTeX package fontspec (with TeXlive `texdoc fontspec`) plus a LaTeX introduction. – Ralf Stubner May 27 '18 at 08:38
  • I tried this example and got the following errors: ! Package fontspec Error: The font "LiberationSerif" cannot be found. ! name = LiberationSerif, rootname = LiberationSerif, pointsize = ! mktexmf: empty or non-existent rootfile! ! mktexmf: empty or non-existent rootfile! ! kpathsea: Running mktexmf LiberationSerif.mf ! The command name is C:\Users\Owner\AppData\Roaming\TinyTeX\bin\win32\mktexmf ! Cannot find LiberationSerif.mf. – ForEverNewbie Nov 06 '20 at 15:43
  • ! Cannot find LiberationSerif.mf. ! kpathsea: Running mktextfm LiberationSerif ! The command name is C:\Users\Owner\AppData\Roaming\TinyTeX\bin\win32\mktextfm ! kpathsea: Appending font creation commands to missfont.log. Error: LaTeX failed to compile test.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See test.log for more info. Execution halted – ForEverNewbie Nov 06 '20 at 15:43
  • @ForEverNewbie Do you have these fonts installed? Either as system fonts visible in other apps or within your TeX distribution? – Ralf Stubner Nov 07 '20 at 15:10
  • @RalfStubner, thanks!, I did not know the WPS Office software i use only has a handful of fonts installed, so i guess i need to install the fonts i need. For what i could find, I installed the package `extrafont`, then used `font_import()` followed by `loadfonts()`, and then tried to render a pdf document again with different fonts that appear in the output for `fonttable()` , but none worked. Any ideas of how i should proceed? – ForEverNewbie Nov 08 '20 at 23:18
  • @ForEverNewbie All that would be on the R level, but here you need the fonts on the TeX level. – Ralf Stubner Nov 15 '20 at 11:06
  • @RalfStubner, oh i see, so how could i do this? – ForEverNewbie Nov 15 '20 at 22:09
  • @ForEverNewbie That depends on the font and the TeX engine used. In this particular case, you would install the Liberation fonts (https://github.com/liberationfonts/) at the OS level and XeLaTeX will get them from their via the font name. However, these fonts where just an example because I had them installed on my system. With XeLaTeX you should be able to use any font that you have installed. You have to reference it via it's name. – Ralf Stubner Dec 03 '20 at 10:25