I have an automated report which I am creating in R markdown and knitting to Microsoft Word. At the start of the report, there is an executive summary which includes some inline code referencing calculated figures. The executive summary is displayed on a coloured background, which I achieved by creating a custom style in the reference document.
Currently, the table of contents is always printed at the beginning of the document, however I would like it to appear after the executive summary. I would still like it to include the usual contents formatting (numbered headings and page numbers) as some people may be looking at the printed document (hard copy) so hyperlinks will not always be sufficient.
I've had a look for solutions, but none of them quite seem to fit my scenario.
For example, the render_toc()
function here prints the contents as a list of hyperlinks anywhere in the document, but it doesn't number headings and also doesn't print the page numbers.
I've also seen the latex \tableofcontents
tag but this doesn't work for a Microsoft word output.
The only other potential solution I could find was to use the parameter include-before
in my YAML header - but the only examples I can find of this show the actual text in the YAML header (for example see here), which won't work for my case as it is a complex summary that has a lot of inline code and needs to appear on the custom css style coloured background.
Does anyone have any suggestions?
Here is a small and (very) simplified example - I would expect the document title and executive summary to be on the first page, then the contents should start after the page break on page 2, then the remaining sections should be after the contents:
---
title: "My report"
toc-title: "Report contents"
output:
officedown::rdocx_document:
reference_docx: "templates/wordtemplate.docx"
toc: TRUE
toc_depth: 3
---
```{r setup, include=FALSE, purl=FALSE}
# Set chunk options:
knitr::opts_chunk$set(echo = FALSE,
message = FALSE,
warning = FALSE,
eval = TRUE,
ft.align = "left",
fig.width = 10,
out.width = "100%")
```
```{r data}
# Example data:
df <- data.frame(id = c(1,2,3), fruit = c("apple", "orange", "apple"))
```
:::{custom-style="Colour_background"}
<br>
# Executive summary
There were `r nrow(df)` people who responded to the survey.
<br>
:::
\pagebreak
# Introduction
## Objectives
Objectives of study
## Background
Background to study
# Methods
Some methods text
# Results
Some results text