Can I change the MSWord Normal style when using RMarkdown and knitr without affecting the R code output

I am trying to write a document using RMarkdown v2 (see below) and then link it to MSWord using "Knit Word" in RStudio (v 0.8.1091) (knitr v1.8). I changed the normal style in the docx that was created by knitting so that the normal style text will be double and the first line of the paragraph will be indented. I have included this docx file under reference_docx in the YAML header. This works great, except that the R code is also double-spaced, indented by the first line (see below). I assumed the piece of code was a different style that I could change, but I have not yet defined what that style is.

So how can I change the body text in my MSWord file without changing the style of the R code snippet?

My RMarkdown doc 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)`.

      

As a result, the Word document looks like this ... enter image description here

Thanks in advance for your help.

+3


source to share


2 answers


Open CSS.docx in Word, find the style named SourceCode

. Then edit this style to have one line spacing.



enter image description here

+2


source


Your template file must be do t x extension , not c x. (make sure you save it as a template)

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

Changing Body Text so that all paragraphs are changed (not just the first paragraph) should not change Source. However, "Source Code" is based on "Normal". It turns out that "Body Text" is in turn based on "Normal". Therefore, if you change "Normal", you have changed both. I suspect this has happened.

Sytle from example text

To output the code, we have the following dependencies



Sytle from code output

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

Body text dependency style

I would recommend restarting from a blank output document to override the new template to make sure nothing is screwed up. I had to do it myself (at first, and I could not reproduce the problem, then somehow I changed the "normal" style too, a ^ _ ^) ...

0


source







All Articles