3
votes

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.

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

My RMarkdown document looks like this ...

---
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 ... enter image description here

Thank you in advance for your help.

2
I'm not quite sure about what exactly the style is for the code chunks in Word (I do not have Word at hand at the moment), but when you put your cursor in any code chunks, the style should be highlighted in the Styles panel (if the style exists): vimeo.com/110804387Yihui Xie
@Yihui, thanks. The style is "NormalTOK" when the cursor is on "tmp", it is "KeywordTOK" when on "rnorm", and it is "SourceCode" when on the output (e.g., "Min."). IF I highlight an entire row in the output then it is "SourceCode". Also see comment to Dirk's "answer" below.D Ogle

2 Answers

2
votes

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

enter image description here

0
votes

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

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.

Sytle from the text of the example

For the code output we have the followinng dependencies

Sytle from the code output

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

Style dependency from body text

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 ^_^)...