繁体   English   中英

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

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

我正在写一个R markdown(.rmd)的文档。 我希望能够编织Word和PDF输出。 我的图号编号有困难。 使用PDF输出时,数字会自动编号(通过图.lp的Latex输出?)但数字未在Word中编号。

经过多次搜索,我发现代码将在Word中提供图形编号 - 但现在我在编织PDF时得到双页编号。 我是新手,所以无法插入图片。 但图标题如下:

图1.图1. Blah Blah Blah

有没有办法抑制PDF的自动编号?

这里也提出类似的问题,但没有给出解决方案。 我的YAML标题和图号编号包含在下面。

YAML:

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

图编号代码(通过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])
        }
    }
})

然后在块选项中调用它,如下所示:

```{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()添加你想要的输出...

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

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

暂无
暂无

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

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