微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Rmarkdown:在pdf或word输出中修改Figure X标签为Figure SX

如何解决Rmarkdown:在pdf或word输出中修改Figure X标签为Figure SX

我正在尝试使用 Rmarkdown 自动标记 figure 的标题,但不是计算 figure 1,我希望它是 figure S1,即只是在那里添加 SAn example herehere 表明使用 pdf 输出是不可能的。好的,我对 .doc 文件没问题,但我的图标题仍然没有打印出来?我想知道有什么问题?

R 降价的最小示例:

    ---
title: Supporting information
subtitle: "Iron(I) etc"
author: "Some people here"
abstract: "Added the addresses here since there is no abstract in the SI"
output:
  word_document:
    fig_caption: yes
---

```{r,include=F}
library(captioner)

figS<- captioner(prefix = "figure S",auto_space = TRUE,levels = 1,type = NULL,infix = ".")

figS("figure S1","Single-crystal X-ray structure of some text (1)",display=FALSE)

```

```{r Xray,fig.cap=figS("This is my figure description"),echo=FALSE}
plot(cars)
```

这会正确打印出 figure S1。但是,现在我的实际人物描述不见了?

enter image description here

我想要的输出是 pdf,但如果不是,我可以用 word。感谢您提出如何解决此问题的建议!

解决方法

你可以在YAML部分的header-includes:下使用一些LaTeX命令,如下:

---
title: "Untitled"
output: pdf_document
header-includes:
  - \renewcommand{\figurename}{Figure S}
  - \makeatletter
  - \def\fnum@figure{\figurename\thefigure}
  - \makeatother
---

```{r,fig.cap="This is my figure description",fig.height=3}
plot(pressure)
```

```{r,fig.cap="Another figure description",fig.height=3}
plot(iris$Sepal.Length,iris$Petal.Width)
```

enter image description here

(R 代码块中的参数 fig.height 不是必需的;我使用它只是为了在同一页面中获取两个图并截取屏幕截图)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。