在 R Markdown 中使用 pandoc 和 bookdown 删除图形标题中的冒号

Remove colon in figure caption using pandoc and bookdown in R Markdown

我正在更改我的 R Markdown 中图形标题的字体,并使用 bookdown 和 pandoc 来执行此操作。我的问题与: 密切相关。我能够获得正确的图编号,并且能够更改标题“图 1”部分的格式。但是,我不知道如何删除输出中的冒号(即“图 1:.”)。

最小示例

Pandoc 函数(取自

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

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

R Markdown

---
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)
```

输出

Figure 1:. This is a caption.

期望输出

Figure 1. This is a caption.

在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 执行此操作。

看起来 rmarkdown 中有一个默认添加冒号的更改。这也是链接 post 中的答案不再起作用的原因。有关这方面的更多信息和解决方案,请参阅 https://community.rstudio.com/t/how-to-change-the-figure-table-caption-style-in-bookdown/110397.

除了那里提供的解决方案之外,您还可以通过用点替换冒号来达到您想要的结果。调整由 提供的 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