To create a PDF document from R Markdown you specify the pdf_document
output format in the front-matter of your document:
---
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 the documentation on Raw TeX for details.
Note that PDF output (including Beamer slides) requires a full installation of TeX.
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 table of contents depth isn’t explicitly specified then it defaults to 3 (meaning that all level 1, 2, and 3 headers will be included in the table of contents).
You can add section numbering to headers using the number_sections
option:
---
title: "Habits"
output:
pdf_document:
toc: true
number_sections: true
---
There are a number of options that affect the output of figures within PDF documents:
fig_width
and fig_height
can be used to control the default figure width and height (6 x 4.5 is used by default)
fig_crop
controls whether the the pdfcrop utility (if available) is automatically applied to pdf figures (this is true by default). If your graphics device is postscript, you are recommended to disable this feature (see more info here).
fig_caption
controls whether figures are rendered with captions (this is false by default).
dev
controls the graphics device used to render figures (defaults to pdf)
For example:
---
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 include:
Option | Description |
---|---|
default | Call the print.data.frame generic method |
kable | Use the knitr::kable function. |
tibble | Use the tibble::print.tbl_df function. |
For example:
---
title: "Habits"
output:
pdf_document:
df_print: kable
---
The highlight
option specifies the syntax highlighting style. Supported styles include “default”, “tango”, “pygments”, “kate”, “monochrome”, “espresso”, “zenburn”, and “haddock” (specify null to prevent syntax highlighting):
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, etc.). For example:
---
title: "Crop Analysis Q3 2013"
output: pdf_document
fontsize: 11pt
geometry: margin=1in
---
Available metadata variables include:
Variable | Description |
---|---|
lang | Document language code |
fontsize | Font size (e.g. 10pt, 11pt, 12pt) |
documentclass | LaTeX document class (e.g. article) |
classoption | Option for documentclass (e.g. oneside); may be repeated |
geometry | Options for geometry class (e.g. margin=1in); may be repeated |
mainfont, sansfont, monofont, mathfont | Document fonts (works only with xelatex and lualatex, see the latex_engine option) |
linkcolor, urlcolor, citecolor | Color for internal, external, and citation links (red, green, magenta, cyan, blue, black) |
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
---
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: header.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.
By default R Markdown is defined as all pandoc markdown extensions with the following tweaks for backward compatibility with the markdown package:
+autolink_bare_uris
+ascii_identifier
+tex_math_single_backslash
You can enable or disable markdown extensions using the md_extensions
option (you preface an option with -
to disable and +
to enable it). For example:
---
title: "Habits"
output:
html_document:
md_extensions: -autolink_bare_uris+hard_line_breaks
---
The above would disable the autolink_bare_uris
extension and enable the hard_line_breaks
extension.
For more on available markdown extensions see the pandoc markdown specification.
If there are pandoc features you want to use that lack equivilants in the YAML options described above you can still use them by passing custom pandoc_args
. For example:
---
title: "Habits"
output:
pdf_document:
pandoc_args: [
"--no-tex-ligatures"
]
---
Documentation on all available pandoc arguments can be found in the pandoc user guide.