Reputation: 838
I'm writing a tutorial using knitr and I want to show some of the warnings and errors that students might encounter. While I'm capable of nicely displaying the code chunks within the box using the tidy=TRUE
option, I don't understand how to handle the display of warnings and errors. For example, if I have the following code:
\documentclass{article}
\begin{document}
<<setupOp, include=FALSE>>=
opts_chunk$set(tidy=TRUE,
tidy.opts=list(blank=FALSE, width.cutoff=20))
@
<<ErrorTest>>=
warning(paste("A suuuuuuuuuuuuuuuuuper", "loooooooooooong waaaaaaaaaaaaarning"))
@
\end{document}
The warning code line is nicely displayed within the box, but the warning itself stretch beyond the box. I have a feeling that it has to do with the fact that the warning message is one very long string, but I don't know how to tell knitr to keep the warning inside the box. I've looked at the documentation on knitr chunk options and this formatR info, but I can't find the solution.
Thanks!
Upvotes: 2
Views: 373
Reputation: 94317
LaTeX is trying to generate a left and right justified block of text here. That means word-wrapping and hyphenating to get a nice straight right edge. Your warning has long words in it, LaTeX doesn't hyphenate typewriter text, so it overfills the box allocated for it and prints an overfull warning to the TeX log file.
Even if it could hyphenate the text, it might struggle finding a good place to hyphenate an odd word. For example, you should never break "buttoned" across lines as "but-toned". TeX has a complicated algorithm for this.
A solution may be to set \raggedright for your R blocks:
{
\raggedright
<<setupOp, include=FALSE>>=
opts_chunk$set(tidy=TRUE,
tidy.opts=list(blank=FALSE, width.cutoff=20))
@
<<ErrorTest>>=
warning(paste("A suuuuuuuuuuuuuuuuuper", "loooooooooooong waaaaaaaaaaaaarning"))
@
}
Like this, TeX should start a new line whenever a word would go outside the box. Enclose in a curly-bracket pair so normal text is unaffected. I don't know what else this might affect within the code block, so caveat emptor.
Upvotes: 4