93 PDF outputs
Written by Yena Joo and last updated on 7 October 2021.
93.1 Introduction
So far, we learned almost everything to write a proper paper, and the content should be exported into pdf document. In this lecture, we are going to learn how to produce a PDF in R Markdown.
Note that PDF output requires an installation of LaTeX.
Prerequisite skills include:
- You should know how to use RMarkdown.
Highlights:
- Use of YAML metadata
93.2 PDF outputs
You have to use R Notebook or R Markdown to produce a PDF. When you open the Notebook, you specify the pdf_document output format in the YAML metadata. To have a PDF format, the Rmd file should look something like this:
This is the original format of the R Markdown metadata, when you first create a new markdown document.
---
title: "Title"
author: "Author"
date: "Feb 7, 2021"
output: html_document
---
Since we want the pdf output, change the html_document
to pdf_document
.
---
title: "Title"
author: "Author"
date: "Feb 7, 2021"
output: pdf_document
---
Under output
:
- You can add a table of contents by writing toc: true
.
- You can also specify the depth of headers that it applies to using toc_depth
.
- You can add section numbering to headers using number_sections
.
---
title: "Title"
author: "Author"
date: "Feb 7, 2021"
output:
pdf_document:
toc: true
number_sections: true
---
You can enhance the default display of data frames using df_print
.
---
title: "Title"
author: "Author"
date: "Feb 7, 2021"
output:
pdf_document:
df_print: kable
---
Also, there are various LaTex packages that may not be built in the template, but you can still include them to the YAML.
Here is another example of including a package using header-includes:
.
---
title: "Title"
author: "Me"
header-includes:
- \usepackage{LaTex pacakge of your choice }
output:
pdf_document
---
You can find LaTeX packages on CTAN(The Comprehensive TeX Archive Network) subpage.
93.3 Exercises
93.3.1 Question 1
What is the correct command to create table of contents?
a. toc: false
b. toc_depth: true
c. toc: true
d. toc_depth: 2
93.3.2 Question 2
Modify the metadata so it changes into the pdf document with table of contents with depth of the header being 3, and add numbering to the header section.