Custom Fonts in RMarkdown PDF

I'm trying to create a new RMarkdown report that conforms to my organization's brand standards. As such, I've had to try and learn how to change the font within RMarkdown. I've had luck with the body text using the LaTex package fontspec. However, the Title, Author, & Date aren't changing.

Does anyone have a solution for this? Or at least an idea?

Here is an example:


title: "Font Test"
author: "dlsweet"
date: "October 18, 2017"
output:
pdf_document:
latex_engine: xelatex
header-includes:

  • \usepackage{fontspec}

\setmainfont{Calibri Light}

knitr::opts_chunk$set(echo = TRUE)

R Markdown

This is an R Markdown document. Markdown is a simple formatting >syntax for authoring HTML, PDF, and MS Word documents. For >more details on using R Markdown see
http://rmarkdown.rstudio.com.
<
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)

Including Plots

You can also embed plots, for example:

plot(pressure)

Note that the echo = FALSE parameter was added to the code >chunk to prevent printing of the R code that generated the plot.

1 Like

From looking at the default Rmd latex template (got there by way of LaTeX options from the R Markdown docs, it looks like title, author, etc. are explicitly defined separately (sorry, I'm sure my terminology is wrong— I'm not much of a latex whiz), so is it possible you'd have to override their fonts separately as well?

1 Like

Thanks @mara, I didn't have to quite override them separately but you gave me the idea to try moving my LaTex command into my YAML Header.

By changing my header to this, it formatted all of the fonts for the entire document:

---
title: "Font Test"
author: "dlsweet"
date: "October 18, 2017"
output: 
  pdf_document:
    latex_engine: xelatex
header-includes:
  - \usepackage{fontspec}
  - \setmainfont{Museo}
---
5 Likes

@mara, Do you know if there is a way to see the LaTex code behind the RMarkdown document that I'm currently working with. I accidentally broke my document :confused: when I was trying to get the header to include my logo and now I can either choose to use a logo or the right font because of a missing $

Using the settings button next to the Knit button (image), go to "Output Options...". Under the "Advanced" tab, check "Keep text source file used to produce PDF". Then, next time you knit the document, it will leave a .tex file (starting with the same name as your .Rmd file). That should have the raw LaTex.

5 Likes

Thanks @nick! That got me the LaTex file, now I just need to figure out how to fix my error!

I figured it out! I had an _ in my logo filename and xelatex was reading it in as a mathematical symbol. After changing the filename to not use an _ it worked perfectly! :grinning:

2 Likes

Sorry I'm just seeing this now— lucky for us all, @nick's a champ!

1 Like

A post was split to a new topic: Custom fonts with shinyapps.io