Search code examples
rrstudior-markdownbookdown

Adding table of content with bookdown::html_document2 in Rstudio


I'm building a bookdown::html_document2 with Rstudio and I can't seem to manage to add a table of content using Rstudio.

I've found this question (Bookdown: TOC with html_document2) that is related however I knit my document using the Knit button of Rstudio while they are using a command line. In my case, it produces the error:

Error in yaml::yaml.load(string, ...) : 
  Scanner error: mapping values are not allowed in this context at line 4, column 33
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load_utf8 -> <Anonymous>
Execution halted

To reproduce the error, try:

---
title: "main title"
author: "Me"
date: "September 26, 2018"
output: bookdown::html_document2:
  toc=TRUE
---


# title 1

# tt

# rr 

## rrr

in Rstudio and hit Knit

Any idea how to make it work? Thanks


Solution

  • Move the bookdown::html_document2: into the next row, and add toc=TRUE underneath it. Be carefull for indendation. Check out the full YAML syntax.

    ---
    title: "main title"
    author: "Me"
    date: "September 26, 2018"
    output:
      bookdown::html_document2:
        toc: true
    ---
    
    
    # title 1
    
    # tt
    
    # rr 
    
    ## rrr