如何在 rmarkdown HTML 和 PDF 中显示代码块的行号 [英] How to display line numbers for code chunks in rmarkdown HTML and PDF

查看:57
本文介绍了如何在 rmarkdown HTML 和 PDF 中显示代码块的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 rmarkdown 显示代码块的行号?

how do i display the line numbers of my code chunk with rmarkdown?

```{r}
   x <- 1:10
   y <- x^2
   plot(x,y)
```

我希望回声类似于

 1  x <- 1:10
 2  y <- x^2
 3  plot(x,y)

最好是在 Github 上...
很高兴得到任何帮助

Preferably like it is on Github...
Would be glad for any help

推荐答案

您可以生成两个代码块:一个用于演示,另一个用于执行.

You can produce two code blocks: one for the presentation and another, hidden, for execution.

---
output:
  pdf_document:
     highlight: haddock
---

```{#numCode .R .numberLines}
   x <- 1:10
   y <- x^2
   plot(x,y)
```

```{r results='asis', echo=FALSE}
   x <- 1:10
   y <- x^2
   plot(x,y)
```

注意:如果用 html_document 替换 pdf_document,则必须提供元数据highlight".

Note: If you replace pdf_document with html_document, you must provide the metadata "highlight".

这篇关于如何在 rmarkdown HTML 和 PDF 中显示代码块的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆