Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly number headings in Word from a RMarkdown document

I'm creating a RMarkdown document that I want to export in MS Word with RStudio.

I want a table of contents and numbered headings. Here is my sample markdown document:

---
title: "Test"
author: "Ben"
date: "`r format(Sys.time(), '%d/%m/%Y')`"
output:
  word_document:
    toc: yes
---

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

# Header 1

## Header 2

## Header 2

### Header 3

This produces the following word doc:

Could be better

That's a good start. Following this tutorial, I edited the heading styles of the output document in Word to make them numbered.

I also changed the Table of contents title heading so that it's based on normal text and not another heading, otherwise the table of contents title gets numbered as well.

I saved the modified document in a template folder and added it as reference in the markdown header:

---
title: "Test"
author: "Ben"
date: "`r format(Sys.time(), '%d/%m/%Y')`"
output:
  word_document:
    toc: yes
    reference_docx: "../templates/word-styles-reference-01.docx"
---

Here is the output:

Better

Now, I want a page break after my table of contents, so I followed this other tutorial and changed my Heading 6 so that it is white, very small, based on the normal style and adds a page break afterwards.

The new markdown file looks like this:

---
title: "Test"
author: "Ben"
date: "`r format(Sys.time(), '%d/%m/%Y')`"
output:
  word_document:
    toc: yes
    reference_docx: "../templates/word-styles-reference-01.docx"
---

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

###### Page break after table of contents

# Header 1

## Header 2

## Header 2

### Header 3

And here is the output:

Ugly

I now have my page break but the Heading 6 title is numbered by Word and thus my first title is numbered 2.

In the end, it's either:

  • a word question: how do I modify the style of Heading 6 so it's not numbered?
  • an R question: how can I add a page break without using word headings?
like image 310
Ben Avatar asked Feb 01 '17 14:02

Ben


People also ask

How do you set the heading level in R Markdown?

We can insert headings and subheadings in R Markdown using the pound sign # . There are six heading/subheading sizes in R Markdown. The number of pound signs before your line of text determines the heading size, 1 being the largest heading and 6 being the smallest.


2 Answers

Following on from the Blockquote debate. I found the following solution:

R Markdown

add a block quote with a space

>  

Word template

The block quote is formatted as Block Text style in Word.

Edit the Block Text style

  • small font (I use size 3)
  • change the paragraph settings
    • no space before/after
    • tick "page break before"
like image 95
Joanne Demmler Avatar answered Sep 19 '22 15:09

Joanne Demmler


There is a few needs for you : first, page break AND unumbered title (see steps 3 & 4), then, maybe you want to adress the size of your titles (see bonus steps) and maybe there is a necessary last stuff to indicate to MS word (see steps 6). In every case, you should manage your word template properly for made a page-break with header 6 and I guess you've miss a step (I translate from the French interface of MS word, maybe there is a few differences in the English version) :

1) First, open your 'reference_docx' in MS word (which is, for you : "../templates/word-styles-reference-01.docx"). It's a good idea to save a copy of that by security.

2) Define a style for header 6 by right-clicking on it in the 'ribon of styles', then select 'modify'.

3) In the dedicated windows for indicate your 'header 6 style' (first case will be 'name : Title 6'), click on 'Format' button ==> then 'numbering' (or 'numbers') ==> then select 'none' ==> Then validate your choice.

4) In the same dedicated windows for modifying styles of 'title 6', click again on 'Format' ==> then on the 'paragraph' button ==> then the second tab named 'chaining' (or 'linking', in french 'enchaînement') ==> click on 'page break before' ==> then validate.

5) Validate your modifications and save your reference word document.

  • Try at this point to knit your rmardkown to a word output. In your Rmarkdown document, you should made your first notes page with this : ###### [and a space].
  • Note that if you properly manage your word_template, you gain access to the possibility of a page break with an unumbered title like that : "###### NOTES^[Page left empty.]", wich is better if your page break is a "notes-pages" and not a page-break. If your page-break is only a single 'note-page', you can't define a police-size of 1 for the title.

Bonus step : If you want ONLY a page-break without title, you should define a size of 1 for the police of the title 6. Indicate this in the first screen of 'modifying your title 6' (the one wich begin by 'name : Title 6', after you right-click on your title 6 and then 'modifying'). If you want a note page with a title like 'Notes', you can't define a police size of 1 for your page break.

If the 'title style 6' don't working for made an unumbered page-break, go in the final step (good luck).

5) It's sometimes necessary to go in the list-tools which is in the 'paragraph' ribbon (left to your 'styles ribon') and click on the symbol with a list of '1 / a / i' (the one for define a 'list of several levels') ==> then click on 'define a new list of several levels' ==>

  • define precisely every title(s) styles with this tools, by clicking on every number of titles at the left (1 to 6 cause you're working in rmarkdown so you had only 6 choices of titles), then click on 'plus' (or 'more') ==> then indicate precisely what you want for this title in the right-part of the 'list of several levels' panel (typically you want to 'restart the list' with every levels, and the page-break 'title 6' maybe need to adress that, you maybe want to center or made indentation for your titles at every steps of the list).

Congratulations, you are free for ms word for a while (except that your titles are in the microsoft blue, and you should write your title in black by playing with every single styles by clicking on 'modifying' for every of them, you should had the same police styles in most of your titles, etc.). Microsoft torture is so elaborate...

like image 39
Clément LVD Avatar answered Sep 21 '22 15:09

Clément LVD