Open In App

How to Add Table of Contents in Rmarkdown?

Last Updated : 11 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Rmarkdown is a powerful tool for creating dynamic and reproducible reports, presentations, and documents. One useful feature of Rmarkdown is the ability to include a table of contents, which can make it easier for readers to navigate long documents. By adding a table of contents to your Rmarkdown document, you can help readers quickly find the information they need and can also improve the overall user experience. Here, we will explore how to add a table of contents to an RMarkdown document using simple, easy steps. If you are a beginner or an experienced Rmarkdown user, you will get the full idea of how you can create an effective table of contents in your RMarkdown documents. To use the Rmarkdown document tool, you need to install the Rmarkdown package.

Adding a Table of Contents in Rmarkdown               

There are four concepts related to adding a table of contents in Rmarkdown. I am pointing out a few important ones:

  • Markdown Syntax: Rmarkdown is based on Markdown syntax, which is a lightweight markup language that allows you to format text using a simple syntax. To create headings that will appear in the table of contents, you’ll need to use Markdown syntax to format your headings.
  • Pandoc: Rmarkdown uses Pandoc as its document conversion tool. This is a versatile document conversion tool that can convert documents between various formats, like HTML, PDF, and Microsoft Word.
  • Knitr: Knitr is an R package that provides a flexible framework for creating dynamic reports in R Programming Language. It is used to knit R code and output into a variety of output formats, including HTML, PDF, and Microsoft Word. In Rmarkdown, Knitr is used to generate the table of contents using the table_of_contents() function.
  • Output Formats: Rmarkdown supports a variety of output formats, including HTML, PDF, and Microsoft Word. The process for adding a table of contents may vary slightly depending on the output format you choose. For example, if you’re creating a PDF output, you’ll need to use a different syntax to enable the table of contents

Step Needed to Add the Table of Contents in Rmarkdown 

To add a table of contents in Rmarkdown, you can follow these steps:

Add YAML header

Add a YAML header to the beginning of your R Markdown document by placing three dashes at the top of the file followed by the YAML code block. In the YAML header, you can specify various settings for your document, including the output format, title, author, and table of contents. For example, the following YAML header specifies a PDF output format and enables the table of contents:

R




---
title: "My R Markdown Document"
output:
 pdf_document:
   toc: true
---


Use heading tags

Use heading tags to structure your R Markdown document. You can use the ‘#’ symbol followed by a space to create headings at different levels.

R




# Introduction
This is the introduction section.
 
## Background
This is the background section.
 
# Methods
This is the methods section.
 
## Data Collection
This is the data collection section.


Knit your document

After you have added the YAML header and headings to your R Markdown document, you can knit your document to create the output file. To do this, click the “Knit” button in RStudio or use the render() function in R.

View the table of contents

When you open the output file, you should see a table of contents that includes links to each heading in your document. You can click on the links to navigate to different sections of your document.

How to Add Table of Contents in Rmarkdown?

 

By following these steps, you can easily add a table of contents to your R Markdown document.  

R




---
title: "Example Report with Table of Contents"
author: "Jane Doe"
output:
 pdf_document:
   toc: true
   number_sections: true
---
 
# Introduction
This is an example Rmarkdown document that demonstrates how to add a table of contents to a PDF output.
 
## Section 1
This is the first section of the document.
 
### Subsection 1.1
This is the subsection of Section 1.
 
### Subsection 1.2
This is another subsection of Section 1.
 
## Section 2
This is the second section of the document.
 
### Subsection 2.1
This is the subsection of Section 2.
 
### Subsection 2.2
This is another subsection of Section 2.
 
# Conclusion


Output:

How to Add Table of Contents in Rmarkdown?

 

That’s it! This is just a simple example, but it should give you an idea of how to add a table of contents to a Rmarkdown document that is rendered to a PDF output. Example of an RMarkdown document that includes a table of contents:

R




---
title: "Example RMarkdown Document"
output:
 word_document:
   toc: true
---
 
# Introduction
This is an example RMarkdown document that includes a table of contents.
 
## Background
Example Background area
 
## Objective
The objective of this document.
 
# Methods
The methods used in this document.
 
# Results
Document results
 
# Conclusion
RMarkdown document is a simple and useful way to help readers navigate long documents.


Output:

How to Add Table of Contents in Rmarkdown?

 

If this document is rendered in a Word file using rmarkdown::render(), it will include a table of contents at the beginning of the document. The table of contents will include links to each section based on the document that has a heading (indicated by the # syntax). An example RMarkdown document that includes a table of contents 

R




---
title: "Example Table of Contents"
author: "Your Name"
date: "2023-03-26"
output:
 html_document:
   toc: true
   toc_depth: 3
   number_sections: true
   theme: united
   highlight: tango
   css: my.css
---
 
# Introduction
 
This is the introduction section.
 
## Purpose
 
The purpose of this document is to demonstrate how to create a table of contents in RMarkdown.
 
## Background
 
Here is some background information on the topic.
 
# Methods
 
In this section, we will outline the methods used in this document.
 
## Data
 
We used a dataset from the `ggplot2` package.
 
## Analysis
 
We conducted our analysis using the `dplyr` package.
 
# Results
Here are the results of our analysis.
 
## Descriptive Statistics
We calculated some descriptive statistics on the dataset.
 
## Visualizations
We created some visualizations to display the results.
 
# Discussion
In this section, we will discuss the implications of our results.
 
## Interpretation
We will interpret the results in the context of the research question.
 
## Limitations
We will discuss some limitations of our analysis.
 
# Conclusion
This is the conclusion section.
 
Note that the my.css file should be located in the same folder as the RMarkdown document. Here is an example my.css file:
 
/* my.css */
 
body {
 font-family: Arial, sans-serif;
 font-size: 14px;
 line-height: 1.5;
 color: #333;
}
 
h1, h2, h3, h4, h5, h6 {
 font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
 font-weight: bold;
 line-height: 1.2;
 margin-top: 1.5em;
 margin-bottom: 0.5em;
}
 
h1 {
 font-size: 36px;
}
 
h2 {
 font-size: 30px;
}
 
h3 {
 font-size: 24px;
}
 
h4 {
 font-size: 18px;
}
 
h5 {
 font-size: 16px;
}
 
h6 {
 font-size: 14px;
}
 
a {
 color: #337ab7;
 text-decoration: none;
}
 
a:hover {
 color: #23527c;
 text-decoration: underline;
}
 
.table {
 width: 100%;
 margin-bottom: 1em;
 border-collapse: collapse;
}
 
.table th,
.table td {
 padding: 0.75em;
 border: 1px solid #ccc;
}
 
.table th {
 font-weight: bold;
 background-color: #f5f5f5;
}
 
.table-striped tbody tr:nth-of-type(odd) {
 background-color: #f9f9f9;
}
 
code {
 font-family: Menlo, Monaco, Consolas,
  "Courier New", monospace;
 font-size: 12px;
 color: #c7254e;
 background-color: #f9f2f4;
 padding: 0.25em;
}


Output: 

How to Add Table of Contents in Rmarkdown?

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads