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

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

Button to hide/fold code in revealjs Quarto

问题

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

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

  1. ---
  2. title: "Code-fold in revealjs"
  3. format: revealjs
  4. code-fold: true
  5. ---
  6. ## 图表
  7. ```{r}
  8. #| code-fold: true
  9. #| code-summary: "展开查看完整代码"
  10. library(ggplot2)
  11. dat <- data.frame(cond = rep(c("A", "B"), each=10),
  12. xvar = 1:20 + rnorm(20,sd=3),
  13. yvar = 1:20 + rnorm(20,sd=3))
  14. ggplot(dat, aes(x=xvar, y=yvar)) +
  15. geom_point(shape=1) +
  16. 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:

  1. ---
  2. title: "Code-fold in revealjs"
  3. format: revealjs
  4. code-fold: true
  5. ---
  6. ## Graph
  7. ```{r}
  8. #| code-fold: true
  9. #| code-summary: "expand for full code"
  10. library(ggplot2)
  11. dat <- data.frame(cond = rep(c("A", "B"), each=10),
  12. xvar = 1:20 + rnorm(20,sd=3),
  13. yvar = 1:20 + rnorm(20,sd=3))
  14. ggplot(dat, aes(x=xvar, y=yvar)) +
  15. geom_point(shape=1) +
  16. geom_smooth()
  17. ```

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

图表

  1. #| echo: true
  2. #| code-fold: true
  3. #| code-summary: "展开查看完整代码"
  4. library(ggplot2)
  5. dat <- data.frame(cond = rep(c("A", "B"), each=10),
  6. xvar = 1:20 + rnorm(20,sd=3),
  7. yvar = 1:20 + rnorm(20,sd=3))
  8. ggplot(dat, aes(x=xvar, y=yvar)) +
  9. geom_point(shape=1) +
  10. geom_smooth()

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


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


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

  • code-fold.lua

图表

  1. #| echo: true
  2. #| code-fold: true
  3. #| code-summary: "展开查看完整代码"
  4. library(ggplot2)
  5. dat <- data.frame(cond = rep(c("A", "B"), each=10),
  6. xvar = 1:20 + rnorm(20,sd=3),
  7. yvar = 1:20 + rnorm(20,sd=3))
  8. ggplot(dat, aes(x=xvar, y=yvar)) +
  9. geom_point(shape=1) +
  10. geom_smooth()

code-fold.lua

  1. local str = pandoc.utils.stringify
  2. local template_fold_p1 = [[
  3. <details>
  4. <summary>
  5. %s
  6. </summary>
  7. ]]
  8. local template_fold_p2 = [[
  9. %s
  10. </details>
  11. ]]
  12. function Div(el)
  13. if el.attributes['code-fold'] then
  14. local code_summary = str(el.attributes['code-summary'])
  15. local fold = string.format(template_fold_p1, code_summary)
  16. local fold_rb1 = pandoc.RawBlock('html', fold)
  17. local fold_rb2 = pandoc.RawBlock('html', template_fold_p2)
  18. el.content:insert(1, fold_rb1)
  19. el.content:insert(fold_rb1)
  20. return el
  21. end
  22. 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).

  1. ---
  2. title: &quot;Code-fold in revealjs&quot;
  3. format: revealjs
  4. ---
  5. ## Graph
  6. ```{r}
  7. #| echo: true
  8. #| code-fold: true
  9. #| code-summary: &quot;expand for full code&quot;
  10. library(ggplot2)
  11. dat &lt;- data.frame(cond = rep(c(&quot;A&quot;, &quot;B&quot;), each=10),
  12. xvar = 1:20 + rnorm(20,sd=3),
  13. yvar = 1:20 + rnorm(20,sd=3))
  14. ggplot(dat, aes(x=xvar, y=yvar)) +
  15. geom_point(shape=1) +
  16. geom_smooth()
  17. ```

<hr>

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

<hr>

You can use the following Lua filter to add code folding

  1. ---
  2. title: &quot;Code-fold in revealjs&quot;
  3. format: revealjs
  4. filters:
  5. - code-fold.lua
  6. ---
  7. ## Graph
  8. ```{r}
  9. #| echo: true
  10. #| code-fold: true
  11. #| code-summary: &quot;expand for full code&quot;
  12. library(ggplot2)
  13. dat &lt;- data.frame(cond = rep(c(&quot;A&quot;, &quot;B&quot;), each=10),
  14. xvar = 1:20 + rnorm(20,sd=3),
  15. yvar = 1:20 + rnorm(20,sd=3))
  16. ggplot(dat, aes(x=xvar, y=yvar)) +
  17. geom_point(shape=1) +
  18. geom_smooth()
  19. ```

code-fold.lua

  1. local str = pandoc.utils.stringify
  2. local template_fold_p1 = [[
  3. &lt;details&gt;
  4. &lt;summary&gt;
  5. %s
  6. &lt;/summary&gt;
  7. ]]
  8. local template_fold_p2 = [[
  9. %s
  10. &lt;/details&gt;
  11. ]]
  12. function Div(el)
  13. if el.attributes[&#39;code-fold&#39;] then
  14. local code_summary = str(el.attributes[&#39;code-summary&#39;])
  15. local fold = string.format(template_fold_p1, code_summary)
  16. local fold_rb1 = pandoc.RawBlock(&#39;html&#39;, fold)
  17. local fold_rb2 = pandoc.RawBlock(&#39;html&#39;, template_fold_p2)
  18. el.content:insert(1, fold_rb1)
  19. el.content:insert(fold_rb1)
  20. return el
  21. end
  22. 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:

确定