突出显示xaringan中的代码选择

时间:2018-08-25 11:35:45

标签: r-markdown xaringan

如何突出显示xaringan中的单个单词或一组代码,而不是整行?

在下面的示例中,我只想突出显示管道运算符%>% 而不是整行。

---
output:
  xaringan::moon_reader:
    css: [default]
    lib_dir: libs
    nature:
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
---

```{r setup, include=F}
library(magrittr)
```

Highlight Whole Line (not what I need)
```{r, eval=F}
iris %>% #<<
  summary()
```

Highlight Whole Line 2 (also not what I need)
```{r, eval=F}
{{ iris %>% }}
  summary()
```

Highlight Pipe only (What I would need, doesnt work)
```{r, eval=F}
iris {{ %>% }}
  summary()
```

Highlight Pipe only html-mark (doesnt work, as expected)
```{r, eval=F}
iris <mark>%>%</mark>
  summary()
```

这将导致enter image description here

感谢您的帮助。

1 个答案:

答案 0 :(得分:6)

我发现了一个解决方案:使用highlightSpans: true,然后在代码内部使用反引号。 即

---
output:
  xaringan::moon_reader:
    css: [default]
    lib_dir: libs
    nature:
      highlightStyle: github
      highlightLines: true
      highlightSpans: true
      countIncrementalSlides: false
---

```{r, eval=F}
iris `%>%`
  summary()
```

产生

enter image description here

该方法的唯一警告是它仅在R本身不评估代码的情况下才运行。 (eval=TRUE将返回错误)

其来源是:https://github.com/gnab/remark/wiki/Configuration