Created on 2019-04-18.
Updated on 2019-04-19.
Before reading
atusy/rmd-line-num.Pandoc has an official support to number lines on fenced code by giving a class attribute numberLines (https://www.pandoc.org/MANUAL.html#extension-fenced_code_attributes). The document also introduces that lineAnchors class makes line numbers be clickable, and startFrom arugment supports specify starting number.
Note that class attributes require . before the class name.
becomes
This is enough for rmarkdown::pdf_document.
For html_document, we also need to specify highlight other than default in YAML front matter1, 2. See ?rmarkdown::html_document for available highlight options.
The above success infers a success in chunks of Rmarkdown.
For code chunks of Rmarkdown documents, numberLines class can be given by assigning class.source = "numberLines" as a chunk option3. You may also add lineAnchors demilited by space ("numberLines lineAnchors") or as another element of vector (c("numberLines", "lineAnchors")) 4. Unfortunately, I find no way to add startFrom="integer".
Again, you need to specify highlight for html_document.
Thus,
---
title: Line numbers with Rmarkdown documents
output: 
  html_document:
    highlight: pygment
---
```{r, class.source = "numberLines lineAnchors"}
x <- seq(10)
mean(x)
```becomes
## [1] 5.5Great again!!
Unfortunately, line numbering does not work on rmarkdown::word_document.
It does not work properly on rmarkdown::html_notebook and blogdown::html_page as well. I guess some tricks required in CSS or JS.
You may also want to number lines on outputs by class.output = "numberLines" with or without lineAnchors just like input. However, this changes background colors to gray.
If output format is html, css will help.
Before adding class.output = "numberLines", an output in html is
After adding it, the output in html becomes
When chunkout class is further added, the output in html becomes
You can see chunkout class is added to pre tag.
So, lets modify css with
and you’ll be happy, right?
Sorry I do not support pdf because \(LaTeX\) kills me.
Super easy by a following template and edit after <!-- Start your body -->.
Disable numbering by class.source = NULL, class.output = NULL.
Note autonumbering does not work on Pandoc’s fenced code 5.
---
output:
  html_document:
    highlight: pygment
  pdf_document: default
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
  class.source = "numberLines lineAnchors", 
  class.output = c("numberLines lineAnchors chunkout") 
)
# Add some arbitrary setup codes
```
```{css, echo = FALSE}
div.sourceCode pre.chunkout {
  background: white;
}
```
<!-- Start your body -->
**Numbered**
```{r}
x <- seq(10)
mean(x)
```
**Unnumbered**
```{r, class.source = NULL, class.output = NULL}
x <- seq(10)
mean(x)
```Numbered
Unnumbered
## [1] 5.5“Chunk numberLines hook]” on Rpubs figured out requirement of pygment (https://rpubs.com/Thell/numberLines).↩
@niszet0 mentioned that not only pygment but also highlight other than default are acceptable (https://niszet.hatenablog.com/entry/2019/04/18/194246 in Japanese).↩
See “Tex Results” section of “Chunk options and package options” by Yi Hui (https://yihui.name/knitr/options/#text-results)↩
@niszet mentioned me on Twitter https://twitter.com/niszet0/status/1118837863200591872↩
The document indicates a following YAML front matter should work, but doesn’t (https://www.pandoc.org/MANUAL.html#reader-options).↩