Search code examples
rr-markdownofficerofficedown

Why are landscape pages causing issues with page numbers/section headers/footers in officeverse?


When knitting the below Rmd with a landscape page and viewing the word output produced by it, it becomes apparent that page two seems to be missing. Also, page numbering in the footer of the page only starts after the landscape page has been included.

Conversely, when knitting the below Rmd without a landscape page there are no such issues.

Why is that and how can I make the document with landscape pages behave like the one without?

With landscape page:

---
output: officedown::rdocx_document
---

```{r setup, include=FALSE}
library(officedown) # 0.3.0
```

<!---BLOCK_TOC--->

# Chapter 1
# Chapter 2
<!---BLOCK_LANDSCAPE_START--->
# Chapter 3
<!---BLOCK_LANDSCAPE_STOP--->
# Chapter 4
\newpage
# Chapter 5
\newpage
# Chapter 6

Without landscape page:

---
output: officedown::rdocx_document
---

```{r setup, include=FALSE}
library(officedown) # 0.3.0
```

<!---BLOCK_TOC--->

# Chapter 1
# Chapter 2
# Chapter 3
# Chapter 4
\newpage
# Chapter 5
\newpage
# Chapter 6

Solution

  • Page 2:
    Using <!---BLOCK_LANDSCAPE_STOP{break_page: "nextPage"}---> instead of <!---BLOCK_LANDSCAPE_STOP---> fixes this.

    Page numbers in footer starting on page 1:
    Add the below chunk right after <!---BLOCK_TOC--->. The functions in this chunk are implemented in the officer package.

    ```{r}
    footer_default <- block_list(fpar(run_word_field(field = "PAGE"),
                                      fp_p = fp_par(text.align = "center") ))
    
    block_section(prop_section(footer_default = footer_default))
    ```
    

    All together:

    ---
    output: officedown::rdocx_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    library(officedown) # 0.3.0
    library(officer) # 0.6.2
    ```
    
    <!---BLOCK_TOC--->
    
    ```{r}
    footer_default <- block_list(fpar(run_word_field(field = "PAGE"),
                                      fp_p = fp_par(text.align = "center") ))
    
    block_section(prop_section(footer_default = footer_default))
    ```
    
    # Chapter 1
    # Chapter 2
    <!---BLOCK_LANDSCAPE_START--->
    # Chapter 3
    <!---BLOCK_LANDSCAPE_STOP{break_page: "nextPage"}--->
    # Chapter 4
    \newpage
    # Chapter 5
    \newpage
    # Chapter 6
    

    Relevant references:
    https://github.com/davidgohel/officedown/discussions/103
    https://github.com/davidgohel/officedown/discussions/138