-1

I want to include R code in my LaTeX file and as I want to improve readability I really liked the way knitr package highlights the syntax of code with different colors. But what I would like to avoid is compiling the code and showing the output as well because my code is pretty long and showing the output would elongate it even more. So I would like to cite only the raw code without neccesity to color everything on my own. Is there any way to do this?

jakes
  • 1,964
  • 3
  • 18
  • 50

1 Answers1

1

I think you can just specify eval=FALSE in the chunk options. You could even include

 library(knitr)
 opts_chunk$set(eval=FALSE)

in a hidden chunk at the top of the file.

This .Rnw file:

\documentclass{article}
\begin{document}
\thispagestyle{empty}

<<opts,echo=FALSE>>=
library(knitr)
opts_chunk$set(eval=FALSE)
@

Here's some R code ...
<<ex1>>=
set.seed(101)
for (i in 1:1000) {
  x <- matrix(rnorm(1e4),100)
  print(eigen(x))
}
@

Compiles to this:

enter image description here

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • could you provide some example,please? – jakes Aug 19 '17 at 17:20
  • 1
    Another (potentially simpler) option is to just write in markdown using pandoc for the highlighting via "three backticks and r" instead an evaluated knitr. I tend to do that quite a bit for different programming languages. – Dirk Eddelbuettel Aug 19 '17 at 18:46