简体   繁体   中英

Wrapping long string in knitr code block

I'm gratuitously using knitr to write a formal PDF document. 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.

based on the syntax of your code, you may want to directly use LaTex instead of RStudio ( knitr ) to create your output. 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. 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. It might be beneficial if you set that option as a global chunk option, as in this post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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