You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quarto中如何在图/表注释内用Markdown引用生成参考文献

在Quarto图表/表格注释中自动解析引用键的方法

一、直接在代码块的图/表标题参数中用引用键

Quarto的代码块选项(比如tbl-cap、fig-cap)本身支持Markdown引用语法,直接写@Smith2020就能被Pandoc识别,自动把该引用纳入参考文献列表。

表格示例:

根据@Smith2020的结果显示:

```{r}
#| label: tbl-tablex
#| tbl-cap: "核心结论(来源:@Smith2020)"
#| echo: false

library(flextable)
foo <- data.frame(组别 = c("A", "B", "C"), 数值 = c(10, 20, 30))
foo %>% 
  flextable() %>% 
  set_table_properties(layout = "autofit")
#### 图表示例:
```qmd
```{r}
#| label: fig-mtcars
#| fig-cap: "马力与油耗关系图(按变速箱类型区分),数据来源:@Smith2020"
#| echo: false

library(ggplot2)
mtcars2 <- mtcars
mtcars2$am <- factor(mtcars$am, labels = c('自动', '手动'))
ggplot(mtcars2, aes(hp, mpg, color = am)) + 
  geom_point() + 
  geom_smooth(formula = y ~ x, method = "loess")
### 二、在表格动态内容(如页脚)中解析引用键
如果要在表格的页脚这类动态生成的文本里用引用键,得用`quarto::cite()`函数配合R代码生成,同时用`!expr`让Quarto解析代码中的引用逻辑。

示例:
```qmd
```{r}
#| label: tbl-tablex
#| tbl-cap: "核心结论"
#| echo: false

library(flextable)
foo <- data.frame(组别 = c("A", "B", "C"), 数值 = c(10, 20, 30))
foo %>% 
  flextable() %>% 
  set_table_properties(layout = "autofit") %>% 
  add_footer_row(values = !expr paste0("数据来源:", quarto::cite("Smith2020"), "."))
`quarto::cite("Smith2020")`会自动调用Quarto的引用解析规则生成对应格式,同时该引用会被自动加入参考文献。

### 三、在ggplot的注释(caption)中解析引用键
同样用`!expr`前缀和`quarto::cite()`函数,让ggplot的动态注释支持引用键:

示例:
```qmd
```{r}
#| label: fig-mtcars
#| fig-cap: "马力与油耗关系图(按变速箱类型区分)"
#| echo: false

library(ggplot2)
mtcars2 <- mtcars
mtcars2$am <- factor(mtcars$am, labels = c('自动', '手动'))
ggplot(mtcars2, aes(hp, mpg, color = am)) + 
  geom_point() + 
  geom_smooth(formula = y ~ x, method = "loess") +
  labs(caption = !expr paste0("数据来源:", quarto::cite("Smith2020"), "."))
`!expr`的作用是告诉Quarto:这部分内容要当作R代码解析,而不是普通字符串,这样引用键就能被正确识别并生成参考文献条目。

内容的提问来源于stack exchange,提问作者Nicolás Sacco
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 09:15:34