Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: how to change font and alignment of text in knitr word

```{r results = 'asis'}
for(i in 1:10){
  cat(paste("This is iteration number" i))
}
```

After I output the resulting .docx file, the text is left-aligned in size 12 Cambria font by default. How can I change the alignment to center, change the font size, and font type in my .Rmd file?

like image 793
Adrian Avatar asked Oct 28 '25 04:10

Adrian


1 Answers

One way to do this is to use the reference_docx tag in the YAML header like this:

---
title: "Untitled"
author: "Me X. Person"
date: "April 3, 2018"
output:
  word_document:
    reference_docx: word-style-02.docx
fontsize: 10pt
---

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

```{r results = 'asis'}
for(i in 1:10){
  cat(paste("This is iteration number", i, "<P><br>"))
}
```

Next, prepare a file in Word where all you do is control the styles -- I knit to Word and found out that in this example, the code is controlled by the style titled Compact. So I clicked on that square in the ribbon in the MS Word header, and set it to 16 point Courier New (centered). The Word file that I used is here so you can download and open/try it: Drive link

The output it generates looks like this in Word:

enter image description here

If this is way off of what you were looking for, just let me know and I will delete it. Hopefully it is at least little bit useful.

like image 167
mysteRious Avatar answered Oct 29 '25 19:10

mysteRious



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!