【问题标题】:Suppress automatic figure numbering in pdf output with r markdown/knitr使用 r markdown/knitr 抑制 pdf 输出中的自动图形编号
【发布时间】:2015-03-06 16:28:06
【问题描述】:

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

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

图 1. 图 1. Blah Blah Blah

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

here 提出了类似的问题,但没有给出解决方案。 我的 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")}

【问题讨论】:

  • 好问题。太糟糕了,我只能给一个学分。
  • 我遇到了同样的问题。当编织到bookdown::html_document2 时,数字的自动编号工作正常,但是当编织到bookdown::word_document2 并在 YAML 标题中包含 always_allow_html: TRUE 时,数字会被编号两次。我相信always_allow_html: TRUE 是问题的原因,但还没有解决方案...

标签: r knitr r-markdown


【解决方案1】:

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

当然。为您的输出格式添加一个format 变量,并在figref 中为该格式添加一个处理程序。对于 RStudio 预览版,您可以使用 format &lt;- knitr::opts_knit$get("out.format"),但对于发布版,您需要手动设置。
然后在figref() 中添加任何你想要的输出...

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

我个人会使用预览版和 switch 语句进行处理。按照https://stackoverflow.com/a/27321367/173985 的思路。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-23
    • 2019-11-01
    • 1970-01-01
    • 2015-08-28
    • 2019-05-20
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多