我使用RMarkdown生成docx报告,并且在代码内部使用特殊的打印功能。此外,我使用bookdown
进行引用。
这里是一个示例:
---
title: "styles"
output:
bookdown::word_document2:
fig_caption: yes
---
```{r setup, include=FALSE}
library(ggplot2)
knitr::opts_chunk$set(echo = FALSE, dpi = 400, fig.width = 5/2.54, fig.height = 5/2.54)
```
## With ggplot2 (**Figure \@ref(fig:ggplot)**):
```{r fig2, echo=FALSE, results='asis'}
print_output=function() {
xx=qplot(cyl, mpg, data=mtcars)
cat("Blah blah blah\n\r")
knitr::knit_print(xx)
cat("\n\r")
cat("<caption> (\\#fig:ggplot) This is a ggplot but style is not ok</caption>", "\n\r")
cat("Blah blah blah", "\n\r")
# return(1)
}
print_output()
```
**Figure style is "body text" and caption style is "Compact".**
## With automatic captionning (**Figure \@ref(fig:fig3)**):
```{r fig3, echo=FALSE, fig.cap="Automatic captionning is OK with style"}
qplot(cyl, mpg, data=mtcars)
```
**Figure style is "captioned figure" and caption style is "image caption".**
实际上,生成的md
文件是不同的:
![plot of chunk fig2](figure/fig2-1.png)
<caption> (\#fig:ggplot) This is a ggplot but style is not ok</caption>
对:
![Automatic captionning is OK with style](figure/fig3-1.png)
我尝试使用captioner
软件包获得了相同的结果。
如何使自定义函数生成的样式能够像fig.cap
一样正确应用?更好的是,有没有一种方法可以指定我要用于图形,标题,表格等的docx样式名称?