When using knitr
and rmarkdown
together to create a word document you can use an existing document to style the output.
For example in my y
This was actually a known issue. Fortunately, it was solved in v2.0 or later releases of pandoc
.
And I have tested the newer version, and found that there is a newly-added hidden style called "Table". Following @CL.'s suggestions to change "Table" style in reference.docx
will be okay now.
In addition, look at this entry of pandoc
's v2.0 release notes:
Use
Table
rather thanTable Normal
for table style (#3275).Table Normal
is the default table style and can’t be modified.
TableNormal
doesn't work for me too.
On my Dutch version of Word 2016 (Office 365), I found out that I could markup tables with the style Compact
.
Input (refdoc.docx
contains the Compact
style):
---
title: "Titel"
subtitle: "Ondertitel"
author: "`r Sys.getenv('USERNAME')`"
output:
word_document:
toc: true
toc_depth: 2
fig_width: 6.5
fig_height: 3.5
fig_caption: true
reference_docx: "refdoc.docx"
---
And RMarkdown:
# Methoden {#methoden}
```{r}
kable(cars)
```
Output:
This is essentially a combination of the answer that recommends TableNormal, this post on rmarkdown.rstudio.com and my own experiments to show how to use a TableNormal
style to customize tables like those generated by kable
:
RMD:
---
output:
word_document
---
```{r}
knitr::kable(cars)
```
TableNormal
as style name and define the desired styles. In my experiments most styles worked, however some did not. (Adding a color to the first column and making the first row bold was no problem; highlighting every second row was ignored.) The last screenshot in this answer illustrates this step.styles.docx
.Modify the header in the RMD file to use the reference DOCX (see here; don't screw up the indentation – took me 10 minutes find this mistake):
---
output:
word_document:
reference_docx: styles.docx
---
Knit to DOCX again – the style should now be applied.
Following the steps I described above yields this output:
And here a screenshot of the table style dialog used to define TableNormal
. Unfortunately it is in German, but maybe someone can provide an English version of it:
As this does not seem to work for most users (anyone but me …), I suggest we test this systematically. Essentially, there are 4 steps that can go wrong:
TableNormal
style is saved in the DOCX.I therefore suggest using the same minimal RMD posted above (full code on pastebin) to find out where the results start do differ:
TableNormal
added: reference.docxThe three files are generated on the following system: Windows 7 / R 3.3.0 / RStudio 0.99.896 / pandoc 1.15.2 / Office 2010.
I get the same results on a system with Windows 7 / R 3.2.4 / RStudio 0.99.484 / pandoc 1.13.1 / Office 2010.
I suppose the most likely culprits are the pandoc and the Office versions. Unfortunately, I cannot test other configurations at the moment. Now it would be interesting to see the following: For users where it does not work, what happens …
With a number of users running these tests it should be possible to find out what is causing the problems.
I was able to get my word output to use a default table style that I defined in a reference .docx.
Instead of 'TableNormal', the table style it defaulted to was 'Table'.
I discovered this by knitting an rmarkdown with a kable.
---
date: "December 1, 2017"
output:
word_document:
reference_docx: Template.docx
---
`r knitr::kable(source)`
Then I took a look at that generated document's XML to see what style it had defaulted to.
require(XML)
docx.file <- "generated_doc.docx"
## unzip the docx converted by Pandoc
system(paste("unzip", docx.file, "-d temp_dir"))
document.xml <- "temp_dir/word/document.xml"
doc <- xmlParse(document.xml)
tblStyle <- getNodeSet(xmlRoot(doc), "//w:tblStyle")
tblStyle
I defined the 'Table' style to put some color and borders in the reference docx. This works for one standard table style throughout the document, I haven't found a way to use different styles throughout.
This stayed true even after I opened the reference doc and edited it.
You need to have a reference_docx: style.docx
which has "Table" style in it. (see @Liang Zhang's explanation and links above).
pandoc -o custom-reference.docx --print-default-data-file reference.docx
reference.docx
file, find the table created (a basic 1 row table with a caption).Using this reference document, you can also change the table and figure caption styles.