In RMarkdown, can a floating TOC be placed on the right side of page

Viewed 2922

When I add a floating Table of Contents to my R-Markdown document, it always is on the left side of the page (with the content to the right), like so:

---
title: "some title"
author: "me"
date: "3/2/2020"
output: 
  html_document:
    toc: TRUE
    toc_float: TRUE
---

Default placing of floating TOC


However, I'd like to move the floating TOC to the right side of the page. How can I accomplish this? The image below is what I'd like:


enter image description here

2 Answers

Insert the CSS chunk below (after YAML) and try increasing/decreasing the values of px (right and margin-left) for fine-tuning:

---
title: "some title"
author: "me"
date: "3/2/2020"
output: 
  html_document:
    toc: TRUE
    toc_float: TRUE
---

```{css toc-content, echo = FALSE}
#TOC {
  right: 270px;
  margin: 20px 0px 25px 0px;
}

.main-container {
    margin-left: 200px;
}
```

The R Hmisc package hidingTOC function does this, allows for changing some of the characteristics of the display, and provides buttons labeled 1 2 3 that the reader can click to show the table of contents to level 1, level 1-2, level 1-3. No special yaml is needed; just have require(Hmisc) in your setup chunk and have the inline text sometime after that chunk: r hidingTOC(buttonLabel="Outline") or just use the default. You can also just say r Hmisc::hidingTOC() as the inline R code. Don't need to put these commands in a chunk; they render HTML which is automatically included in the document.

Related