126
votes

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
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 Kosiński
Its nice to see the complete workflow - there's various ways of going from markdown to PDF. Without that, we're guessing. Good guess @tonytonovSpacedman
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 Kosiński

4 Answers

188
votes

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.

29
votes

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()`
13
votes

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')
```
0
votes

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