[Tex/LaTex] Loading Custom Fonts into RMarkdown

fontsfontspecknitrrrstudio

I've been testing loading my own fonts into a personal rmarkdown document using the fontspec latex package. Because I'll eventually need to knit this on a server, using the font library on my mac is not an option and I need to refer to a specific filepath.

I've been at this for a while and while the fonts seem to load, the latex compilation will throw an error where the \begin{document} latex tag will not be recognized in the .tex document generated by rmarkdown even though the the tag will be in the .tex file. Does anyone have a solution for this or a workaround for getting custom fonts in from a local filepath?

I'm using RStudio Version 1.1.453 and Latex Version 3.14159265-2.6-1.40.19.


To load the fonts I've used the following convention in my YAML header specified by this post:

---

title: "Test Report" 
author: "me"
geometry: left = .5cm, right = .5cm, top = 3cm, bottom = 3cm
output:`
pdf_document:
 keep_tex: yes
 latex_engine: lualatex
header-includes:
 - \usepackage{wallpaper}
 - \usepackage{fontspec}
 - \pagenumbering{gobble}
 - \setmainfont[Path = ~/Desktop/Typography/ , UprightFont =  font1 , ItalicFont = font2 , BoldFont =  font3 , Extension = .otf]{common_filenames}

---

Best Answer

~ is an active character denoting a non breaking space, you need \string~ to get a literal ~