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
 keep_tex: yes
 latex_engine: lualatex
 - \usepackage{wallpaper}
 - \usepackage{fontspec}
 - \pagenumbering{gobble}
 - \setmainfont[Path = ~/Desktop/Typography/ , UprightFont =  font1 , ItalicFont = font2 , BoldFont =  font3 , Extension = .otf]{common_filenames}

  • 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~ – David Carlisle Sep 17 '18 at 15:31
  • Adding \string~ fixed it. Thanks! – Rishi Kowalski Sep 17 '18 at 15:53

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.