135

I wonder if one could simply use LaTeX \newpage command in R markdown v2 in a different way than this:

```{r, results='asis', echo=FALSE}
cat("\\newpage")
```

I produce pdf_output. If any1 has any idea please do not hesitate to comment :) ! Thanks

I create pdf like this:

---
title: " "
author: " "
date: "2014"
output: 
   pdf_document:
      includes:
         in_header: naglowek.tex
      highlight: pygments
      toc: true
      toc_depth: 3
      number_sections: true
      keep_tex: true
---
tonytonov
  • 25,060
  • 16
  • 82
  • 98
Marcin
  • 7,834
  • 8
  • 52
  • 99
  • And then what packages/functions do you use? Or do you just click buttons in RStudio? – Spacedman Aug 11 '14 at 10:29
  • What's the difference? My packages or functions has nothing to do with that I'd like to add newpage in some parts of code. – Marcin Aug 11 '14 at 10:52
  • Its nice to see the complete workflow - there's various ways of going from markdown to PDF. Without that, we're guessing. Good guess @tonytonov – Spacedman Aug 11 '14 at 11:11
  • I don't think so it was a guess. It was easy question I think you overestimated it. Btw thanks for chat. Have a nice day. – Marcin Aug 11 '14 at 11:13

4 Answers4

200

Simply \newpage or \pagebreak will work, e.g.

hello world
\newpage
```{r, echo=FALSE}
1+1
```
\pagebreak
```{r, echo=FALSE}
plot(1:10)
```

This solution assumes you are knitting PDF. For HTML, you can achieve a similar effect by adding a tag <P style="page-break-before: always">. Note that you likely won't see a page break in your browser (HTMLs don't have pages per se), but the printing layout will have it.

tonytonov
  • 25,060
  • 16
  • 82
  • 98
37

In the initialization chunk I define a function

pagebreak <- function() {
  if(knitr::is_latex_output())
    return("\\newpage")
  else
    return('<div style="page-break-before: always;" />')
}

In the markdown part where I want to insert a page break, I type

`r pagebreak()`
Billy34
  • 1,777
  • 11
  • 11
17

You can make the pagebreak conditional on knitting to PDF. This worked for me.

```{r, results='asis', eval=(opts_knit$get('rmarkdown.pandoc.to') == 'latex')}
cat('\\pagebreak')
```
Bravoking
  • 351
  • 3
  • 5
  • This works great, creates a page break in PDF but does not output anything in HTML (where page breaks don't make sense). Seems wort it to update the main answer to add this possibility. – Magnus May 02 '18 at 09:39
  • 1
    ```{r, results='asis', eval=(opts_knit$get('rmarkdown.pandoc.to') == 'latex')} cat('\\pagebreak') ``` results in `Error in eval(x, envir = envir) : object 'opts_knit' not found Calls: ... process_group.block -> call_block -> eval_lang -> eval -> eval Execution halted` `opts_knit$get` works fine in the console though. ? – keithpjolley Sep 29 '18 at 17:34
  • add `echo = FALSE` to the knitr opts to avoid to get the statement `cat('\\pagebreak')` in the output file. – Akronix Dec 14 '18 at 12:17
  • @Akronix I used `cat('\\pagebreak')` in chunk and added `echo = FALSE` to the knitr opts, but still get "## \newpage" in my pdf, any idea why? – Benjamin Telkamp Jan 18 '19 at 17:55
  • @BenjaminTelkamp You have to add also the results and eval arguments as stated in the answer. At least that works for me generating the pdf using `latex_engine: xelatex`. (Insert three backticks to the beginning and to the end of this code) ```{r page break, results='asis', eval=(opts_knit$get('rmarkdown.pandoc.to') == 'latex'), echo = FALSE} cat('\\pagebreak') ``` – Akronix Jan 20 '19 at 10:10
  • I re-copy it here for better clarity: – Akronix Jan 20 '19 at 10:40
  • 1
    `\`\`\`{r page break, results='asis', eval=(opts_knit$get('rmarkdown.pandoc.to') == 'latex'), echo = FALSE} cat('\\pagebreak') \`\`\` ` – Akronix Jan 20 '19 at 10:40
2

If you're having problems with \newpage or \pagebreak and floating figures/tables. You need to use \clearpage, as answered here.

bttomio
  • 2,206
  • 1
  • 6
  • 17