英文:
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
的选项?
英文:
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:
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()
你可以使用以下 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: "Code-fold in revealjs"
format: revealjs
---
## Graph
```{r}
#| echo: true
#| 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()
```
<hr>
<hr>
You can use the following Lua filter to add code folding
---
title: "Code-fold in revealjs"
format: revealjs
filters:
- code-fold.lua
---
## Graph
```{r}
#| echo: true
#| 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()
```
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论