Rmarkdown: Indentation of TOC items in HTML output

PoGibas

I want to indent TOC according to header level.

My example document looks like this:

# Tutorial
## Start a new project
### Project structure
### Analysis code

I'm compiling Rmd document with:

rmarkdown::render("foo.Rmd", 
                  output_options = HTMLlook, 
                  output_file = "foo.html")

HTMLlook <- list(toc = TRUE,
                 toc_depth = 5,
                 toc_float = list(collapsed = FALSE, 
                                  smooth_scroll = TRUE))

This produces document with TOC

enter image description here

However, I want indented TOC (indentation equivalent to header level). Wanted result should look like this: enter image description here

Is it possible to set this option in render or maybe pass css parameters to it?

Martin Schmelzer

I am not aware of a built-in solution. But here is a little tweak:

<script>
    $(document).ready(function() {
      $items = $('div#TOC li');
      $items.each(function(idx) {
        num_ul = $(this).parentsUntil('#TOC').length;
        $(this).css({'text-indent': num_ul * 10, 'padding-left': 0});
      });

    });
</script>

The depth of your headers is actually mapped inside the TOC. For each level you go down, a new ul element is created. This is what we are making use of here. In detail:

When the document has finished loading ($(document).ready(....):

  1. Select all list items inside the element with id TOC
  2. For each list item count the number of parent elements until you reach the element with id TOC. This is the number of ul elements.
  3. Change the style for the current list item according to the number of parents.

You can tweak the spacing by playing around with the two parameters for text-indent and padding-left.


MRE:

---
title: "Habits"
author: Martin Schmelzer
date: September 14, 2017
output: 
  html_document:
    toc: true
    toc_depth: 5
    toc_float: 
      collapsed: false
      smooth_scroll: true
---

<script>
$(document).ready(function() {
  $items = $('div#TOC li');
  $items.each(function(idx) {
    num_ul = $(this).parentsUntil('#TOC').length;
    $(this).css({'text-indent': num_ul * 10, 'padding-left': 0});
  });

});
</script>

# In the morning 

## Waking up 

### Getting up

#### Take a shower

##### Make coffee

# In the evening

## Make dinner

This is the result:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

RMarkdown with knitr to HTML: How to hide bullets in TOC (table of contents)?

Add toc entries for html generated in r chunks in rmarkdown

RMarkdown: Floating TOC AND TOC at the start of the report

Centering a Xtable in Rmarkdown html output

image logo over TOC in Rmarkdown

Hyperlink a pdf on local drive to rmarkdown html output

Rmarkdown html matrix output width too narrow

Use internal links in RMarkdown HTML output

Suppress alt text for GIFs in rmarkdown HTML output

dfSummary - formatting factor levels in html output in Rmarkdown?

How to place TOC anywhere in word rendered Rmarkdown

Rmarkdown: remove some headers from TOC

Shell Script output indentation

Output indentation in C

Changing the font size of figure captions in RMarkdown HTML output

Change color of error messages in RMarkdown code output (HTML, PDF)

How to add code folding to output chunks in rmarkdown html documents

How to render HTML from RMarkdown without javascript in output

How to show significance stars in R Markdown (rmarkdown) html output notes?

Suppress running messages from RStan in Rmarkdown HTML output

How to remove white space at the end of an Rmarkdown HTML output

Avoid text-wrapping of code in Rmarkdown html output

Using nest and tidy to create formatted html regression output in RMarkdown

Inserting custom-spaced page breaks in Rmarkdown HTML output

Multiline input prompt indentation interfering with output indentation

Randomize items in rmarkdown transparently

Rmarkdown : is it possible to display the TOC of the current tab only in a tabset

Why does toc_depth doesn't work with rmarkdown theme?

Empty TOC-slide using Rmarkdown beamer template