I wonder if there is any function to put line numbers
with knitr
in .Rnw
. I found this discussion and some documents (now removed from the web) but could not find the way to put line numbers.

- 19,824
- 17
- 99
- 186

- 22,666
- 37
- 165
- 309
-
To clarify, you want line numbers in the pdf output? Or in the .Rnw file? – CephBirk Dec 21 '14 at 21:41
-
Yes, I need line numbers in pdf output. – MYaseen208 Dec 21 '14 at 21:42
-
And you want them for every line of the document or just line numbers in the chunk output? – CephBirk Dec 21 '14 at 21:44
-
If you want line numbers for everything and are using Latex, then take a look at this solution: http://tex.stackexchange.com/a/18776 – Richie Cotton Dec 24 '14 at 05:53
-
How about the solution here? http://stackoverflow.com/a/25299194/134830 – Richie Cotton Dec 24 '14 at 07:57
-
Thanks @RichieCotton for your efforts. Same problem has been mentioned in the link without any solution. – MYaseen208 Dec 24 '14 at 08:18
3 Answers
This solution uses the LaTeX listings package to create line numbers. I can only get them to work by accumulating across all code chunks, but I imagine there is a similar solution that will enumerate lines only within each chunk. Here's the .Rnw source:
\documentclass{article}
\usepackage{listings}
\begin{document}
<<setup, echo=FALSE>>=
knit_hooks$set(source = function(x, options) {
paste("\\begin{lstlisting}[numbers=left, firstnumber=last]\n", x,
"\\end{lstlisting}\n", sep = "")
})
@
<<a, results='hold'>>=
1:2
3:4
5:6
@
<<b>>=
"test1"
"test2"
"test3"
@
\end{document}
The key parts of this are in the source hook, which is basically copied from here. The firstnumber=last
tells listings to accumulate line numbers across listings. Without it, all lines are numbered 1 because knitr is putting each code line in its own listing.
And here's the result:
If you want each code block to start numbering from 1, add a hook to reset the counter:
knit_hooks$set(reset = function(before, options, envir){
if(before){
return("\\setcounter{lstnumber}{1}")
}
})
and then use reset=TRUE
to activate the hook in each chunk you want:
<<a, results='hold', reset=TRUE>>=
1:2
3:4
@

- 43,637
- 12
- 109
- 140
-
(+1): This is great @Thomas. Would be nice if line numbers change within each R Chunk. – MYaseen208 Dec 24 '14 at 08:21
-
This works fine. But it is not getting the code (font) color as with original 'knitrout'. Any solution for that. – MYaseen208 Dec 24 '14 at 11:05
-
@MYaseen208 You should be able to add `language=R` to the listings options (i.e., where it says `numbers=left, firstnumber=last`). You my have to go through the [listings manual](http://texdoc.net/texmf-dist/doc/latex/listings/listings.pdf) to understand how to gain fine control over the highlighting. – Thomas Dec 24 '14 at 11:08
-
Any chance of customizing the rendering of the line numbers (i.e. lower font size)? Thanks. – Ervan Mar 09 '17 at 13:21
When using knitr with Lyx or Latex, I've found it helpful to add the lineno package to the document pre-amble and then to enclose the chunk with the \internallinenumbers \resetlinenumber[13]
.
Here's a minimal example:
\usepackage{lineno}
then in the body text, add the following before the code chunk:
{\internallinenumbers \resetlinenumber[13]
and then this after the code chunk:
}
With LyX (what I use for rapid LaTeX generation), I simply go to the document menu, then Settings->LaTeX Preamble and I add \usepackage{lineno}
, click Apply, OK, and then Close. Then in the main document before my code chunk, I insert LaTeX source by clicking the "TEX" button menu button or by pressing "Ctrl+L" on the keyboard. Then I paste in {\internallinenumbers \resetlinenumber[13]
. Finally, I place the cursor immediately after the code chunk and do the same thing. only I close the line numbering with a curly brace: }
.
Here is a minimal example, when the code is in place is pasted below:
\documentclass[english]{article}
\usepackage{lineno}
\begin{document}
First line in main document before code chunk.
{\internallinenumbers \resetlinenumber[13]
<<CodeBlock1, highlight=TRUE, eval=FALSE, size="small">>=
x<-rnorm(10)
mean(x)
@
}
\end{document}

- 1,384
- 2
- 10
- 28
For use in HTML, you can set the chunk option class.source
to pass custom css to the output:
```{r class.source = c("numCode", "R", "numberLines")}
# your code
```

- 1,071
- 2
- 23
- 42