这个code example to generate a list of definitions适用于我,但只适用于一个索引列表。每当我试图添加另一个列表(例如,定理)时,只有在设置中最后定义的列表才能正常工作。
可能我没有正确地修改knit_hooks$set():
---
title: "Create several lists"
output: bookdown::html_document2
---
```{r setup, include=FALSE}def_list = list()
Knitr::knit_hooks$set(引擎=函数(前面,选项,环境){
if (before && options$engine == 'definition') {# collect definition terms from options$namedef_list[[options$label]] <<- options$name}NULL})
thm_list = list()
knitr::knit_hooks$set(engine =function(前面,选项)){
if (before && options$engine == 'theorem') {# collect theorem terms from options$namethm_list[[options$label]] <<- options$name}NULL})
```{definition, d1, name='Foo: My first definition'}Foo被定义为..。
```{theorem, t1, name='My first theorem'}第一个定理..。
```{definition, d2, name='Bar: My second definition'}酒吧定义为..。
```{theorem, t2, name='My second theorem'}第二个定理..。
---
**All definitions:**
```{r echo=FALSE, results='asis'}def_list = unlist(def_list)
cat(sprintf('- \@ref(def:%s) %s',name( def_list),def_list),sep = '\n')
**All theorems:**
```{r echo=FALSE, results='asis'}thm_list = unlist(thm_list)
cat(sprintf('- \@ref(thm:%s) %s',name( thm_list),thm_list),sep = '\n')
产出:

发布于 2019-09-26 00:19:49
问题是你重新设置了钩子。因此,应该将钩子设置一次以处理两个列表,如下所示:
---
title: "Create several lists"
output: bookdown::html_document2
---
```{r setup, include=FALSE}def_list = list()
thm_list = list()
knitr::knit_hooks$set(engine =function(前面,选项)){
if ( before ) { if ( options$engine == "theorem" ) { thm_list[[options$label]] <<- options$name } else if ( options$engine == "definition" ) { def_list[[options$label]] <<- options$name }}NULL})
```{definition, d1, name='Foo: My first definition'}Foo被定义为..。
```{theorem, t1, name='My first theorem'}第一个定理..。
```{definition, d2, name='Bar: My second definition'}酒吧定义为..。
```{theorem, t2, name='My second theorem'}第二个定理..。
---
**All definitions:**
```{r echo=FALSE, results='asis'}def_list = unlist(def_list)
cat(sprintf('- \@ref(def:%s) %s',name( def_list),def_list),sep = '\n')
**All theorems:**
```{r echo=FALSE, results='asis'}thm_list = unlist(thm_list)
cat(sprintf('- \@ref(thm:%s) %s',name( thm_list),thm_list),sep = '\n')
然后得到所需的输出:

https://stackoverflow.com/questions/58029200
复制相似问题