Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add title to R markdown TOC

Tags:

r

r-markdown

Is it possible to add a title to the TOC generated by an R markdown document.

Specifically, I have something like this now:

---
title: "The Rebel Base Locations"
author: "Darth Vader"
date: "A long time ago in a galaxy far far away"
output: 
  html_document:
    css: custom.css
    toc: true
    toc_depth: 3
    number_sections: true
---

What I want is the TOC to have a header called "Contents". I was able to push it to the left using some css.

like image 578
user1357015 Avatar asked Jun 16 '15 23:06

user1357015


1 Answers

You can add a new field right before the table of contents in the html template. Following this instructions, do this:

  1. Copy the html template to your .rmd file and change the $toc$ field to something like this:
$if(toc)$
<div id="$idprefix$TOC">
<h1 class="toctitle">$toctitle$</h1>
$toc$
</div>
$endif$
  1. Then, add the toctitle and template fields to your RMarkdown file. For example:
toctitle: "Contents"
output: 
  html_document:
    template: toctitle.html
    css: custom.css
    toc: true
    toc_depth: 3
    number_sections: true

This should add the title before your TOC. You can do other modifications, e.g. change from h1 to another tag and/or use the field class on the CSS. If you have problems for not using the default template (mathjax won't work), you can change the default style, not adding totctitle to a different rmd file later shouldn't be a problem.

like image 65
Molx Avatar answered Sep 20 '22 01:09

Molx