0

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
```
shafee
  • 15,566
  • 3
  • 19
  • 47
plover
  • 47
  • 1
  • 6
  • Have you tried [this answer](https://stackoverflow.com/a/57883514/10858321) to the question you have linked in your question? – shafee Dec 29 '22 at 17:02
  • Sort of: I don't need to prevent it from starting again at 1, I'm just trying to get the numbers to show at all. I'm newish to R, and It's not clear to me how to implement or modify to my use the code from tarleb using `pandoc_args: ['--lua-filter=number-lines.lua']` . Specifically, given a markdown file with the code I provided in the YAML header part, where to put or use tarleb's answer. When I add the `pandoc_args:` line to the YAML the RMD won't knit at all. – plover Dec 29 '22 at 17:40
  • 1
    I have just ran the above code with the `code_folding` option commented and got the line numbers. – shafee Dec 29 '22 at 17:43
  • Can you explain exactly what you did (I'm newish to R). Thanks – plover Dec 29 '22 at 17:44

1 Answers1

1

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
```
shafee
  • 15,566
  • 3
  • 19
  • 47