隐藏/折叠 revealjs Quarto 中的代码按钮

huangapple go评论49阅读模式
英文:

Button to hide/fold code in revealjs Quarto

问题

I can help with the translation. Here's the content you provided:

我想在revealjs Quarto演示中使用code-fold选项,或类似的东西。当我们在代码块中使用这个选项时:

---
title: "Code-fold in revealjs"
format: revealjs
code-fold: true
---

## 图表

```{r}
#| code-fold: true
#| code-summary: "展开查看完整代码"
library(ggplot2)
dat <- data.frame(cond = rep(c("A", "B"), each=10),
                  xvar = 1:20 + rnorm(20,sd=3),
                  yvar = 1:20 + rnorm(20,sd=3))

ggplot(dat, aes(x=xvar, y=yvar)) +
  geom_point(shape=1) + 
  geom_smooth() 

输出:

隐藏/折叠 revealjs Quarto 中的代码按钮

我们可以看到没有代码折叠按钮。所以我想知道是否有类似revealjs的选项?

英文:

I would like to use a code-fold option in a revealjs Quarto presentation, or something similar. When we use this option in a code chunk like this:

---
title: "Code-fold in revealjs"
format: revealjs
code-fold: true
---

## Graph

```{r}
#| code-fold: true
#| code-summary: "expand for full code"
library(ggplot2)
dat <- data.frame(cond = rep(c("A", "B"), each=10),
                  xvar = 1:20 + rnorm(20,sd=3),
                  yvar = 1:20 + rnorm(20,sd=3))

ggplot(dat, aes(x=xvar, y=yvar)) +
  geom_point(shape=1) + 
  geom_smooth() 
```

Output:

隐藏/折叠 revealjs Quarto 中的代码按钮

We can see there is no code-fold button. So I was wondering if anyone knows if there is an option like that in revealjs?

答案1

得分: 1

更新

只需使用 echo: true,无需使用 Lua 过滤器。

Quarto revealjs 格式默认使用 echo: false 用于每个代码块。这样默认情况下将不会起作用代码折叠(因为在呈现的幻灯片中没有要折叠的代码块)。


标题: "在 revealjs 中折叠代码"
格式: revealjs

图表

#| echo: true
#| code-fold: true
#| code-summary: "展开查看完整代码"
library(ggplot2)
dat <- data.frame(cond = rep(c("A", "B"), each=10),
                  xvar = 1:20 + rnorm(20,sd=3),
                  yvar = 1:20 + rnorm(20,sd=3))

ggplot(dat, aes(x=xvar, y=yvar)) +
  geom_point(shape=1) + 
  geom_smooth() 

隐藏/折叠 revealjs Quarto 中的代码按钮


你可以使用以下 Lua 过滤器来添加代码折叠


标题: "在 revealjs 中折叠代码"
格式: revealjs
filters:

  • code-fold.lua

图表

#| echo: true
#| code-fold: true
#| code-summary: "展开查看完整代码"
library(ggplot2)
dat <- data.frame(cond = rep(c("A", "B"), each=10),
                  xvar = 1:20 + rnorm(20,sd=3),
                  yvar = 1:20 + rnorm(20,sd=3))

ggplot(dat, aes(x=xvar, y=yvar)) +
  geom_point(shape=1) + 
  geom_smooth() 

code-fold.lua

local str = pandoc.utils.stringify

local template_fold_p1 = [[
<details>
  <summary>
  %s
  </summary>
]]

local template_fold_p2 = [[
%s
</details>
]]

function Div(el)
  if el.attributes['code-fold'] then
    local code_summary = str(el.attributes['code-summary'])
    local fold = string.format(template_fold_p1, code_summary)
    local fold_rb1 = pandoc.RawBlock('html', fold)
    local fold_rb2 = pandoc.RawBlock('html', template_fold_p2)
    el.content:insert(1, fold_rb1)
    el.content:insert(fold_rb1)
    return el
  end
end
英文:

Update

Just use echo: true, No need to use that Lua filter.

Quarto revealjs format by default uses echo: false for every chunk. That's code-folding will not work by default (because there is no code block to fold in the rendered slide).

---
title: &quot;Code-fold in revealjs&quot;
format: revealjs
---

## Graph

```{r}
#| echo: true
#| code-fold: true
#| code-summary: &quot;expand for full code&quot;
library(ggplot2)
dat &lt;- data.frame(cond = rep(c(&quot;A&quot;, &quot;B&quot;), each=10),
                  xvar = 1:20 + rnorm(20,sd=3),
                  yvar = 1:20 + rnorm(20,sd=3))

ggplot(dat, aes(x=xvar, y=yvar)) +
  geom_point(shape=1) + 
  geom_smooth() 
```

<hr>

隐藏/折叠 revealjs Quarto 中的代码按钮

<hr>

You can use the following Lua filter to add code folding

---
title: &quot;Code-fold in revealjs&quot;
format: revealjs
filters: 
  - code-fold.lua
---

## Graph

```{r}
#| echo: true
#| code-fold: true
#| code-summary: &quot;expand for full code&quot;
library(ggplot2)
dat &lt;- data.frame(cond = rep(c(&quot;A&quot;, &quot;B&quot;), each=10),
                  xvar = 1:20 + rnorm(20,sd=3),
                  yvar = 1:20 + rnorm(20,sd=3))

ggplot(dat, aes(x=xvar, y=yvar)) +
  geom_point(shape=1) + 
  geom_smooth() 
```

code-fold.lua

local str = pandoc.utils.stringify

local template_fold_p1 = [[
&lt;details&gt;
  &lt;summary&gt;
  %s
  &lt;/summary&gt;
]]

local template_fold_p2 = [[
%s
&lt;/details&gt;
]]

function Div(el)
  if el.attributes[&#39;code-fold&#39;] then
    local code_summary = str(el.attributes[&#39;code-summary&#39;])
    local fold = string.format(template_fold_p1, code_summary)
    local fold_rb1 = pandoc.RawBlock(&#39;html&#39;, fold)
    local fold_rb2 = pandoc.RawBlock(&#39;html&#39;, template_fold_p2)
    el.content:insert(1, fold_rb1)
    el.content:insert(fold_rb1)
    return el
  end
end

huangapple
  • 本文由 发表于 2023年4月17日 15:46:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定