rknitr

Displaying <<..>>= in output


I want demonstrate a sample piece of R code WITH the knitr <<..>>= preamble in a LaTeX document. Here is an example of the output I desire:

It's got to be simple - but I'm missing something. I checked the documentation and scanned stack overflow - but without luck. Here is a MWE:

    \documentclass{article}
    \begin{document}
    <<mychunk, cache=TRUE, eval=FALSE, dpi=100>>=
    "hello world" 
    @
    \end{document}

Suggestions? I tried indenting the code in LaTex and wrapping in a verbatim block, but only got errors.


Solution

  • I just checked the manual of knitr. This is how the package author solved the problem:

    <<use-ext-chunk, echo=FALSE, comment=NA>>=
    cat('<<Q1, echo=TRUE, tidy=TRUE>>=','@',sep='\n')
    @
    

    which produces the output as shown on page 9 of the knitr manual

    Here is a minimal example:

    \documentclass[a4paper]{article}
    \begin{document}
    
    
    <<use-ext-chunk, echo=FALSE, comment=NA>>=
      cat('<<Q1, echo=TRUE, tidy=TRUE>>=','@',sep='\n')
    @
    
    \end{document}
    

    which produces the attached output.

    enter image description here