如何在 pdf rmarkdown 输出中将长注释包装在代码块中?

问题描述 投票:0回答:1

当我将 rmarkdown 文档输出为 pdf 时,很长的注释不会换行。例如:

x = 1 + 1 #This is a comment that will be written super duper long to see if it wraps around the document or if it trails off.

当我将文档编织成pdf时,结果是注释从页面上消失并且没有环绕。

我研究了这个问题并尝试了两件事:

  1. 下载formatR包,然后使用“tidy=TRUE, tidy.opts=list(width.cutoff=60)”作为{r}括号中的选项。我确保在文档顶部的代码块中使用库(formatR),如下所示:
library(formatR)

我还尝试在代码块中使用带有长注释的库(formatR)以及该代码块中的“tidy = TRUE,tidy.opts = list(width.cutoff = 60)”选项。

  1. 使用以下全局语句:
knitr::opts_chunk$set(tidy.opts = list(width.cutoff = 60), tidy = TRUE)

第一个解决方案没有像我想要的那样包装评论。第二种解决方案包含长代码,但不包含代码的注释。我怎样才能把长评论也包装起来?

r r-markdown rstudio
1个回答
0
投票

一种方法可能是将注释放在单独的行上,然后使用

formatR
就可以了:

---
title: "Long code comment wrapping"
output: pdf_document
---

```{r setup, include=FALSE}

library(formatR)

knitr::opts_chunk$set(tidy.opts = list(width.cutoff = 80), tidy = TRUE)

```


```{r}

x = 1 + 1 
# This is a comment that will be written super duper long to see if it wraps around the document or if it trails off.

```

这会导致:

© www.soinside.com 2019 - 2024. All rights reserved.