Inserting a page break within a code chunk in rmarkdown (converting to pdf)

后端 未结 2 1336
滥情空心
滥情空心 2021-02-05 08:03

I am using rmarkdown, pandoc and knitr to create a pdf including chunks of r code. Within a code chunk I have a for loop which prints a number of graphs and some statistical out

2条回答
  •  粉色の甜心
    2021-02-05 08:33

    See below a reduced and reproducible example. The answer and some general remarks:

    • To dynamically create new pages or sections in a markdown document use results='asis' in the chunk options.
    • You have to add a linebreak (\n) after \\pagebreak or else "ValueForV" will be pasted directly after "\linebreak", which results in an Undefined control sequence error.
    • Make sure that \newpage and \pagebreak are in a separate line by using linebreaks \n before.
    • Escape \newpage and \pagebreak (i.e., \\newpage, \\pagebreak).

      ---
      title: "test"
      output: pdf_document
      ---
      
      ```{r, echo=FALSE, results='asis'}
      for (i in 1:3) {
        print(ggplot2::qplot(i, i+1))
        cat("\n\n\\pagebreak\n")
        writeLines("ValueForV")
      }
      ```
      

提交回复
热议问题