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).
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<> ).
There are a number of options that affect the output of figures within PDF documents:
--- title: "Habits" output: pdf_document: fig_width: 7 fig_height: 6 fig_caption: true ---
You can enhance the default display of data frames via the df_print option. Valid values are presented in Table 3.3.
Option | Description |
---|---|
default | Call the print.data.frame generic method |
kable | Use the knitr::kable() function |
tibble | Use the tibble::print.tbl_df() function |
A custom function | Use the function to create the table. See 3.1.6.2 |
--- title: "Habits" output: pdf_document: df_print: kable ---
The highlight option specifies the syntax highlighting style. Its usage in pdf_document is the same as html_document (Section 3.1.4). For example:
--- title: "Habits" output: pdf_document: highlight: tango ---
Many aspects of the LaTeX template used to create PDF documents can be customized using top-level YAML metadata (note that these options do not appear underneath the output section, but rather appear at the top level along with title , author , and so on). For example:
--- title: "Crop Analysis Q3 2013" output: pdf_document fontsize: 11pt geometry: margin=1in ---
A few available metadata variables are displayed in Table 3.4 (consult the Pandoc manual for the full list):
Variable | Description |
---|---|
lang | Document language code |
fontsize | Font size (e.g., 10pt , 11pt , or 12pt ) |
documentclass | LaTeX document class (e.g., article ) |
classoption | Options for documentclass (e.g., oneside ) |
geometry | Options for geometry class (e.g., margin=1in ) |
mainfont, sansfont, monofont, mathfont | Document fonts (works only with xelatex and lualatex ) |
linkcolor, urlcolor, citecolor | Color for internal, external, and citation links |
By default, citations are processed through pandoc-citeproc , which works for all output formats. For PDF output, sometimes it is better to use LaTeX packages to process citations, such as natbib or biblatex . To use one of these packages, just set the option citation_package to be natbib or biblatex , e.g.
--- output: pdf_document: citation_package: natbib ---
By default, PDF documents are rendered using pdflatex . You can specify an alternate engine using the latex_engine option. Available engines are pdflatex , xelatex , and lualatex . For example:
--- title: "Habits" output: pdf_document: latex_engine: xelatex ---
The main reasons you may want to use xelatex or lualatex are: (1) They support Unicode better; (2) It is easier to make use of system fonts. See some posts on Stack Overflow for more detailed explanations, e.g., https://tex.stackexchange.com/q/3393/9128 and https://tex.stackexchange.com/q/36/9128.
R Markdown documents are converted to PDF by first converting to a TeX file and then calling the LaTeX engine to convert to PDF. By default, this TeX file is removed, however if you want to keep it (e.g., for an article submission), you can specify the keep_tex option. For example:
--- title: "Habits" output: pdf_document: keep_tex: true ---
You can do more advanced customization of PDF output by including additional LaTeX directives and/or content or by replacing the core Pandoc template entirely. To include content in the document header or before/after the document body, you use the includes option as follows:
--- title: "Habits" output: pdf_document: includes: in_header: preamble.tex before_body: doc-prefix.tex after_body: doc-suffix.tex ---
You can also replace the underlying Pandoc template using the template option:
--- title: "Habits" output: pdf_document: template: quarterly-report.tex ---
Consult the documentation on Pandoc templates for additional details on templates. You can also study the default LaTeX template as an example.
Similar to HTML documents, you can enable or disable certain Markdown extensions for generating PDF documents. See Section 3.1.10.4 for details. You can also pass more custom Pandoc arguments through the pandoc_args option (Section 3.1.10.5), and define shared options in _output.yml (Section 3.1.11).