RMarkdown 中的编号代码块 [英] Numbered Code Chunks in RMarkdown

查看:59
本文介绍了RMarkdown 中的编号代码块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为 RMarkdown 中的代码块提供一个选项,以便将 cell number 附加到 HTML 输出.就像 Jupyter 有单元格编号一样.

我看过一些行编号示例,这不是我想要的.

当我与某人通过电话讨论 RMarkdown HTML 文件时,使用手机号码很有帮助.我可以请他/她查看单元格 23.我有很多 R 代码,因此提供部分标题虽然可能,但很乏味.

解决方案

这是一个仅使用 CSS 的解决方案.它依赖于

---标题:块计数器"作者:罗曼·勒苏尔"输出:html_document---```{css,echo=FALSE}身体 {counter-reset: counter-rchunks;}div.主容器{padding-left: 5em;}前.r {计数器增量:计数器块;位置:相对;溢出:可见;}前.r::before {内容:'在['计数器(counter-rchunks)']:';显示:内联块;位置:绝对;左:-5em;颜色:RGB(48、63、159);}`````{r 汽车}总结(汽车)`````{r 头车}头(车)``

您可能需要将此解决方案适应您的 HTML 模板.
您还可以将这些 CSS 规则插入到 .css 文件中,并将其包含在您的 html_document 中.

Is there an option I can provide to code chunks in RMarkdown so that it will have a cell number attached to the HTML output. Much like Jupyter has cell numbers.

I've seen some example with line numbering which is not what I want.

Using cell numbers is helpful when I'm discussing an RMarkdown HTML file over the phone with someone. I can ask him/her to see cell 23. I have a lot of R code, so providing section titles, while possible, is tedious.

解决方案

Here's a solution using only CSS. It relies on CSS counters: each new R chunk increments the counter (named counter-rchunks).

You can knit the following minimal Rmd file and get this result:

---
title: "Counter for chunks"
author: "Romain Lesur"
output: html_document
---

```{css, echo=FALSE}
body {
  counter-reset: counter-rchunks;
}

div.main-container {
  padding-left: 5em;
}

pre.r {
  counter-increment: counter-rchunks;
  position: relative;
  overflow: visible;
}

pre.r::before {
  content: 'In [' counter(counter-rchunks) ']: ';
  display: inline-block;
  position: absolute;
  left: -5em;
  color: rgb(48, 63, 159);
}
```

```{r cars}
summary(cars)
```

```{r head-cars}
head(cars)
```

You may have to adapt this solution to your HTML template.
You also can insert these CSS rules to a .css file and includes it in your html_document.

这篇关于RMarkdown 中的编号代码块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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