在使用R-Markdown创建的Word文档中格式化图标题

huangapple go评论133阅读模式
英文:

Format figure headers in Word created with R-Markdown and

问题

抱歉,我只能翻译您提供的自然语言文本,无法处理代码部分。

英文:

I'm trying to create a R-markdown document in Word format and have gotten most stuff to work as i want but having trouble with my figure headers.

I want to format my figure headers but unfortunately I cant get a title on it to format. I have a template.docx-file with all my formats but that doesn't help me when my figure-header ends up with the Normal style (which needs to be as-is for normal text).

Judging by examples on here this should be possible. I'm using the captioner-package

Reproducible example code here except template-file

---
title: "Untitled"
author: "Some_author"
date: "`r Sys.Date()`"
output: 
  word_document:
    reference_docx: template.docx

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(captioner)
fig_nums <- captioner()

```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

`r fig_nums("Press_plot", "Great figure header")`

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

答案1

得分: 0

根据我的理解,在纯粹的R-Markdown中似乎无法正常工作,但使用officedown包,我最终成功使其工作。

install.packages("officedown")
library(officedown)

此外,我在我的参考文档中创建了两个名为Figure和Caption的样式(当它们不存在时,officedown会发出警告),并具有所需的格式。以下是工作代码:

---
title: "Untitled"
author: "Some_author"
date: "`r Sys.Date()`"
output: 
  officedown::rdocx_document:
    reference_docx: Template.docx
---

```R
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(captioner)
fig_nums <- captioner()

R Markdown

这是一个R Markdown文档。Markdown是一种用于编写HTML、PDF和MS Word文档的简单格式语法。有关使用R Markdown的详细信息,请参阅http://rmarkdown.rstudio.com

当您单击Knit按钮时,将生成一个文档,其中包括内容以及文档内嵌的任何R代码块的输出。您可以像这样嵌入R代码块:

{r pressure, echo=FALSE, fig.cap="economics plot", fig.id = "tsplot", fig.cap.style = "Caption", fig.topcaption=TRUE}
plot(pressure)

请注意,已添加echo = FALSE参数以防止打印生成图表的R代码。

英文:

As far as I understand this doesn't seem to work in vanilla R-Markdown but with the package officedown I finally got it to work.

install.packages(&quot;officedown&quot;)
library(officedown)

On top of that I created in my reference document two styles named Figure and Caption (officedown gave a warning when they didn't exist) with the desired formatting. Working code as this:

---
title: &quot;Untitled&quot;
author: &quot;Some_author&quot;
date: &quot;`r Sys.Date()`&quot;
output: 
  officedown::rdocx_document:
    reference_docx: Template.docx
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(captioner)
fig_nums &lt;- captioner()

```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see &lt;http://rmarkdown.rstudio.com&gt;.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:


```{r pressure, echo=FALSE, fig.cap=&quot;economics plot&quot;, fig.id = &quot;tsplot&quot;, fig.cap.style = &quot;Caption&quot;, fig.topcaption=TRUE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

huangapple
  • 本文由 发表于 2023年6月8日 17:07:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定