51

I am producing reports using rmarkdown. When knitting a PDF

---
title: "Untitled"
output: pdf_document
---

I would like to specify the font to be used in creating the PDF. The official documentation (see section "LaTeX Options) says I can do this. enter image description here However, I've never used LaTeX and fail to understand how such selection can be made in YAML options at the top of the .Rmd document used by rmarkdown package.

Question: How do I change the font in the PDF produced by rmarkdown?

sessionInfo() R version 3.1.0 (2014-04-10) Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_1.0.0 RODBC_1.3-10  knitr_1.6     dplyr_0.2

I've never used LaTeX and don't want to get into it at this very mom

andrey
  • 2,029
  • 2
  • 18
  • 23
  • 2
    What is it that you want to change specifically, the size or the font family? The documentation you linked to provides an example of using `geometry` in the YAML header (it sets it to `margin=1in`), so might lead you to try the other arguments in the same manner. I'm guessing you have MiKTeX installed which *does* include `xelatex`, so you allegedly can set `mainfont` in a similar fashion. I don't have it accessible at the moment, but try: `output:\n pdf_document:\n mainfont: Times New Roman` (\n implies CR and indentation) and see what that does. You probably need `latex_engine: xelatex` too. – r2evans Jun 26 '14 at 05:05
  • Yes, I have MiKTeX and use xelatex. Unfortunately, the cod you suggested does'nt work - it produces the error, probably because of wrong syntax. SImilar fashion does not work: yaml doesn't take commands such as mainfont: phv, for example, giving error "Unused arguments" – andrey Jun 26 '14 at 14:08
  • see my answer, you were right, i just didn't know how to implement the syntax. Thanks! – andrey Jun 26 '14 at 14:27
  • good to hear. Because I wasn't in a position to test the code, I didn't want to post an answer; so because I was using a comment, I couldn't show indentation. I probably could have stressed the need for proper indentation more, glad you figured it out. – r2evans Jun 26 '14 at 15:32

3 Answers3

48

The indentation in YAML options is meaningful. As the instructions point out "note that these options do not appear underneath the output section but rather appear at the top level along with title, author, etc.)". So,

---
output:
  pdf_document:
    latex_engine: xelatex
    sansfont: Calibri Light
---

will produce an unused argument error, while

---
output:
  pdf_document:
    latex_engine: xelatex
sansfont: Calibri Light
---

will do the job. In addition, LaTeX commands inserted after YAML seem to override it: so

---
output:
  pdf_document:
    latex_engine: xelatex
    sansfont: Calibri Light
---
\fontsize{12}{22}
\fontseries{b}
\selectfont

produces the PDF with default font, not Calibri, however, the font option is passed fine.

andrey
  • 2,029
  • 2
  • 18
  • 23
  • 4
    There is also `classoption: 12pt` now. – hpesoj626 Aug 24 '18 at 02:24
  • 3
    The middle option (2nd chunk here) did not work for me. – Nova Sep 23 '18 at 20:26
  • @hpesoj626 could you provide an example of using classoption: 12pt? I have entered it in my YAML and it seems to have no effect. – Joe Jan 08 '19 at 20:04
  • If you need to change the font size *within* a code chunk, ensure the chunk options are set to `results = "asis"` and you can use `cat("\\fontsize{15}{15} \\ \\selectfont \\ ") `. – Nova Jul 13 '21 at 12:52
11

Just a quick example. add these lines to your RMD main text area and see the effects.

\fontfamily{cmr}
\fontsize{12}{22}
\fontseries{b}
\selectfont

Hope this may help

tky
  • 687
  • 1
  • 5
  • 9
  • 2
    This partially works. It's sensitive to the font size and line size, but \fontfamily , for example \fontfamily{phv} does not affect the PDF. Certainly a good start! Thanks! – andrey Jun 26 '14 at 14:04
11

I use pdfLatex, so almost nothing worked until I use "header-includes", like this:

---
title: "TITLE"
author: "ANDRES"
output: pdf_document
fontsize: 11pt
header-includes:
  % Allows me to use and print accented characters (document is in Spanish)
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}

  % Set up the document in Spanish
  \usepackage[spanish]{babel}

  % It allows me to place the tables or images where I want, whether there is space or not
  \usepackage{float}

  % It has several commands to have more control over the tables (at least I use it for this)
  \usepackage{array}

  % For custom headers and footers
  \usepackage{fancyhdr}

  % This line allows me to manipulate images within latex
  \usepackage{graphicx}

  % This package has several uses, for this document I used it to manage hyperlinks and introduce metadata to the document
  \usepackage{hyperref}

  % These lines define the font that I want to use in the document
  \usepackage{helvet}
  \renewcommand{\familydefault}{\sfdefault}
---

With this change the generated pdf would look like this:

enter image description here

The other solutions only achieved this:

enter image description here

Just a part of the text in the font I needed.

andres_995
  • 141
  • 1
  • 5
  • Thank you @andres. This dramatically improves the PDF output format over the defaults. Could you add comments indicating what each of your `\usepackage` lines does? – Stan May 11 '21 at 17:24
  • 1
    @Stan I have added the comments in \usepackage, I hope they help you, regards – andres_995 May 22 '21 at 20:52