Search code examples
rggplot2yamlknitrgoogle-fonts

Rmarkdown Will Not Knit Montserrat Font to PDF


In my report I am trying to change all labels or anything in my charts that has text to Montserrat font. I followed the instructions on this website for google fonts because I don't think Montserrat font is already in Rmarkdown. I could be wrong. Anyways I followed the instructions so that my text in my ggplot graph would be Montserrat. I downloaded the Montserrat text from google and placed it in Font Book on my Mac and here is the code that I used in Rmarkdown.

library(sysfonts)#for fonts

#Adding Font
font_add_google("Montserrat")

ggplot(chart2[-(nrow(chart2)),], aes(x = Garagen, y =Summe_Einnahmen_April))+
  geom_bar(stat = "identity")+
  labs(title = "Summe Einnahmen für Juli", xlab = "Garagen", y = "Summe Einnahmen")+
  theme(axis.text.x = element_text(angle = 90))+ 
  theme(legend.position = "none")+
  theme(legend.position = "none", text = element_text(family = "Montserrat"))

When I run this code I get no complaints and no error message but when I try to then knit the report to a pdf I get this error message:

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : invalid font type Calls: ... drawDetails -> drawDetails.text -> grid.Call.graphics In addition: There were 50 or more warnings (use warnings() to see the first 50) Execution halted

This error message is shown in the Rmarkdown tab so I go to the console and type warnings() and get nothing. If there is an easier way to get the Montserrat text to be the default text then I would definitely be interested or any other help is most welcome.

If it helps here is my YAML

---
title: "Template"
output:
  pdf_document: default
  html_document:
       df_print: paged
header-includes:
  - \usepackage{colortbl}
  - \usepackage{titling}
  - \pretitle{\begin{center}
    \includegraphics[width=2in,height=2in]{logo.jpg}\LARGE\\}
  - \posttitle{\end{center}} 
  
---

I also just realised that there is german letters that are in my text. I don't know if this is also important to know.


Solution

  • I researched online what latex package I needed for Montserrat. Just by simply adding the following package to my yaml section I was able to have all text in my Rmarkdown report as Montserrat.

    - \usepackage[defaultfam,tabular,lining]{montserrat}
    
    ---
    title: "Template"
    output:
      pdf_document: default
      html_document:
           df_print: paged
    header-includes:
      - \usepackage[defaultfam,tabular,lining]{montserrat}
      - \usepackage{colortbl}
      - \usepackage{titling}
      - \pretitle{\begin{center}
        \includegraphics[width=2in,height=2in]{logo.jpg}\LARGE\\}
      - \posttitle{\end{center}} 
      
    --- 
    

    Note I also did not need the following code anymore

    #Adding Font
    font_add_google("Montserrat")
    
     theme(legend.position = "none", text = element_text(family = "Montserrat"))
    
    

    However Im not sure if it is still necessary to have cairo and Quartz installed as it says in the website that I have in the question section.