1

When making an RMarkdown containing tabs, some extra (blank) items appear in the table of contents.

Example

This generates the html doc below

---
output: 
  html_document:
    toc: true 
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# First Tabs {.tabset .tabset-fade .tabset-pills}

Text before tabs

## First tab

Content in first tab

## Second tab

Content in second tab

#


# here is another section
Some further content. 

enter image description here

Everything is as expected, except there's a blank line in the TOC.

What I've tried

I tried replacing the # that ends the tabbed content with </div> as described here. This causes the TOC to populate correctly, but (strangely) causes the content after the tabs to left-align (no idea why)

For ease of reproducibility, here's the code and a screengrab of the resulting HTML

---
output: 
  html_document:
    toc: true 
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# First Tabs {.tabset .tabset-fade .tabset-pills}

Text before tabs

## First tab

Content in first tab

## Second tab

Content in second tab

</div>


# here is another section
Some further content. 

enter image description here

stevec
  • 41,291
  • 27
  • 223
  • 311
  • Just remove the single `#` – J_F May 04 '20 at 07:18
  • @J_F thanks, I’ll try now. Why do you have it in the linked answer though? (tbh, I don’t quite understand how knitr interprets it, I have followed your answer (including closing #) and found it works perfectly with the toc being the only exception – stevec May 04 '20 at 07:36
  • @J_F I tested on a large file, with many tabs, and simiply removing the closing hashes works! I am quite confused though, as [not using closing hashes caused problems in the past](https://stackoverflow.com/questions/61207158/how-to-facet-across-tabs-in-ggplot2-rmarkdown#comment108639268_61208750). I need to read up on the way knitr interprets these structures and converts them to html – stevec May 04 '20 at 07:51

1 Answers1

3

As written in the comment: Just remove the single #. There is also a workaorund, if you have have the following problem

  • Use TOC
  • Use Tabs
  • End tabbed region with further text under the tabbed region

Problem: Normaly use ## to end tabbed region, but this would be another header in the TOC

Solution: ## {.unlisted .unnumbered} will remove the header from TOC.

Example:

---
output: 
  html_document:
    toc: true 
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## title {.tabset .tabset-fade}
content above tabbed region.

### tab 1 

tab content 1

### tab 2

tab content 2

## {.unlisted .unnumbered}

content below tabbed region

enter image description here

J_F
  • 9,956
  • 2
  • 31
  • 55
  • 1
    Great knowledge! A couple of questions: is there any reason not to always use `## .unlisted .unnumbered` to close a tabbed section, as there appears to be no downside? Also, how did you find out about `.unlisted` and `.unnumbered` classes? Are they defined somewhere in kntir css, or pandoc css? – stevec May 04 '20 at 20:11
  • 3
    `.unnumbered` is in the Pandoc manual; `.unlisted` is a relatively new feature, and does not seem to be documented yet. We have mentioned them in these sections in this book: https://bookdown.org/yihui/rmarkdown-cookbook/unnumbered-sections.html and https://bookdown.org/yihui/rmarkdown-cookbook/toc-unlisted.html – Yihui Xie May 04 '20 at 20:56