title: How to wrap text in R source with tidy and knitr
body: <p>I'm working with knitr lately and while most aspects of that have gone quite smoothly, there's one formatting issue with including R code in the finished document that I haven't figured out. I often need to create relatively long text strings in my R chunks, e.g. captions for <code>xtable()</code> functions. While tidy generally does a great job at wrapping R code and keeping it in the shaded boxes in LaTeX, it doesn't know what to do with text stings, so it doesn't wrap them, and they flow off the right side of the page.</p>
  
  <p>I would be most happy with a solution that has tidy doing all the work. However, I'd also be satisfied with a solution that I can apply manually to long strings in R chunks in my Rnw source. I just don't want to have to edit the tex file created by KnitR.</p>
  
  <p>Below is a minimal working example.</p>
  
  <pre><code>\documentclass[12pt, english, oneside]{amsart}
  
  \begin{document}
  
  &lt;&lt;setup, include=FALSE, cache=FALSE, tidy=TRUE&gt;&gt;=
  options(tidy=TRUE, width=50)
  @
  
  &lt;&lt;&gt;&gt;=
  x &lt;- c("This","will","wrap","nicely","because","tidy","knows","how","to","deal","with","it.","So","nice","how","it","stays","in","the","box.")
  longstr &lt;- "This string will flow off the right side of the page, because tidy doesn't know how to wrap it."
  @
  
  \end{document}
  </code></pre>