Line numbers for code blocks

I'd like to have line numbers for code blocks in my R Markdown documents. When I follow Yihui Xie's cookbook, I think I should end up with this code:

---
title: "Untitled"
author: "Unauthored"
date: "0/20/2020"
output:
  html_document:
    highlight: tango
---

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

## Line numbers

```{r, attr.source='.numberLines'}
if (TRUE) {
  x <- 1:10
  x + 1
}
```

But it gives me no line numbers:

Is it me?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

When running this example, I correctly get the numbered lines

See how the code block is indented like yours but with numbers before. It is possible that this is due to your theme note correctly appearing on your computer. I think that because if you deactivate numbered lines you got this

With the code block not indented.

Try to highlight with your mouse what is before your code block

Thank you, good to know it is me.

I did the highlighting trick before, which revealed that the numbers are actually missing. I also tried with three different browsers.

Is there anything else I could try?

What is your rmarkdown version and pandoc version ?

I was running rmarkdown 2.1 and pandoc 2.9.1.1. Just updated to rmarkdown 2.3, but no luck.

I tried with the pandoc version shipped with RStudio first, 2.7.3, and it's working.
Tried then with last pandoc 2.10.1 and I can reproduce your issue. I will look into it.

Thanks for diving into this!

Adding rmarkdown::find_pandoc(version = '2.7.3') to the setup chunk did the trick.

Shall I mark your response as the solution, or shall I wait for you to look into the problem with more recent pandoc versions?

Let's keep it open until the issue is resolved

I'll post back when it is. Thanks for the report !

1 Like

Also experiencing the same issue. I tried adding rmarkdown::find_pandoc(version = '2.7.3') to the set up chunk but that did not work for me. I get the following:

I am running the latest version of R and R Studio. Was there ever a solution to this one?

I can't reproduce - the example in the first post is now working ok.

  1. Open a new Rmd file
  2. Copy paste the code in the first example
  3. Click the knit button

Please check your Rmd file for problematic syntax. The formatting error you encounter is different than the original one.

If you still have an issue, please open a new post or GH issue with a new reproducible example.

Thank you!