Search code examples
r-markdownrenderingpdflatex

Difference in PDFs when manually generated with Knit-Button vs rmarkdown::render() - e.g. section numbers not always included


I try to generate multiple reports (1 per group) with rmarkdown::render(), but it produces no section numbers.

I use the following file-structure for this in case you want to reproduce the sample (I tried to simplify each file and use only necessary code to reproduce the error):

*) groups.R: defines groups and subgroups in a nested list (all groups stored in a list and each group is a list itself)

groups <- list(
             list(c("subgroup1","subgroup2"),"maingroup1"),
             list(c("subgroup3","subgroup4"),"maingroup2")
          )

*) main.Rmd: template for the reports

---
output:
   pdf_document:
      number_sections: true
classoption: a4paper
geometry: left=2cm,right=1cm,top=1.5cm,bottom=1cm,includeheadfoot
fontfamily: helvet
fontsize: 11pt
lang: en
header-includes:
-   \usepackage{lastpage}
-   \usepackage{fancyhdr}
-   \pagestyle{fancy}
-   \fancyhf{}
-   \fancyhead[R]{\fontsize{9}{11} \selectfont \leftmark}
-   \fancyhead[L]{\fontsize{9}{11} \selectfont Special report xxx}
-   \fancyfoot[R]{\fontsize{9}{0} \selectfont Page \thepage\ of 
\pageref{LastPage}}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = 
FALSE,comment=NA,warning=FALSE,message=FALSE)
```

\thispagestyle{empty}   
\tableofcontents        

\newpage
\setcounter{page}{1}

# Introduction

Some text...

```{r results="asis"}
source("graphics.R")
```

*) graphics.R: generates graphics for each subgroup (sections/section numbers are produced with cat() for each subgroup)

load("actgroup.RData")
source("template_graphics.R")

for (g in 1:length(act.group[[1]][[1]])) {

  subgroup.name <- act.group[[1]][[1]][g]

  cat("\\clearpage")
  cat("\n# ",subgroup.name, "\n")

  template_graphics(cars)
  cat("\n\n")
  cat("\\clearpage")
  template_graphics(iris)
  cat("\n\n")
  cat("\\clearpage")
  template_graphics(airquality)
  cat("\n\n")
  cat("\\clearpage")

  cat("\n")

}

*) template_graphics.R: template for plotting

template_graphics <- function(data) {
  plot(data)
}

*) loop.R: used for generating all reports as PDF - 1 per group

setwd("YOUR DIRECTORY HERE")

library(rmarkdown)
source("groups.R")

for(i in 1:length(groups)) {
  act.group = list(groups[[i]])
  save(act.group,file="actgroup.RData")
  rmarkdown::render("main.Rmd",
                output_format=pdf_document(),
                output_file=paste0("Special Report ",act.group[[1]][[2]],".pdf"),
                output_dir="~/Reports")
}

The problem is, that the final documents do not show the section numbers. When I knit manually in main.Rmd (pressing knit-Button), the section numbers are printed.

Version rmarkdown::render enter image description here

Version knit-Button enter image description here

I thought that pressing the knit-Button also starts the rendering-process with rmarkdown::render()? So it's surprising that the reports are not identical?

In advance I installed tinytex::install_tinytex(). The used latex-packages in main.Rmd were automatically installed during the first time rendering.

I am not sure what the problem is. I use R 4.1.0 and RStudio 2022.02.2.

Thanks for your help!!


Solution

  • The behaviour of pdf_document() as the output-format in rmarkdown::render caused the missing section-numbers. In my YAML-header in main.Rmd I chose to keep the section numbers with number_sections: true. If this should also be rendered when using rmarkdown::render, it has to be an argument in the function: pdf_document(number_sections=TRUE)

    The code of loop.R produces now pdfs with section numbers:

    library(rmarkdown)
    source("groups.R")
    
    for(i in 1:length(groups)) {
        act.group = list(groups[[i]])
        save(act.group,file="actgroup.RData")
        rmarkdown::render("main.Rmd",
                   output_format=pdf_document(number_sections=TRUE),
                   output_file=paste0("Special Report ",act.group[[1]][[2]],".pdf"),
                   output_dir="~/Reports")
    }
    

    More information on pdf_document() can be found here: https://pkgs.rstudio.com/rmarkdown/reference/pdf_document.html

    Alternatively, fill in just a text-reference as output-format: output_format="pdf_document". When set like this, the options of the YAML-Header are not overwritten and the numbers are also included.