plover
plover

Reputation: 53

adding numbers to code blocks in RMD output

I am trying to add line numbers to code blocks, but it's not working. I'm using the code attr.source='.numberLines' with Pandoc 2.11.4. but the output files omit the code numbers.

I've tried the options referenced in this question and also the documentation from bookdown, but the attr.source='.numberLines' is being ignored and it doesn't show up as an autofill option, like say echo= would.

What I want is something that looks like this (just the number part) enter image description here

Thanks! I've tried this in two different applications of R studio, and it's not working in either.

An example of the code I'm using:

---
title: "example"
author: "plover"
date:  '2022-12-29. Version: `r Sys.Date()`'
output:
  html_document:
    code_folding: show
    highlight: tango
    number_sections: false
    df_print: kable
    theme: flatly
    toc: yes
    toc_float: yes
    toc_depth: '3'
  word_document:
    toc: no
  pdf_document:
    toc: yes
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

```

Don't evaluate this code
```{r abc, eval = FALSE }
a <-1+1
b <-2+1
c = a + b
```

Don't evaluate this code, but show me the lines
```{r def, eval = FALSE, attr.source = '.numberLines' }
d <-3+1
e <-4+2
f = d + e
```

Evaluate this code, and show me the lines
```{r ghi, attr.source = '.numberLines' }
g <-3+1
h <-4+2
i = g + h
```

Upvotes: 0

Views: 163

Answers (1)

Shafee
Shafee

Reputation: 20087

Try this. (Actually did nothing but comment out the yaml option code_folding: show)

---
title: "example"
author: "plover"
date:  '2022-12-29. Version: `r Sys.Date()`'
output:
  pdf_document:
    toc: yes
  word_document:
    toc: no
  html_document:
    # code_folding: show
    highlight: tango
    number_sections: false
    df_print: kable
    theme: flatly
    toc: yes
    toc_float: yes
    toc_depth: '3'
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

```

Don't evaluate this code
```{r abc, eval = FALSE }
a <-1+1
b <-2+1
c = a + b
```

Don't evaluate this code, but show me the lines
```{r def, eval = FALSE, attr.source=".numberLines"}
d <-3+1
e <-4+2
f = d + e
```

Evaluate this code, and show me the lines
```{r ghi, eval=FALSE, attr.source=".numberLines"}
g <-3+1
h <-4+2
i = g + h
```

Upvotes: 1

Related Questions