如何在 Rmarkdown 中添加目录? [英] How to add table of contents in Rmarkdown?

查看:65
本文介绍了如何在 Rmarkdown 中添加目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RStudio 编写 Markdown 文档,并希望在文档顶部添加目录 (TOC),以便用户可以单击相关部分进行阅读.rpubs 上有一些相关的例子,但现在我似乎找不到它们.请注意,我不使用 pandoc 并且对 Rmd 很陌生 &knitr.有没有办法在不使用 pandoc 的情况下添加 TOC?如果必须使用 pandoc 那么哪些函数是相关的?

I am using RStudio for writing markdown documents and want to add Table of Contents (TOC) at top of the documents so that the user could click the relevant section for reading. There were some relevant examples on rpubs but now I can't seem to find them. Please note that I don't use pandoc and am quite new to Rmd & knitr. Is there any way to add TOCs without using pandoc? If using pandoc is must then which functions are relevant?

这是一个小的示例页面:

Here's a small sample page:

---
title: "Sample Document"
output:
  html_document:
    toc: true
    theme: united
---

Header 1
---------------
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
    
## Header 2
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. 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.

我尝试在 RStudio v 0.98.864 中运行它,并且成功了!但遗憾的是它在 0.98.501 和 0.98.507 上不起作用.我正在 0.98.501 中撰写我的论文,并且在更新 RStudio 之后,我的一些分析不起作用.所以,我恢复到 0.98.501.我现在应该怎么办?我真的想要 TOC,但又不损害其他分析的输出.

I tried running this in RStudio v 0.98.864 and it worked! but sadly it didn't work on 0.98.501 and 0.98.507. I am working on my thesis in 0.98.501 and after updating RStudio, some of my analyses didn't work. So, I reverted back to 0.98.501. What should I do now? I really want TOCs but without harming the outputs of other analyses.

推荐答案

语法是

---
title: "Sample Document"
output:
  html_document:
    toc: true
    theme: united
---

文档中.确保它位于文档的开头.还要确保您的文档确实有标题,否则 R 无法告诉您在目录中想要什么.

in the documentation. Make sure this is at the beginning of your document. Also make sure your document actually has headers otherwise R can't tell what you want in the table of contents.

这篇关于如何在 Rmarkdown 中添加目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆