简体   繁体   English

使用r markdown / knitr抑制pdf输出中的自动图形编号

[英]Suppress automatic figure numbering in pdf output with r markdown/knitr

I am writing a document in R markdown (.rmd). 我正在写一个R markdown(.rmd)的文档。 I would like to be able to knit both Word and PDF outputs. 我希望能够编织Word和PDF输出。 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 . 使用PDF输出时,数字会自动编号(通过图.lp的Latex输出?)但数字未在Word中编号。

After much searching, I found code that will provide figure numbering in Word - but now I get double page numbering when knitting a PDF. 经过多次搜索,我发现代码将在Word中提供图形编号 - 但现在我在编织PDF时得到双页编号。 I'm new, so I can't insert an image. 我是新手,所以无法插入图片。 But the figure caption looks like: 但图标题如下:

Figure 1. Figure 1. Blah Blah Blah 图1.图1. Blah Blah Blah

Is there a way to suppress the automatic numbering for PDF? 有没有办法抑制PDF的自动编号?

A similar question was asked here , but a solution was not given. 这里也提出类似的问题,但没有给出解决方案。 My YAML header and figure numbering chunck are included below. 我的YAML标题和图号编号包含在下面。

YAML: YAML:

output:
  pdf_document:
    fig_caption: yes
    keep_tex: yes
  word_document:
    fig_caption: yes

Figure numbering code (found via http://galahad.well.ox.ac.uk/repro/ ) 图编号代码(通过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")}

Is there a way to suppress the automatic numbering for PDF? 有没有办法抑制PDF的自动编号?

Sure. 当然。 Add a format variable for your output format and a handler for that format within figref . 为输出格式添加format变量,并在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. 使用RStudio预览版,您可以使用format <- knitr::opts_knit$get("out.format")但是在发布版本中您需要手动设置它。
Then in figref() add whatever you desire for the output... 然后在figref()添加你想要的输出...

    if ( format == "latex" ) return( caption )
    if (!missing(caption)) {
    --- >8 ---

Personally I'd use the preview edition and a switch statement for the handling. 就个人而言,我会使用预览版和switch语句进行处理。 Along the lines of https://stackoverflow.com/a/27321367/173985 . 沿着https://stackoverflow.com/a/27321367/173985

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM