Lambo
Lambo

Reputation: 897

How to wrap a really long paragraph R output in R knitr

I'm trying to put a long paragraph into a pdf file using R Sweave/Knitr. This long paragraph is extracted from one of the excel columns and pasted together. You can consider the paragraph as a 1 by 1 object:

cat("loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong")

Currently, if I complie this long paragraph into a pdf:

 \documentclass{article}
 \begin{document}

 <<warning=FALSE, comment=NA, message=FALSE, echo=FALSE, tidy=TRUE,background="white">>=

 cat("looonno, oooooooooooooooooooooo, ooooooooooooooooooooooo, ooooooooooooooooooo, ooooonnnnnnnnnnnnnnnnnng")

 @

 \end{document}

the output cannot wrap the text itself and the result will always be like this: enter image description here

The output is outside the edge of the page.

I have tried using option(width=60), but it doesn't help in this situation unless there are multiple items in one object.

Hope some could come up with an idea that make the text wrap within the margin.

Upvotes: 4

Views: 464

Answers (1)

Silence Dogood
Silence Dogood

Reputation: 3597

With combination of strwrap, writeLines and appropriate width parameter you could do:

Output:

enter image description here

\documentclass{article}
\begin{document}

<<warning=FALSE, comment=NA, message=FALSE, echo=FALSE, tidy=TRUE,background="white">>=



x=paste0("looonno, oooooooooooooooooooooo, ooooooooooooooooooooooo, ooooooooooooooooooo, ooooonnnnnnnnnnnnnnnnnng, texttttttttttttttttttttttttt,
        goessssssssssssssss,onnnnnnnnnnn, foreverrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr, annnnnnnnnnnnnnnnnnnnnnnnnnd, everrrrrrrr")

cat("Before:","\n")

cat(x,"\n")

cat("After:","\n")

cat(writeLines(strwrap(x, width = 100)))


@

\end{document}

Upvotes: 2

Related Questions