我正在尝试使用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}
输出位于页面边缘之外。
我尝试过使用option(width=60)
,但除非一个对象中有多个项目,否则在这种情况下无效。
希望有些人可以提出一个让文字在保证金内包装的想法。
答案 0 :(得分:2)
结合使用strwrap
,writeLines
和适当的width
参数,您可以这样做:
<强>输出:强>
\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}