繁体   English   中英

在 Rmarkdown 中使用 pandoc 和 bookdown 时如何删除图形标题中的冒号?

[英]How remove colon in figure caption when using pandoc and bookdown in Rmarkdown?

我正在更改 Rmarkdown 中图形标题的字体,并使用bookdown和 pandoc 来这样做。 我的问题与以下内容密切相关: How to change the figure caption format in bookdown? . 此外,我能够获得正确的数字编号,并能够更改标题“图 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

要在 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 做到这一点。

看起来在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

在此处输入图片说明

暂无
暂无

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

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