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

后端 未结 3 1707
终归单人心
终归单人心 2020-12-02 06:24

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         


        
相关标签:
3条回答
  • 2020-12-02 06:39

    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.

    0 讨论(0)
  • 2020-12-02 06:46

    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()`
    
    0 讨论(0)
  • 2020-12-02 06:49

    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 讨论(0)
提交回复
热议问题