如何在R knitr

时间:2017-06-29 09:22:50

标签: r latex knitr sweave

我正在尝试使用R Sweave / Knitr将长段放入pdf文件中。这个长段从一个excel列中提取并粘贴在一起。您可以将段落视为1对1的对象:

cat("loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong")

目前,如果我将这段长篇文章编成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}

输出无法包装文本本身,结果将始终如下: enter image description here

输出位于页面边缘之外。

我尝试过使用option(width=60),但除非一个对象中有多个项目,否则在这种情况下无效。

希望有些人可以提出一个让文字在保证金内包装的想法。

1 个答案:

答案 0 :(得分:2)

结合使用strwrapwriteLines和适当的width参数,您可以这样做:

<强>输出:

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}