138

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
---
4
  • And then what packages/functions do you use? Or do you just click buttons in RStudio?
    – Spacedman
    Commented Aug 11, 2014 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
    Commented Aug 11, 2014 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
    Commented Aug 11, 2014 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
    Commented Aug 11, 2014 at 11:13

4 Answers 4

202

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.

3
  • Does that have any effect when knitting to html? Does it cause any error?
    – W7GVR
    Commented Oct 29, 2015 at 17:55
  • Since it is a question about creating a newpage. How would you create a new page in html output?
    – Marcin
    Commented Feb 16, 2016 at 13:57
  • 1
    @MarcinKosiński You can insert HTML tags directly: see edit for clarification.
    – tonytonov
    Commented Feb 16, 2016 at 14:16
40

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()`
21

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')
```
7
  • 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
    Commented May 2, 2018 at 9: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: <Anonymous> ... process_group.block -> call_block -> eval_lang -> eval -> eval Execution halted opts_knit$get works fine in the console though. ? Commented Sep 29, 2018 at 17:34
  • add echo = FALSE to the knitr opts to avoid to get the statement cat('\\pagebreak') in the output file.
    – Akronix
    Commented Dec 14, 2018 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? Commented Jan 18, 2019 at 17:55
  • 1
    ```{r page break, results='asis', eval=(opts_knit$get('rmarkdown.pandoc.to') == 'latex'), echo = FALSE} cat('\\pagebreak') ```
    – Akronix
    Commented Jan 20, 2019 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.