Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the position of the table of contents in rmarkdown?

With RStudio and knitr I see that I can add a TOC with the following code in my .rmd file.

----------------
output: 
  html_document:
    toc: yes
-----------------

However, this places the TOC at the very beginning of the HTML document. Is there a way to move the TOC lower on the page? Say after an introductory paragraph?

I tried to use __TOC__ and __FORCETOC__ but it did not change the TOC position.

like image 782
Jake Russ Avatar asked Aug 30 '14 23:08

Jake Russ


People also ask

What is TOC in R Markdown?

1 Table of contents. You can add a table of contents (TOC) using the toc option and specify the depth of headers that it applies to using the toc_depth option. For example: --- title: "Habits" output: html_document: toc: true toc_depth: 2 ---

How do I change my R Markdown folder?

The usual way to change the working directory is setwd() , but please note that setwd() is not persistent in R Markdown (or other types of knitr source documents), which means setwd() only works for the current code chunk, and the working directory will be restored after this code chunk has been evaluated.

How do you add a table of contents in R Markdown?

You can embed an R code chunk like this: ```{r} summary(cars) ``` You can also embed plots, for example: ```{r, echo=FALSE} plot(cars) ``` ### Header 3 Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

What does ## do in R Markdown?

For example, # Say Hello to markdown . A single hashtag creates a first level header. Two hashtags, ## , creates a second level header, and so on. italicized and bold text - Surround italicized text with asterisks, like this *without realizing it* .


2 Answers

The position of the TOC is fixed in the R Markdown default HTML template. If you want to change its position in the document, you'll need to modify the template:

  1. Make a copy of the R Markdown HTML template to use as a starting point. You can find it by running this R command: system.file("rmd/h/default.html", package="rmarkdown")
  2. Move the $toc section to where you want the table of contents to appear.
  3. Save the modified template in the same folder as the document you're rendering as e.g. lowertitle.html
  4. Add template: lowertitle.html to the html_document settings.

From the standpoint of the template, all of the document's content is an atomic unit, so it might be necessary to put any content you want to appear before the TOC in the template itself.

like image 76
Jonathan Avatar answered Sep 22 '22 21:09

Jonathan


You can use JQuery to relocate the TOC to an arbitrary position in the file. Simply insert a heading where you want the TOC to go, and use the ID generated by rendering the R Markdown file. For example:

<script>
  // Move TOC to the Table of Contents heading (with id "table-of-contents")
  $(function() {
    $( "#TOC" ).insertAfter( $( "#table-of-contents" ) );
  });
</script>

A heading called "Table of Contents" somewhere in the R Markdown file will receive id "table-of-contents". The TOC has id "TOC". The Jquery bit above selects that TOC, and inserts it after the "Table of contents" heading, wherever in the document it's located.

like image 37
Matherion Avatar answered Sep 24 '22 21:09

Matherion