awenborn
awenborn

Reputation: 417

R Markdown Grouping of Figures to Prevent Pagebreak

I'm having a problem with assigning LaTeX environments within an RMarkdown for-loop code-chunk.

In short, I've written an R Markdown document and a series of R-scripts to automatically generate PDF reports at the end of a long data analysis pipeline. The main section of the report can have a variable number of sections that I'm generating using a for-loop, with each section containing a \subsection heading, a datatable and plot generated by ggplot. Some of these sections will be very long (spanning several pages) and some will be very short (~1/4 of a page).

At the moment I'm just inserting a \pagebreak at the end of each for-loop iteration, but that leaves a lot of wasted space with the shorter sections, so I'm trying to "group" each section (i.e. the heading, table and chart) so that there can be several per page, but they will break to a new page if the whole section won't fit.

I've tried using a figure or minipage environment, but for some reason those commands are printed as literal text when the plot is included; these work as expected with the heading and data table, but aren't returned properly in the presence of the image.

I've also tried to create a LaTeX samepage environment around the whole subsection (although not sure this will behave correctly with multi-page sections?) and then it appears that the Markdown generated for the plot is not interpreted correctly somewhere along the way (Pandoc?) when it's within that environment and throws an error when compiling the TeX due to the raw Markdown ![]... image tag.

Finally, I've also tried implementing \pagebreak[x] and \nopagebreak[y] hints at various points in the subsection but can't seem get these to be produce the desired page breaking behaviour.

I've generated an MWE that reproduces my issues below.

I'd be really grateful for any suggestions on how to get around this, or better ways of approaching "grouping" of elements that are generated in a dynamic fashion like this?

---
title: "Untitled"
author: "I don't know what I'm doing"
date: "26/07/2020"
output:
  pdf_document: 
    latex_engine: xelatex
---

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

```{r cars, results='asis'}

for (i in 1:5){
  cat("\\begin{figure}")  
    cat(paste0("\\subsection{This is subsection ",i,"}"))
    cat("\\Huge Here's some bulk text that would represent a data table... kasvfkwsvg fiauwe grfiwgiu iudaldbau iausbd ouasbou asdbva asdbaisd i iuahihai hiuh iaiuhqijdblab ihlibljkb liuglugu h  uhi uhi uhqw iuh qoijhoijoijoi qwegru wqe grouw egq\\newline")
    plot(mtcars$wt,mtcars[,i])
  cat("\\end{figure}")  
}
```

Edit to add: interestingly these figure and minipage environments seems to work as expected when executing the same example in an .Rnw using knitr... so does that narrow it down to an issue with Pandoc? Again, any help much appreciated!

Upvotes: 2

Views: 729

Answers (1)

tarleb
tarleb

Reputation: 22619

What happens is that the raw TeX commands are not treated as TeX when going through Markdown. You can fix that by explicitly marking the relevant snippets as LaTeX:

for (i in 1:5){
  cat("`\\begin{figure}`{=latex}")  
    cat(paste0("\\subsection{This is subsection ",i,"}"))
    cat("\\Huge Here's some bulk text that would represent a data table... kasvfkwsvg fiauwe grfiwgiu iudaldbau iausbd ouasbou asdbva asdbaisd i iuahihai hiuh iaiuhqijdblab ihlibljkb liuglugu h  uhi uhi uhqw iuh qoijhoijoijoi qwegru wqe grouw egq\\newline")
    plot(mtcars$wt,mtcars[,i])
  cat("`\\end{figure}`{=latex}")  
}

See the generic raw attribute section in the pandoc manual for details.

Upvotes: 2

Related Questions