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

• it would have been more helpful to show the exact error from latex (and actual input not a fragment with names like font1 which make it impossible to debug, but does ~ work there? I'd have expected you to need \string~ Sep 17 '18 at 15:31
• Adding \string~ fixed it. Thanks! Sep 17 '18 at 15:53
~ is an active character denoting a non breaking space, you need \string~ to get a literal ~`