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
---------
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:
system.file("rmd/h/default.html", package="rmarkdown")
$toc
section to where you want the table of contents to appear. lowertitle.html
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.
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.