Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying <<..>>= in output

Tags:

r

knitr

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:

snip-of-code

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.

like image 236
John Leonard Avatar asked Nov 12 '12 13:11

John Leonard


1 Answers

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

like image 115
Christoph_J Avatar answered Sep 23 '22 02:09

Christoph_J