Changing font in PDF produced by rmarkdown
Asked Answered
A

4

52

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

Anhydrite answered 26/6, 2014 at 1:48 Comment(4)
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.Racquelracquet
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"Anhydrite
see my answer, you were right, i just didn't know how to implement the syntax. Thanks!Anhydrite
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.Racquelracquet
A
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.

Anhydrite answered 26/6, 2014 at 14:25 Comment(4)
There is also classoption: 12pt now.Kiele
The middle option (2nd chunk here) did not work for me.Mckay
@Kiele could you provide an example of using classoption: 12pt? I have entered it in my YAML and it seems to have no effect.Esbenshade
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 \\ ") .Mckay
F
13

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.

Frecklefaced answered 22/1, 2021 at 21:29 Comment(2)
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?Curtsy
@Curtsy I have added the comments in \usepackage, I hope they help you, regardsFrecklefaced
F
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

Fimbriate answered 26/6, 2014 at 5:16 Comment(1)
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!Anhydrite
S
0

In case that the font change is only needed for a small text you need to use the statements \begingroup and \endgroup. All the characters output between these will have the determined text styling. Example with cat:

 cat("\\begingroup\\fontsize{8}{8}\\selectfont{YOUR TEXT}\\endgroup") 

Example with in plain rmarkdown:

\begingroup
\fontsize{8}{8}\selectfont
YOUR TEXT
\endgroup 
Sarmiento answered 4/7 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.