Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

r - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...