在不影响R代码块输出的情况下,我可以使用RMarkdown和knitr改变MSWord的正常风格吗?

时间:2022-05-09 06:12:33

I am attempting to write a document using RMarkdown v2 (see below) and then knit this to MSWord using "Knit Word" within RStudio (v 0.8.1091) (knitr v1.8). I have modified the normal style in a docx document that was created through knitting such that text with the normal style will be double-spaced and the first line of the paragraph will be indented. I included this docx file in reference_docx in the YAML header. This works fine except that the R code is also double-spaced with the first line indented (see below). I had assumed that the code chunk was a different style that I could alter but I have not yet determined what that style is.

我正在尝试使用RMarkdown v2编写一个文档(参见下面),然后使用RStudio中的“针织Word”(v 0.8.1091) (knitr v1.8)将其编织到MSWord中。我已经修改了docx文档中的常规样式,该文档是通过编织而创建的,这样的文本与正常的样式将是双倍行距的,段落的第一行将被缩进。我将这个docx文件包含在YAML头部的reference_docx中。这很好,除了R代码也是双间距的,第一行是缩进的(见下面)。我假设代码块是可以修改的另一种样式,但我还没有确定该样式是什么。

So, how can I change the main text in my resulting MSWord file without changing the R code chunk style?

那么,如何在不改变R代码块样式的情况下更改生成的MSWord文件中的主文本?

My RMarkdown document looks like this ...

我的RMarkdown文档看起来是这样的……

---
title: "Chapter XX: XXXXX"
author: "Derek H. Ogle"
output:
  word_document:
    highlight: tango
    reference_docx: CSS.docx
---

R code can be shown in a box

```{r}
tmp <- rnorm(100)
summary(tmp)
```

R results can be included in a dynamic sentence, like this one that shows that the mean of the temporary data.frame is `r round(mean(tmp),2)` and the standard deviation is `r round(sd(tmp),2)`.

The resulting Word document looks like this ... 在不影响R代码块输出的情况下,我可以使用RMarkdown和knitr改变MSWord的正常风格吗?

生成的Word文档如下所示……

Thank you in advance for your help.

事先谢谢你的帮助。

2 个解决方案

#1


2  

Open your CSS.docx in Word, search for the style named SourceCode. Then edit this style to have single line spacing.

打开你的CSS。docx在Word中搜索名为SourceCode的样式。然后编辑此样式,使其具有单行间距。

在不影响R代码块输出的情况下,我可以使用RMarkdown和knitr改变MSWord的正常风格吗?

#2


0  

Your template file should be a dotx extension and not docx. (make sure you save it as template)

您的模板文件应该是dotx扩展名,而不是docx。(确保将其保存为模板)

As far as I can see the text of your example is defined by the style "first paragraph" which is based on "Body Text" and the output code has its own style called "Source Code" based on "Normal".

在我看来,您的示例文本是由基于“正文文本”的样式“第一段”定义的,输出代码有自己的风格,即基于“正常”的“源代码”。

Changing "Body Text" so that all paragraphs are changed (and not just first paragraph) should not change "Source Code". However "Source Code" is based on "Normal". It turns out that "Body Text" in turn is based on "Normal". So if you change "Normal" you changed both. I suspect that it is what happened.

改变“正文”,使所有段落都改变(而不只是第一段)不应改变“源代码”。然而,“源代码”是基于“正常”的。事实证明,“正文”反过来也是基于“正常”。所以如果你改变了“正常”你就同时改变了这两个。我怀疑是发生了什么。

在不影响R代码块输出的情况下,我可以使用RMarkdown和knitr改变MSWord的正常风格吗?

For the code output we have the followinng dependencies

对于代码输出,我们有以下依赖项。

在不影响R代码块输出的情况下,我可以使用RMarkdown和knitr改变MSWord的正常风格吗?

and finally the style "body text" itself is based on Normal

最后,样式“正文”本身是基于正常的

在不影响R代码块输出的情况下,我可以使用RMarkdown和knitr改变MSWord的正常风格吗?

I would recommend to restart from a blank output document in order to redefined a brand new template to make sure nothing is screwed up. I myself had to do that (at first and I could not replicate the problem then somehow I changed the "normal" style too, a ^_^)...

我建议重新启动一个空白的输出文档,以便重新定义一个全新的模板,以确保没有任何事情出错。我自己要做的,首先,我不能复制这个问题然后我也改变了“正常”的风格,一个^ _ ^)……

#1


2  

Open your CSS.docx in Word, search for the style named SourceCode. Then edit this style to have single line spacing.

打开你的CSS。docx在Word中搜索名为SourceCode的样式。然后编辑此样式,使其具有单行间距。

在不影响R代码块输出的情况下,我可以使用RMarkdown和knitr改变MSWord的正常风格吗?

#2


0  

Your template file should be a dotx extension and not docx. (make sure you save it as template)

您的模板文件应该是dotx扩展名,而不是docx。(确保将其保存为模板)

As far as I can see the text of your example is defined by the style "first paragraph" which is based on "Body Text" and the output code has its own style called "Source Code" based on "Normal".

在我看来,您的示例文本是由基于“正文文本”的样式“第一段”定义的,输出代码有自己的风格,即基于“正常”的“源代码”。

Changing "Body Text" so that all paragraphs are changed (and not just first paragraph) should not change "Source Code". However "Source Code" is based on "Normal". It turns out that "Body Text" in turn is based on "Normal". So if you change "Normal" you changed both. I suspect that it is what happened.

改变“正文”,使所有段落都改变(而不只是第一段)不应改变“源代码”。然而,“源代码”是基于“正常”的。事实证明,“正文”反过来也是基于“正常”。所以如果你改变了“正常”你就同时改变了这两个。我怀疑是发生了什么。

在不影响R代码块输出的情况下,我可以使用RMarkdown和knitr改变MSWord的正常风格吗?

For the code output we have the followinng dependencies

对于代码输出,我们有以下依赖项。

在不影响R代码块输出的情况下,我可以使用RMarkdown和knitr改变MSWord的正常风格吗?

and finally the style "body text" itself is based on Normal

最后,样式“正文”本身是基于正常的

在不影响R代码块输出的情况下,我可以使用RMarkdown和knitr改变MSWord的正常风格吗?

I would recommend to restart from a blank output document in order to redefined a brand new template to make sure nothing is screwed up. I myself had to do that (at first and I could not replicate the problem then somehow I changed the "normal" style too, a ^_^)...

我建议重新启动一个空白的输出文档,以便重新定义一个全新的模板,以确保没有任何事情出错。我自己要做的,首先,我不能复制这个问题然后我也改变了“正常”的风格,一个^ _ ^)……