Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rmarkdown PDF custom font

I have an rmarkdown file for which I want to change the font to a custom font (not native to Windows). What I did so far:

  1. I got the .ttf file(s) for the font and installed them on Windows. I can actually access it now in Word, so that part worked.
  2. I edited the YAML part pf my rmarkdown file to reference the font. When I try this with fonts native to Windows (Arial, etc.) this works, so in general the code itself should not be wrong.

EDIT: I found that it also does not work for some of the pre-installed windows fonts, like Bahnschrift (which might be german-specific)

  1. I tried on 2 different PCs and both with lualatex and xelalatex. As described below I also tried extrafont for the plots and it doesnt recognize the font either.

Here YAML code: the rest of the rmarkdown is really just the unchanged template. Round Style is just an example.


---
title: "Untitled"
author: "Someone"
date: "7/8/2021"
output: 
  pdf_document:
    latex_engine: lualatex
mainfont: Round Style
---

#==EDIT====

or another version:

---
title: "Untitled"
author: "Someone"
date: "7/8/2021"
output: 
  pdf_document:
    latex_engine: lualatex
  \usepackage{fontspec}
---
\setmainfont{Round Style}

#==EDIT=END====

My suspicion is that R just doen't realize that the font is installed. When I try find the font via the extrafont package using first font_import() and then fonts() the list that it shows does not contain the new font.

Any help would be great. There are many results for custom fonts but I didn't see this particular issue tackled.

The main error I get is:

! fontspec error: "font-not-found"
! 
! The font "RoundStyle" cannot be found.
like image 573
NicolasH2 Avatar asked Apr 23 '26 21:04

NicolasH2


1 Answers

The line header-includes: is missing. Try with the following header:

---
title: "Untitled"
author: "Someone"
date: "7/8/2021"
output: 
  pdf_document:
    latex_engine: lualatex
header-includes:
- \usepackage{fontspec}
---

\setmainfont{Round Style}
like image 96
bttomio Avatar answered Apr 25 '26 11:04

bttomio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!