创建渲染为 .docx 的 rmarkdown,并在标题中添加引用和上标

问题描述 投票:0回答:1

我正在从 rmarkdown 创建一个 .docx 文档。我使用

bookdown
渲染,因为我喜欢使用文本引用。我在处理包含两个图的图形的标题时遇到困难,为此我需要带有上标的标题。渲染为 pdf 效果很好,但标题没有出现在 docx 中。

---
title: "Untitled"
output:
  bookdown::word_document2: 
    number_sections: false
  bookdown::pdf_document2:
    number_sections: false
  bookdown::html_document2: 
    number_sections: false
date: "2023-06-19"
always_allow_html: false
bibliography: C:/Users/mrd19rph/OneDrive - Bangor University/PhD Folder/General introduction/library.bib
---

'''{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = FALSE, warning = F, message = F)

library(ggplot2)
'''

Details are shown in Figures \@ref(fig:testfig) and \@ref(fig:testfig2).

(ref:testfigcap) Caption for figure with a citation [@Borg1967].

'''{r testfig, out.height= "20%", fig.cap="(ref:testfigcap)"}
ggplot(iris, aes(x = Sepal.Length  , y = Petal.Length))+geom_point()
'''

Text here.

(ref:testfigcap2) Caption containing text^superscript^ for A: by hp and B by wt


'''{r testfig2, fig.show='hold', out.width= "55%",  fig.cap= '(ref:testfigcap2)'}
plota = ggplot(mtcars, aes(x= mpg, y =hp))+geom_point()+
  labs(title = "A")

plotb = ggplot(mtcars, aes(x= mpg, y =wt))+geom_point()+
  labs(title = "B")

plota
plotb
'''
The use of the text reference for a caption  works for the single plot figure, but not for the double plot figure.

PDF 渲染

文字渲染

r r-markdown figure bookdown caption
1个回答
0
投票

我认为这是Word的限制,即你不能为两个单独的图形添加标题。解决这个问题的一种可能的选择是通过使用例如将两个图表粘合在一起来创建一个图。

gridExtra::grid.arrange
patchwork
或 ...:

---
title: "Untitled"
output:
  bookdown::word_document2: 
    number_sections: false
  bookdown::pdf_document2:
    number_sections: false
  bookdown::html_document2: 
    number_sections: false
date: "2023-06-19"
always_allow_html: false
---

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = FALSE, warning = F, message = F)

library(ggplot2)
```

Details are shown in Figures \@ref(fig:testfig) and \@ref(fig:testfig2).

(ref:testfigcap) Caption for figure with a citation [@Borg1967].

```{r testfig, out.height= "20%", fig.cap="(ref:testfigcap)"}
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point()
```

Text here.

(ref:testfigcap2) Caption containing text ^superscript^ for A: by hp and B by wt


```{r testfig2, fig.cap= '(ref:testfigcap2)'}
plota <- ggplot(mtcars, aes(x = mpg, y = hp)) +
  geom_point() +
  labs(title = "A")

plotb <- ggplot(mtcars, aes(x = mpg, y = wt)) +
  geom_point() +
  labs(title = "B")

gridExtra::grid.arrange(plota, plotb, nrow = 1)
```

The use of the text reference for a caption  works for the single plot figure, but not for the double plot figure.

© www.soinside.com 2019 - 2024. All rights reserved.