How to add \newpage in Rmarkdown in a smart way?

RR Markdown

R Problem Overview


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
---

R Solutions


Solution 1 - R

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.

Solution 2 - R

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

Solution 3 - R

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

Solution 4 - R

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcin KosińskiView Question on Stackoverflow
Solution 1 - RtonytonovView Answer on Stackoverflow
Solution 2 - RBilly34View Answer on Stackoverflow
Solution 3 - RBravokingView Answer on Stackoverflow
Solution 4 - RbttomioView Answer on Stackoverflow