[R] Adding a table of contents to html output using the bookdown package

MacQueen, Don macqueen1 at llnl.gov
Wed Feb 21 23:21:09 CET 2018

I am trying to get rmarkdown with bookdown to include a table of contents in html output, and having trouble.

Here is an example that I think illustrates the trouble.
I have a file "test.Rmd" as follows:

[127]% cat test.Rmd
title: Test Document
    toc: true

# Introduction

some text

# A section

some more text

If I run:


I get a table of contents in the html file (test.html). Nice.

However, for more flexibility in switching back and forth between html, pdf, and Word output, and to get section numbering and the ability to cross reference tables and figures by number, I am rendering my .Rmd files using a command like

render('test.Rmd',  output_dir='.',  output_format=html_document2(number_sections=TRUE)  )

Then there is no table of contents.

Are there an additional argument I could add to this render() command to get a TOC? I've looked, and haven't been able to find one.


On the other hand, with
    render('test.Rmd', output_dir='.', output_format=pdf_document2(number_sections=TRUE)  )
I do get a TOC in the pdf document.

> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: OS X El Capitan 10.11.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] bookdown_0.5  rmarkdown_1.6

loaded via a namespace (and not attached):
 [1] compiler_3.4.2  backports_1.1.1 magrittr_1.5    rprojroot_1.2  
 [5] htmltools_0.3.6 tools_3.4.2     yaml_2.1.14     Rcpp_0.12.14   
 [9] stringi_1.1.5   knitr_1.17      stringr_1.2.0   digest_0.6.13  
[13] openxlsx_4.0.17 evaluate_0.10.1

Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
Lab cell 925-724-7509

More information about the R-help mailing list