rr-markdownknitrbookdown

rmarkdown toc missing when rendering bookdown::html_document2


When I knit the following test.Rmd file:

---
title: "test"
output:
  bookdown::html_document2:
    toc: true
    toc_float: TRUE
    code_folding: show
---


# heading 1

# heading 2

It produces a toc:

enter image description here

But when I render it using:

rmarkdown::render('test.Rmd', output_format = "html_document2")

The toc is missing:

enter image description here

Does anyone know how to include toc when rendering with bookdown::html_document2?

thanks


Solution

  • The output format name should be exactly what you specified in the YAML metadata, i.e.,

    rmarkdown::render('test.Rmd', output_format = 'bookdown::html_document2')
    

    instead of

    rmarkdown::render('test.Rmd', output_format = 'html_document2')
    

    Alternatively (not recommended), you can call the output format function with options:

    rmarkdown::render(
      'test.Rmd', 
      output_format = bookdown::html_document2( 
        list(toc = TRUE, toc_float = TRUE
      )
    )
    

    or

    rmarkdown::render(
      'test.Rmd', 
      output_format = 'bookdown::html_document2',
      output_options = list(toc = TRUE, toc_float = TRUE)
    )
    

    These alternative methods are unnecessarily more complicated, and also make the document less reproducible (not making use of options specified inside the document), so you are recommended to just use output_format = 'bookdown::html_document2' and specify output format options in the Rmd document.