Search code examples
rknitrrnotebook

R Notebook: TOC is not kept on the left when knitting to html


I have the following R notebook:

---
title: "test"
author: "by me"
date: 'last updated: Jan 23, 2022'
output:
  html_notebook:
    toc: yes
    toc_float: yes
    number_sections: yes
    highlight: haddock
---

# First headline

## Second headline

```{r}
1:5
```

When I click on "preview", it creates an html document that keeps the TOC on the left side (as expected). When I click on "knit to html", it puts the TOC in the main body of the document. It's still a TOC, but at the wrong place. It also doesn't collapse the individual headlines anymore and instead shows the full ToC.

How can I keep the ToC on the left and keep the headlines collapsed per default?


Solution

  • When you knit to HTML, in reality knitr is using the definitions for html_document, so if you add the appropriate YAML header, it should work.

    ---
    title: "test"
    author: "by me"
    date: 'last updated: Jan 23, 2022'
    output:
      html_document:
        toc: yes
        toc_float: yes
        df_print: paged
        number_sections: yes
        highlight: haddock
      html_notebook:
        toc: yes
        toc_float: yes
        number_sections: yes
        highlight: haddock
    ---
    
    # First headline
    
    ## Second headline
    
    ```{r}
    1:5
    ```
    

    Example of "knit to HTMLE"