5

I am using Quarto to produce some revealjs slides using RStudio and have been using the code-line-numbers option to selectively highlight lines of code. For instance:

```{r, echo = TRUE}
#| code-line-numbers: "1|3"
x <- 1
y <- 2
x + y
x * y
```

Allows me to highlight the first and third lines of code in the presentation.

I would like to additionally be able to highlight certain lines in the output. For instance, if I wanted to only highlight the result of x+y but not x*y, is there a way of doing so? I wondered if there was an option for output-line-number or similar that might have the desired effect, but couldn't find anything like this.

Any pointers appreciated!

shafee
  • 15,566
  • 3
  • 19
  • 47
user2728808
  • 560
  • 3
  • 18

1 Answers1

5

A possible way to do this could be based on using pandoc Lua filter.

Here I have created a chunk-option output-line-numbers to specify which output lines to highlight (works just as code-line-numbers chunk option), but please note that you must have to use class-output: highlight to get this line specific highlighting on chunk output.

---
title: "Output Line highlight"
format: revealjs
filters:
  - output-line-highlight.lua
---

```{r, echo = TRUE}
#| code-line-numbers: "1"
#| class-output: highlight
#| output-line-numbers: "2"

for (i in 1:3) {
  print("This is some random line")
}
```

output-line-highlight.lua

function highlight(line_number)
  local highlighter = {
    CodeBlock = function(block)
      if block.classes:includes('highlight') then
        block.classes:insert('has-line-highlights')
        block.attributes["code-line-numbers"] = line_number
        return block
      end
  end
  }
  return highlighter
end


function Div(el)
  if FORMAT == 'revealjs' then
    if el.classes:includes('cell') then
      line_number = tostring(el.attributes["output-line-numbers"])
      return el:walk(highlight(line_number))
    end
  end
end


output line highlighting for revealjs


shafee
  • 15,566
  • 3
  • 19
  • 47