使用Pandoc和Bookdown在R Markdown中删除图表标题中的冒号

4
我正在更改R Markdown中的图题字体,并使用bookdown和pandoc来实现。我的问题与这个问题密切相关:如何在bookdown中更改图题格式?我能够获得正确的图编号,并且能够改变图题中“Figure 1”部分的格式。但是,我无法弄清楚如何在输出中删除冒号(即,“Figure 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 中调用该文件。
---
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实现这一点。

1个回答

2

看起来 rmarkdown 发生了变化,现在默认情况下会添加一个冒号。这也是为什么链接帖子中的答案不再有效的原因。有关此问题的更多信息和解决方案,请参见 https://community.rstudio.com/t/how-to-change-the-figure-table-caption-style-in-bookdown/110397

除了那里提供的解决方案外,您还可以通过将冒号替换为句点来实现所需的结果。根据 https://dev59.com/OVQK5IYBdhLWcg3wZe9_#59301855 提供的 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

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接