R Markdown: The Definitive Guide

To create a PDF document from R Markdown, you specify the pdf_document output format in the YAML metadata:

 --- title: "Habits" author: John Doe date: March 22, 2005 output: pdf_document ---

Within R Markdown documents that generate PDF output, you can use raw LaTeX, and even define LaTeX macros. See Pandoc’s documentation on the raw_tex extension for details.

Note that PDF output (including Beamer slides) requires an installation of LaTeX (see Chapter 1).

3.3.1 Table of contents

You can add a table of contents using the toc option and specify the depth of headers that it applies to using the toc_depth option. For example:

 --- title: "Habits" output: pdf_document: toc: true toc_depth: 2 ---

If the TOC depth is not explicitly specified, it defaults to 2 (meaning that all level 1 and 2 headers will be included in the TOC), while it defaults to 3 in html_document .

You can add section numbering to headers using the number_sections option:

 --- title: "Habits" output: pdf_document: toc: true number_sections: true ---

If you are familiar with LaTeX, number_sections: true means \section<> , and number_sections: false means \section*<> for sections in LaTeX (it also applies to other levels of “sections” such as \chapter<> , and \subsection<> ).

3.3.2 Figure options

There are a number of options that affect the output of figures within PDF documents: