Search code examples
rr-markdownbookdown

Strange stretching of chapter heading and text in bookdown


Say I have the following bookdown book – a single file called index.Rmd:

--- 
title: "Book"
author: "User"
date: ""
site: bookdown::bookdown_site
documentclass: book
output:
  bookdown::pdf_book
---

# Introduction

## Text
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

## Figure

```{r echo = FALSE, fig.height=3}
plot(0, 0)
```

The first page of the first chapter looks like this:

enter image description here

Now, say I have the exactly the same setup, but in the code chunk fig.height = 10 instead of fig.height = 3. The same page looks like this,

enter image description here

the following page is blank, and the figure appears on the page after that. I understand that the figure needs to be bumped to a new page because it is large, but why the bizarre stretching followed by a blank page and how can avoid it without resizing the figure? (I don't want to resize the figures as they are just fine when rendered in HTML and this book will be both HTML and PDF.)


Solution

  • The problem is that you are not using "floating figures". However, this is necessary for producing PDFs with fixed page breaks, c.f. the bookdown documentation. The solution is simple: add fig.cap = "..."to the chunk:

    --- 
    title: "Book"
    author: "User"
    date: ""
    site: bookdown::bookdown_site
    documentclass: book
    output:
      bookdown::pdf_book
    ---
    
    # Introduction
    
    ## Text
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
    incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
    exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
    irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
    pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
    officia deserunt mollit anim id est laborum.
    
    ## Figure
    
    ```{r echo = FALSE, fig.height=10, fig.cap = "Some plot"}
    plot(0, 0)
    ```
    

    enter image description here

    BTW, the 10 inches height is a bit to high for the page. From the LaTeX log:

    LaTeX Warning: Float too large for page by 19.92784pt on input line 116.

    So a slight reduction of the figure height would be appropriate.