Tyl*_*ker 18 r r-markdown
当我使用rmarkdown软件包将Rmd转换为md时,我可以包含一个toc via:
md_document:
toc: true
Run Code Online (Sandbox Code Playgroud)
但他们没有联系.现在我可以在使用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()
Run Code Online (Sandbox Code Playgroud)
它通过读回文件并手动插入表单链接来工作[Section 1](#section-1)
.
有没有更好的方法来使md toc链接到这些部分?
我有这个作为GitHub回购,如果它更容易,但这是一个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
Run Code Online (Sandbox Code Playgroud)
在 pandoc讨论组中工作,这样的东西对你有用吗?
假设源是 testTOC.Rmd...
```{r mdTOC, echo=FALSE}
mdTOC <- grepl("markdown", knitr::opts_knit$get("rmarkdown.pandoc.to") )
```
```{r, engine='bash', results='asis',echo=FALSE, eval=mdTOC}
# toc-template.txt is somewhere in the path and only contains a single line:: $toc$
pandoc --template=toc-template.txt --toc --to html --from markdown testTOC.Rmd |\
pandoc --to markdown --from html
```
Run Code Online (Sandbox Code Playgroud)
输出::
- [Section 1](#section-1)
- [Section 2](#section-2)
- [Random Stuff A](#random-stuff-a)
- [Random Stuff B](#random-stuff-b)
- [Conclusion](#conclusion)
Run Code Online (Sandbox Code Playgroud)
因此,您需要toc: false
在标头 YAML 中设置不重复。
归档时间: |
|
查看次数: |
760 次 |
最近记录: |