Skip to content

Instantly share code, notes, and snippets.

@yihui
Created March 26, 2012 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yihui/2209775 to your computer and use it in GitHub Desktop.
Save yihui/2209775 to your computer and use it in GitHub Desktop.
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