Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to break lines in knitr output with the listings package
% see http://yihui.name/knitr/demo/listings/ for output
\documentclass{article}
% define a new environment to break lines for error messages
\lstnewenvironment{Serror}{\lstset{style=Routstyle,breaklines=true}}{}
<<setup, include=FALSE>>=
render_listings()
@
\begin{document}
This is an example of breaking lines in listings. We only show how to deal with the error messages; it is straightforward to generalize to other types of output.
<<my-error-hook>>=
# the error messages are written in the Serror environment defined in preamble
knit_hooks$set(error = function(x, options) {
sprintf('\\begin{Serror}\n%s\\end{Serror}\n', x)
})
@
<<error-chunk>>=
stop('I am a long long long long long long long long long long long long long long long long string')
<<session>>=
sessionInfo()
@
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.