1

so I am knitting an R Markdown document to Word. In some places I have code chunks where I want to display the code. What I want to do is to make the font size of the chunk smaller.

I've read through standard references on this: Markdown Cookbook, tried adjusting chunk options, plus the R Studio article and even tried rummaging through Pandoc documentation.

The procedure seems to be to direct knitr to a template document, where you edit the Word styles. The problem is that code chunks seem to be comprised of many different Word styles: "CommentTok", "CommentVarTok", "NormalTok", "ConstantTok" i.e. everything that ends with "Tok" in the styles list. So the only way I can see of achieving this is to go through every single one (and there are a lot) and manually scale down the font size of each one.

But that seems like an unsatisfactory solution, especially if I want to later on change to a different font size or make a new template for another project, as it takes me a good 20 mins or so to go through every one and click "10pt" each time. So I am sure there must be a better solution - but I can't find anything anywhere. So can anyone help? Thanks!

Edit: a kind-of-reproducible example is simply to knit the default R Markdown doc to Word. Here's the markdown doc:

---
title: "Test1"
author: "Author"
date: "01/09/2021"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## 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 cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{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.

Save this file as e.g. "word_template1.rmd".

Then knit using rmarkdown::render("word_template1.rmd"). This is now the template document. Following the R Studio article we can edit the styles in this document (in Word), and use this as a style template for other documents.

Here's an .rmd file that I want to knit to Word (below), let's save it as "testdoc.rmd". I have included some messing around in code chunks which is displayed in Word. If you knit this to Word rmarkdown::render("testdoc.rmd") you will see that the code chunks are composed of many different Word styles.

As mentioned above, I know that I can reduce the size of each individual style fairly easily, by changing the font size in "word_template1.docx". But since there are so many, this can be very time consuming. I am looking for global option to change the size of all code styles. Note that the size code chunk option doesn't seem to do anything in Word.

The desired outcome is to have all code chunk font styles in 10pt in Word, with a single command.

---
title: "Test2"
author: "Author"
date: "01/09/2021"
output:
  word_document:
    reference_docx: word_template1.docx
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Test chunk

```{r cars}
# here are a few operations - when output in Word this results in a mixture of styles
summary(cars)

# some dplyr operations
library(dplyr)
cars |>
  mutate(Newcol = speed*dist) |>
  filter(Newcol > 1500)

# creating a silly function
testfunc <- function(x){
  x > 15
}

# silly application of ifelse
ifelse(testfunc(cars$speed), "fast", "slow")
```
Will
  • 381
  • 2
  • 12
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Aug 31 '21 at 17:06
  • Hi, thanks, I have included an example. It is hard to make it completely self-contained because it involves reading and writing, and manually editing styles in Word. Hopefully it can still help to illustrate the problem. – Will Sep 01 '21 at 07:53

0 Answers0