【问题标题】:R Markdown: Prevent page break within code resultsR Markdown:防止代码结果中的分页符
【发布时间】:2019-11-21 16:02:25
【问题描述】:

我有一个 Rmarkdown 文档,我想将其编入 PDF 文档。如何防止代码块结果中出现分页符。

结果不超过一页。如果将一个太长的页面结果移动到下一页而不是在第 1 页显示表格标题并在第 2 页显示表格的其余部分,这将是明智的。

对不起,如果这个问题的答案在某个地方。没找到。

编辑: 请求了一些代码。所以,我们开始吧。

---
title: "How to prevent page breaks in R Markdown code results"
author: "Georgery"
date: "10 January 2017"
output: pdf_document
---

# Create
# some
# headlines
# to
# fill
# the
# page
# a
# little
# and
# even
# a 
# little
# more
...and now create some code results
```{r, echo = FALSE}
data.frame(
    a = 1:20
    ,b = letters[1:20]
)
```

【问题讨论】:

  • 你的一些小代码会有所帮助
  • 抱歉,虽然我的意思已经很清楚了。
  • 没人帮忙吗?
  • 有没有找到答案?我也很好奇我们是否可以有类似 Latex 的“samepage”或 Word 的“keep lines together”段落选项。
  • @orh 你的评论已经有点老了,但也许我刚刚发布的答案有帮助。

标签: r r-markdown


【解决方案1】:

我不确定这是否可以自动完成,但可能包括 \newpage 或 \pagebreak 会强制输出到新页面。

【讨论】:

  • 是的,我也想过这个,但不是很好,因为每次更改时都必须对其进行编辑。
【解决方案2】:

在一段时间后回到这个问题的解决方案。

对我有用的只是使用knitr 包中的kable()。这样,表格就被渲染为一个对象。代码如下:

---
title: "How to prevent page breaks in R Markdown code results"
author: "Georgery"
date: "10 January 2017"
output: pdf_document
---

# Create
# some
# headlines
# to
# fill
# the
# page
# a
# little
# and
# even
# a 
# little
# more
...and now create some code results

```{r, echo = FALSE, warning = FALSE, message = FALSE}
library(knitr) # needed to make the table a separate object on only one page
library(kableExtra) # not needed but makes the table nicer
library(tidyverse) # not needed at all, but I like the pipe (%>%)

data.frame(
    a = 1:20
    , b = letters[1:20]) %>%
    kable("latex", booktabs = TRUE) %>% # This already puts it on a separate page
    kable_styling(latex_options = c("striped"))
```

【讨论】:

    猜你喜欢
    • 2020-03-28
    • 2014-11-30
    • 2020-11-16
    • 2016-01-22
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多