Search code examples
r-markdownbookdownroboto

How to have roboto font in bookdown::pdf_document2?


Anyone suggestions on how to get roboto font in bookdown::pdf_document2? The code below works fine, when I change the font to Roboto it does not, see also picture. The Roboto and the Comic Sans MS font are in my c:\Windows\Fonts, I use Miktex for pdf-ing. Thanks!

---
header-includes:
- \usepackage{fontspec}
- \setmainfont{Comic Sans MS}
output:
  bookdown::pdf_document2:
    toc: false
    latex_engine: xelatex
---

# Title
This is some text.  
This is some more text.

enter image description here


Solution

  • You can do \usepackage{roboto}. See README of the package for further detailed usage. If you knit the document without roboto package, programmes for knitting documents using LaTeX (e.g. R's tinytex or even MikTeX) will automatically install the missing package(s). See the relevant page of R Markdown Cookbook.

    To make specific texts san serif

    ---
    header-includes:
    - \usepackage{roboto}
    # - \usepackage[sfdefault]{roboto} # To make every texts san serif
    output:
      bookdown::pdf_document2:
        toc: false
        latex_engine: xelatex
    ---
    
    # Title
    
    This is some text.  
    This is some more text.
    
    `\textsf{This is a san serif text.}`{=latex}
    

    enter image description here

    To make every texts san serif

    ---
    header-includes:
    #- \usepackage{roboto}
    - \usepackage[sfdefault]{roboto} # To make every texts san serif
    output:
      bookdown::pdf_document2:
        toc: false
        latex_engine: xelatex
    ---
    
    # Title
    
    This is some text.  
    This is some more text.
    
    `\textsf{This is a san serif text.}`{=latex}
    

    enter image description here