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.

93.3.3 Question 3

True or False: You can specify the depth of headers of the document using toc_depth.
a. True
b. False

93.3.4 Question 4

93.3.5 Question 5

93.3.6 Question 6

93.3.7 Question 7

93.3.8 Question 8

93.3.9 Question 9

93.3.10 Question 10

93.4 Next Steps

Using all the functions we learned so far, try to write a paper of your interest, cite the sources, and knit to PDF to create a perfect professional paper.

93.5 Exercises

93.5.1 Question 1

93.5.2 Question 2

93.5.3 Question 3

93.5.4 Question 4

93.5.5 Question 5

93.5.6 Question 6

93.5.7 Question 7

93.5.8 Question 8

93.5.9 Question 9

93.5.10 Question 10