RMarkdown - print() 命令导致套件 PDF 中的页面边界线

RMarkdown - print() command results in out of page bound lines in kitted PDF

如果将 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 headers,创建序言文件并在 YAML header 中引用它们,尝试各种乳胶引擎将我的文档编成 PDF,定义 PDF 文档的边距,手动拆分long 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.

编辑

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