RMarkdown - print() 命令导致套件 PDF 中的页面绑定(bind)线超出页面范围

标签 r r-markdown knitr

如果将 RMarkdown 文档编织到 PDF 中,是否有任何方法可以避免超出页面边缘的线条?

插图:

# RMarkdown file
    title: "xyz"
    author: "John Doe"
    date: "23 2 2021"
    output:
      pdf_document: 
        latex_engine: xelatex
          toc: true
          toc_depth: 5
     html_document: default
     mainfont: Times New Roman
     fontsize: 12

text
more text
```{r}
  print("This is a very long line illustrating my question that drives me nuts. It should include more text that is supposed line-break if it goes beyond specified margins.)

  a <- 10
  b <- sqrt(2)
 
  print(paste("This is also a use case that I am looking at right now. I want to print results in the PDF documents of a calcualtion and put it in context.", a, "divided by square root of 2 results in", (a/b))

 ```

如果文本超出范围,有什么方法可以强制换行吗?我尝试在 Markdown 文件中编辑 YAML header ,创建序言文件并在 YAML header 中引用它们,尝试各种 latex 引擎将我的文档编织为 PDF,定义 PDF 文档的边距,手动分割长 print() 调用跨越源文件中的多行并遵守最大行数。每行的字符数,但到目前为止都失败了。 HTML 输出似乎没问题。

高度赞赏任何对过去已经提供的答案的指示,因为我确信我不是唯一经历过此类问题的人,但不幸的是我无法找到合适的答案解决我的问题。

最佳答案

一个潜在的解决方案是使用“--listings”(每个 https://bookdown.org/yihui/rmarkdown-cookbook/text-width.html )更改宽度,例如

在您要编织的目录中创建一个名为“preamble.tex”的文件,并包含以下行:

\lstset{
  breaklines=true
}

然后更改 YAML 并编织:

> --- title: "xyz" author: "John Doe" date: "23 2 2021" output:
>     pdf_document:
>         pandoc_args: --listings
>         toc: true
>         toc_depth: 5
>         includes:
>             in_header: preamble.tex
>     html_document: default
>     mainfont: Times New Roman
>     fontsize: 12
> ---
> 
> ```{r setup, include=FALSE}
> knitr::opts_chunk$set(echo = TRUE)
> options(width = 80)
> ```
> 
> text
> more text
> ```{r}
> print("This is a very long line illustrating my
> question that drives me nuts. It should include more text that is
> supposed line-break if it goes beyond specified margins.")
> 
> a <- 10 b <- sqrt(2)   paste("This is also a use case that I am
> looking at right now. I want to print results in the PDF documents of
> a calcualtion and put it in context.", a, "divided by square root of 2
> results in", a/b, sep = " ")
> ```
> 
> ## R Markdown
> 
> This is an R Markdown document. Markdown is a simple formatting syntax
> for authoring HTML, PDF, and MS Word documents. For more details on
> using R Markdown see <http://rmarkdown.rstudio.com>.
> 
> When you click the **Knit** button a document will be generated that
> includes both content as well as the output of any embedded R code
> chunks within the document. You can embed an R code chunk like this:
> 
> ```{r cars}
> summary(cars)
> ```
> 
> ## Including Plots
> 
> You can also embed plots, for example:
> 
> ```{r pressure, echo=FALSE}
> plot(pressure)
> ```
> 
> Note that the `echo = FALSE` parameter was added to the code chunk to
> prevent printing of the R code that generated the plot.

example_1.png

编辑

另一个潜在的解决方案(我通常做的)是使用 {r setup} block 中的 options(width = 80) 将 RMarkdown 输出为 html,然后使用 https://wkhtmltopdf.org/ 将 html 转换为 pdf。如果您使用的是 macOS,则可以使用自制软件安装 wkhtmltopdf (brew install wkhtmltopdf)。这种方法的优点是它保留了任何特定于 html 的格式,即您可以使用主题

关于RMarkdown - print() 命令导致套件 PDF 中的页面绑定(bind)线超出页面范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66341686/

相关文章:

r - 什么是模型矩阵/设计矩阵

r - 在 R 中对重复情况使用权重(特别是二元响应的 gam)

r - 在不切断图形或丢失数据的情况下更改 ggplot 中的 y 轴限制

jupyter-notebook - 如何将 Rmd 文档转换为 jupyter 笔记本

r - Rmarkdown2/中的 PDF 渲染

r - 以 PDF 和 PNG 格式保存图表,但在最终文档中使用 PDF 文件

r - 在 knitr 中打印动态大小的绘图列表

R tm In mclapply(内容(x),FUN,...): all scheduled cores encountered errors in user code

r - Facet_Wrap 标题与 ggplotly 中的 y 轴重叠?

r - 使用 knitr/rmarkdown 以多种自然语言生成输出(来自 data.frame)