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

时间:2021-02-23 06:41:35

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 .

我正在写一个R markdown(.rmd)的文档。我希望能够编写Word和PDF输出。我的图号编号有困难。使用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. I'm new, so I can't insert an image. But the figure caption looks like:

经过多次搜索,我找到了能够在Word中提供图形编号的代码 - 但是现在我在编织PDF时得到了双页编号。我是新手,所以无法插入图片。但数字标题看起来像:

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")}

1 个解决方案

#1


2  

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. 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...

当然。为输出格式添加格式变量,并在figref中为该格式添加处理程序。使用RStudio预览版,您可以使用格式< - knitr :: opts_knit $ get(“out.format”),但是在发布版本中您需要手动设置它。然后在figref()中添加你想要的输出...

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

Personally I'd use the preview edition and a switch statement for the handling. Along the lines of https://*.com/a/27321367/173985.

我个人会使用预览版和switch语句进行处理。沿着https://*.com/a/27321367/173985。

#1


2  

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. 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...

当然。为输出格式添加格式变量,并在figref中为该格式添加处理程序。使用RStudio预览版,您可以使用格式< - knitr :: opts_knit $ get(“out.format”),但是在发布版本中您需要手动设置它。然后在figref()中添加你想要的输出...

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

Personally I'd use the preview edition and a switch statement for the handling. Along the lines of https://*.com/a/27321367/173985.

我个人会使用预览版和switch语句进行处理。沿着https://*.com/a/27321367/173985。