简体   繁体   English

在编织代码块中包装长字符串

[英]Wrapping long string in knitr code block

I'm gratuitously using knitr to write a formal PDF document. 我正在免费使用knitr编写正式的PDF文档。 I would like to use a code block instead a block quote because... it's funny. 我想使用代码块而不是块引号,因为...这很有趣。 However, the paragraph that I would like to put in the code block doesn't wrap to the box. 但是,我要放在代码块中的段落没有包装到方框中。 How can I do this? 我怎样才能做到这一点? I've tried the following: 我尝试了以下方法:

\documentclass[a4paper]{article}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{csquotes}
\begin{document}

<<setup, include=FALSE, results='hide', cache=FALSE>>=
opts_chunk$set(echo=FALSE, warning = FALSE, message = FALSE, cache = FALSE, error = FALSE)
@

Some text

<<tidy=TRUE, width=50, comment=NA>>==
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut     
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu fugiat nulla pariatur.     
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui 
officia deserunt mollit anim id est laborum."
@

\end{document}

...Which gives this: ...哪个给出了:

在此处输入图片说明

I tried this solution but I'd like to avoid numbering each row. 我尝试了这种解决方案,但我想避免为每行编号。 I've also tried pasting in "\\n" to do line breaks to no avail. 我也尝试粘贴“ \\ n”来进行换行,但无济于事。

based on the syntax of your code, you may want to directly use LaTex instead of RStudio ( knitr ) to create your output. 根据代码的语法,您可能需要直接使用LaTex而不是RStudio( knitr )创建输出。 In my RStudio, I got an error message and could not compile the PDF until the LaTex-specific commands such as \\begin{document} and \\end{document} were removed. 在我的RStudio中,我收到一条错误消息,并且在删除LaTex特定的命令(例如\\begin{document}\\end{document}之前,无法编译PDF。 I hope this is what you are looking for: 我希望这是您要寻找的:

---
title: "Untitled"
output: pdf_document
---

```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
options(width=80)
opts_chunk$set(comment = "", warning = FALSE, message = FALSE, echo = TRUE, tidy = TRUE, size="small")
```

Some text

"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut     
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu fugiat nulla pariatur.     
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui 
officia deserunt mollit anim id est laborum."

在此处输入图片说明

I noticed that you specified width=50 as chunk option for just one chunk. 我注意到您仅将width=50指定为一个块的块选项。 It might be beneficial if you set that option as a global chunk option, as in this post . 如果将此选项设置为全局块选项,则可能会有所帮助,如本文所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM