【问题标题】:RMarkdown - print() command results in out of page bound lines in kitted PDFRMarkdown - print() 命令导致成套 PDF 中的页面边界线越界
【发布时间】:2021-02-23 21:56:45
【问题描述】:

如果您将 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 标头,创建前导文件并在 YAML 标头中引用它们,尝试各种乳胶引擎将我的文档编织为 PDF,定义 PDF 文档的边距,手动拆分长 print() 调用以跨越几行在源文件中并遵守最大值。每行的字符数,但到目前为止都失败了。 HTML 输出似乎还不错。

非常感谢任何提示过去已经提供的答案,因为我确信我不是唯一遇到此类问题的人,但不幸的是我无法找到合适的解决我的问题。

【问题讨论】:

    标签: r r-markdown knitr


    【解决方案1】:

    一种可能的解决方案是使用“--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,然后使用 https://wkhtmltopdf.org/ 将 html 转换为 pdf。如果您使用的是 macOS,您可以使用 homebrew (brew install wkhtmltopdf) 安装 wkhtmltopdf。这种方法的优点是它可以维护任何特定于 html 的格式,即您可以使用主题

    【讨论】:

    • 谢谢贾里德!刚注意到你的帖子!我实施了建议的解决方案,正在等待针织输出。如果这行得通,我会欣喜若狂。如果不是出于某种原因,我将尝试您的巧妙技巧,即首先呈现 HTML 文档,然后将其转换为 PDF 文档。非常感谢!
    • 当时我实现了这两种解决方案,所以不知道这两种解决方案中的哪一种对我有用,但它就像一个魅力!不知道为什么我之前尝试在 YAML 标头中添加 preamble.tex 文件之前对我没有成功。再次,非常感谢 Jared :)
    • 不客气,但无需感谢我 - 转发并自己回答一些问题(这是最好的学习方式之一)
    • 作为R的初学者(主要是为了学术论文),恐怕很多时候,我的解决方案不是很有效或不直观,到目前为止我很难将一些问题形式化,但是随着经验的积累,我肯定会在不久的将来这样做,因为我对自己的技能越来越有信心!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 2021-06-26
    相关资源
    最近更新 更多