在rmarkdown的代码块中插入分页符(转换为pdf) [英] Inserting a page break within a code chunk in rmarkdown (converting to pdf)

查看:104
本文介绍了在rmarkdown的代码块中插入分页符(转换为pdf)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rmarkdown,pandoc和knitr创建一个包含r代码块的pdf.在代码块中,我有一个for循环,该循环打印许多图形和一些统计输出.

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

我想在循环中插入一个分页符(出现在pdf输出中).在打印每个图形之后,将发生此分页符,以确保将每个图形打印在一页上,并将统计输出打印在下一页上.

I would like to insert a page break into the loop (to appear in the pdf output). This page break would occur after each graph is printed, to ensure each graph is printed on one page and the statistical output on the next.

我一直找不到在我的r代码块中包含分页符的方法.我尝试过cat("\\newpage")cat("\\pagebreak"),希望能被pandoc识别,但无济于事(它只是在最终的pdf文件中逐字印刷).

I have been unable to find a way of including a page break in my r code chunk. I have tried cat("\\newpage") and cat("\\pagebreak") in the hopes it would be recognized by pandoc but to no avail (it is just printed verbatim in the final pdf).

建议表示赞赏.这是我到目前为止的代码:

Suggestions appreciated. Here is the code I have so far:

```{r, echo =FALSE, message=FALSE, warning=FALSE, comment=NA, results='asis'}
library("markdown") 
library("rmarkdown") 
library("knitr")
library("ggplot2")
for (v in Values){

# read in file
testR <- read.csv(file.path, header=T)

print(ggplot(testR, aes(x=Time, y=Value, color=Batch)) + geom_point(size = 3) +
xlab ("Timepoint") +
ylab (v) +
scale_x_continuous(breaks=seq(0, 60, by=6)) +
ggtitle(paste("Scatterplot of Batches for ", v, sep="")))
ggsave(paste(timestamp, "__", 
       "Scatterplot of Batches for ", v, ".jpeg", sep = "")) 

cat("\\pagebreak")
writeLines(v)
writeLines("\n")
writeLines("\n Test for homogenity of slopes \n")
av1 <- aov(Value~Time*Batch, data=testR)
print(summary(av1))
}
```

推荐答案

请参见下面的简化示例和可复制示例.答案和一些一般性的评论:

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

  • 要在降价促销文档中动态创建新页面或新部分,请在块选项中使用results='asis'.
  • 您必须在\\pagebreak之后添加换行符(\n),否则将直接在"\linebreak"之后粘贴"ValueForV",这将导致Undefined control sequence错误.
  • 通过使用换行符\n来确保\newpage\pagebreak在单独的行中.
  • 转义\newpage\pagebreak(即\\newpage\\pagebreak).

  • 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")
}
```

这篇关于在rmarkdown的代码块中插入分页符(转换为pdf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆