【问题标题】:How remove colon in figure caption when using pandoc and bookdown in Rmarkdown?在 Rmarkdown 中使用 pandoc 和 bookdown 时如何删除图形标题中的冒号?
【发布时间】:2021-09-30 23:03:29
【问题描述】:

我正在更改我的 Rmarkdown 中图形标题的字体,并使用 bookdown 和 pandoc 来执行此操作。我的问题与:How to change the figure caption format in bookdown? 密切相关。此外,我能够获得正确的数字编号,并且能够更改标题的“图 1”部分的格式。但是,我无法弄清楚如何删除输出中的冒号(即“图 1:.”)。

最小示例

Pandoc 函数(取自here

function Image (img)
  img.caption[1] = pandoc.Strong(img.caption[1])
  img.caption[3] = pandoc.Strong(img.caption[3])
  img.caption[4] = pandoc.Strong(".  ")
  return img
end

要在 Rmarkdown 中使用 function Image,请将文件另存为“figure_caption_patch.lua”,该文件将在 YAML 元数据中的 pandoc_args 中调用。

降价

---
title: Hello World
author: "Somebody"
output:
  bookdown::word_document2:
    fig_caption: yes
    number_sections: FALSE
    pandoc_args: ["--lua-filter", "figure_caption_patch.lua"]

---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```    

# Test
Some text (Figure \@ref(fig:Xray)). Some text followed by a figure:

```{r Xray, fig.cap="Single-crystal X-ray structure of some text", echo=FALSE}
plot(cars)
```

输出

图 1:. 这是一个标题。

期望的输出

图 1。这是一个标题。

在 pandoc 函数中,我尝试进一步对img.caption[3] 的字符串进行子集化,但没有成功。我尝试了以下方法:

img.caption[3] = pandoc.Strong(string.sub(img.caption[3], 1, 1))

但这当然行不通。我知道如果我使用的是 R,那么我可以执行以下操作:

a = c("words", "again")
substring(a, 1, 1)[1]

#output
[1] "w"

但不确定,如何用 pandoc 做到这一点。

【问题讨论】:

    标签: r r-markdown pandoc bookdown


    【解决方案1】:

    rmarkdown 似乎发生了变化,默认情况下会添加一个冒号。也是链接帖子中的答案不再起作用的原因。有关这方面的更多信息和解决方案,请参阅https://community.rstudio.com/t/how-to-change-the-figure-table-caption-style-in-bookdown/110397

    除了那里提供的解决方案之外,您还可以通过将冒号替换为点来达到您想要的结果。调整 https://stackoverflow.com/a/59301855/12993861 提供的 lua 过滤器可以这样做:

    function Image (img)
      img.caption[1] = pandoc.Strong(img.caption[1])
      img.caption[3] = pandoc.Strong(pandoc.Str(string.gsub(img.caption[3].text, ":", ".")))
      return img
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      相关资源
      最近更新 更多