问题描述
我正在用R markdown(.rmd)编写文档.我希望能够同时编织Word和PDF输出.我在数字编号方面遇到困难.使用PDF输出时,数字会自动编号(通过fig.lp的Latex输出?),但是数字未在Word中编号.
I am writing a document in R markdown (.rmd). I would like to be able to knit both Word and PDF outputs. I am having difficulty with figure numbering. With PDF output, the figures were automatically numbered (via Latex output of fig.lp?) But figures were not numbered in Word .
经过大量搜索,我找到了可以在Word中提供图形编号的代码-但是现在,在编织PDF时获得了双页编号.我是新手,所以我无法插入图片.但是图形标题看起来像:
After much searching, I found code that will provide figure numbering in Word - but now I get double page numbering when knitting a PDF. I'm new, so I can't insert an image. But the figure caption looks like:
图1.图1. Blah Blah Blah
Figure 1. Figure 1. Blah Blah Blah
是否可以禁止对PDF进行自动编号?
有人问了类似的问题,但未提供解决方案.我的YAML标头和图形编号模块如下.
A similar question was asked here, but a solution was not given.My YAML header and figure numbering chunck are included below.
YAML:
output:
pdf_document:
fig_caption: yes
keep_tex: yes
word_document:
fig_caption: yes
图形编号代码(可通过 http://galahad.well.ox.ac找到. uk/repro/)
Figure numbering code (found via http://galahad.well.ox.ac.uk/repro/)
figRef <- local({
tag <- numeric()
created <- logical()
used <- logical()
function(label, caption, prefix = options("figcap.prefix"),
sep = options("figcap.sep"), prefix.highlight = options("figcap.prefix.highlight")) {
i <- which(names(tag) == label)
if (length(i) == 0) {
i <- length(tag) + 1
tag <<- c(tag, i)
names(tag)[length(tag)] <<- label
used <<- c(used, FALSE)
names(used)[length(used)] <<- label
created <<- c(created, FALSE)
names(created)[length(created)] <<- label
}
if (!missing(caption)) {
created[label] <<- TRUE
paste0(prefix.highlight, prefix, " ", i, sep, prefix.highlight,
" ", caption)
} else {
used[label] <<- TRUE
paste(prefix, tag[label])
}
}
})
然后在块选项中按如下所示调用它:
this is then called in chunk options as follows:
```{r, echo=FALSE, message=FALSE, fig.width=6, fig.cap=figRef("Ex-Airfoil", "Example NACA Airfoil")}
推荐答案
是否可以禁止对PDF进行自动编号?
好的.为您的输出格式添加一个format
变量,并在figref
中为该格式添加一个处理程序.使用RStudio预览版,您可以使用format <- knitr::opts_knit$get("out.format")
,但是对于发行版,则需要手动进行设置.
然后在figref()
中添加所需的输出...
Sure. Add a format
variable for your output format and a handler for that format within figref
. With RStudio preview edition you could use format <- knitr::opts_knit$get("out.format")
but with the release version you'd need to set it manually.
Then in figref()
add whatever you desire for the output...
if ( format == "latex" ) return( caption )
if (!missing(caption)) {
--- >8 ---
我个人将使用预览版和switch语句进行处理.遵循 https://stackoverflow.com/a/27321367/173985 .
Personally I'd use the preview edition and a switch statement for the handling. Along the lines of https://stackoverflow.com/a/27321367/173985.
这篇关于使用R markdown/knitr抑制pdf输出中的自动图形编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!