我能够编写一个 chunk_hook 来扫描块输出并添加叠印环境以及每个绘图周围的 \only<i> 部分:
这是钩子:
library(stringi)
overlay_hook = function(x, options) {
x = knitr:::.chunk.hook.tex(x, options)
if (!is.null(options$overlay_start)) {
ind_matches = stri_locate_all_regex(x, "\\\\includegraphics")[[1]]
stri_sub_all(x, from = ind_matches[,2]+1, length = 0) =
paste0("<", seq_len(nrow(ind_matches)) + options$overlay_start - 1 ,">")
}
return(x)
}
knitr::knit_hooks$set(chunk = overlay_hook)
要使用它,您必须将 overlay_start 设置为一个值(例如,如果它应该从第一个动画步骤开始,则为 1)
<<plot, results='hide', overlay_start = 1, fig.height=3>>=
for (i in 1:3)
plot(function(x) x^i)
@