首页 > 解决方案 > Rmarkdown:在pdf或word输出中修改Figure X标签为Figure SX

问题描述

我正在尝试使用 Rmarkdown 自动标记 Figure 的标题,但Figure 1我希望它不是计数Figure S1,即只是添加到S那里。此处和此处的示例表明使用 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。但是,现在我的实际人物描述不见了?

在此处输入图像描述

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

标签: latexr-markdownknitr

解决方案


您可以使用header-includes:YAML 部分下的一些 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)
```

在此处输入图像描述

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


推荐阅读