【发布时间】:2015-05-02 01:09:06
【问题描述】:
当我使用 rmarkdown 包将 Rmd 制作成 md 时,我可以通过以下方式包含目录:
md_document:
toc: true
但它们没有链接。现在我可以在使用render 之后使用我创建的这个函数手动执行此操作:
rmarkdown::render("README.Rmd", "all")
md_toc <- function(path = {(x <- dir())[tools::file_ext(x) == "md"]}){
x <- suppressWarnings(readLines(path))
inds <- 1:(which(!grepl("^\\s*-", x))[1] - 1)
temp <- gsub("(^[ -]+)(.+)", "\\1", x[inds])
content <- gsub("^[ -]+", "", x[inds])
x[inds] <- sprintf("%s[%s](#%s)", temp, content,
gsub("[;/?:@&=+$,]", "", gsub("\\s", "-", tolower(content))))
cat(paste(x, collapse = "\n"), file = path)
}
md_toc()
它的工作原理是读回文件并手动插入格式为[Section 1](#section-1) 的链接。
有没有更好的方法让 md toc 链接到这些部分?
如果更简单,我将其作为 GitHub repo,但这是 MWE Rmd:
---
title: "testing_Rmd"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
toc: true
theme: journal
number_sections: true
pdf_document:
toc: true
number_sections: true
md_document:
toc: true
---
# Section 1
Stuff
# Section 2
More Stuff
## Random Stuff A
1 + 2
## Random Stuff B
```{r}
1 + 2
```
# Conclusion
【问题讨论】:
标签: r r-markdown