将fig.cap设置为options $ label [英] set fig.cap to options$label
问题描述
如何以编程方式在编织钩中设置图形标题?
How can I programmatically set a figure caption in a knitr hook?
我想将图形标题(如果未明确定义)设置为块标签.我已阅读选项,钩子,尽管我认为我了解其中的作用机制,但我无法使其正常工作.
I'd like to set the figure caption, if not explicitly defined, to the chunk label. I've read the knitr docs on options, options, and hooks, and though I think I understand the mechanisms at play, I can't get it to work.
我的用例也许可以证明这种行为:我的工作流程最近适应于开始在 Rmd 文件中进行数据和可视化探索.我将使用块进行清理,子设置等,然后使用每个可视化示例块.这又快又脏,意味着降价幅度最小.当我查看报告(通常呈现为PDF)时,我将看一个图,并想直接找到它.尽管在图形之前/之后的文本可以提供洞察力,但是由于LaTeX图形规则的存在,这并不是很确定的事情.计算图形编号是可行的,但不是容易的"(并且对于许多图形而言,这是有问题的).标题总是随数字一起出现,因此如果我可以默认将标题标记为大块标签,那就太好了. (是的,我有点懒.)
My use-case that perhaps justifies this behavior: my work-flow recently adapted to start my data and visualization exploration in Rmd files. I'll use chunks for cleaning, subsetting, etc, and then a sample chunk for each visualization. This is quick and dirty, meaning minimal markdown. When I look over the report (typically rendered into PDF), I'll look at a figure and want to go straight to the source for it. Though text before/after the figure can provide insight, due to LaTeX figure rules it is not a sure thing. Counting figure numbers is feasible, but not "easy" (and becomes problematic with many figures). Captions are always with the figure, so it'd be great if I can default to filling the caption with the chunk label. (Yes, it's a little lazy of me.)
MWE在下面.
钩子代码运行得很好;挂钩中返回的字符串正确显示.但是,图形标题未更改. 异常:当存在未定义fig.cap
的块时,所有后续块均将其标题设置为第一个未标题的块名称;出于opts_chunk
的全球性,这并不令我感到惊讶,所以出来了.
The hook code ran just fine; the returned strings in the hook appeared correctly. However, the figure caption did not change. Exception: when there is a chunk with an undefined fig.cap
, all subsequent chunks have their caption set to the first un-captioned chunk name; this doesn't surprise me due to the global nature of opts_chunk
, so that's out.
我怀疑它可能与输出挂钩"副大块挂钩"有关,但这确实是每个大块的事情,我不想修改剧情,只需设置标题
I suspect it might be related to "output hooks" vice "chunk hooks," but this really is a per-chunk thing and I do not want to modify the plot, just set the caption.
MWE:
---
title: "Document Title"
author: "My Name"
output:
pdf_document:
fig_caption: yes
---
# Header
```{r setup}
knit_hooks$set(autocap = function(before, options, envir) {
if (before) {
if (is.null(options$fig.cap)) {
options$fig.cap <- options$label
knitr::opts_current$set(fig.cap = options$label)
knitr::opts_chunk$set(fig.cap = options$label) # wrong!
paste('Set: `', options$label, '`, `',
knitr::opts_current$get('fig.cap'), '`', sep = '')
} else {
paste('Kept: `', options$fig.cap, '`', sep = '')
}
}
})
opts_chunk$set(autocap = TRUE)
```
## No Plot
```{r textOnly}
1+1
```
## Caption Already Set
```{r someplot, fig.cap='someplot caption'}
plot(0)
```
## Caption Not Set
```{r anotherPlot}
plot(1)
```
推荐答案
这样可以吗?我只是简单地修改了knitr内部函数.img.cap
函数可以找到这里.
Is it ok like this ? I simply modify the knitr internal function .img.cap
function which can be found here.
```{r}
.img.cap = function(options) {
if(is.null(options$fig.cap)) options$label else options$fig.cap
}
assignInNamespace(".img.cap", .img.cap, ns="knitr")
```
这篇关于将fig.cap设置为options $ label的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!