【问题标题】:Use a specific slide number for slideNumberFormat in xaringan在 xaringan 中为 slideNumberFormat 使用特定的幻灯片编号
【发布时间】:2021-03-16 15:49:28
【问题描述】:

xaringan 演示文稿中,slideNumberFormat 参数通常设置为%current% / %total%。在这个参数中可以使用特定的幻灯片编号吗?

例如,如果我有这样的“最终”幻灯片:

---
name: mylastslide
# Thanks

后面有附录幻灯片,我想显示像 %current% / %mylastslide% 这样的幻灯片编号,%mylastslide% 是我的幻灯片编号 mylastslide

谢谢。

[在@user2554330 建议后编辑]

对于此代码,使用增量幻灯片,

---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
institute: "RStudio, PBC"
date: "2016/12/12 (updated: `r Sys.Date()`)"
output:
  xaringan::moon_reader:
    lib_dir: libs
---

background-image: url(https://upload.wikimedia.org/wikipedia/commons/b/be/Sharingan_triple.svg)

```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
```

<script>
var slideshow = remark.create({slideNumberFormat : function (current, total) {
  return 'Slide ' + current + ' of ' + (this.getSlideByName("mylastslide").getSlideIndex() + 1); },
  highlightStyle: "github",
  highlightLines: true,
  countIncrementalSlides: false});
</script>

Image credit: [Wikimedia Commons](https://commons.wikimedia.org/wiki/File:Sharingan_triple.svg)

---
class: center, middle

# xaringan

---

hello 

--

world

--

thanks

--

really

--

byebye

---
name: mylastslide

# Final slide

Install the **xaringan** package from [Github](https://github.com/yihui/xaringan):

```{r eval=FALSE, tidy=FALSE}
devtools::install_github("yihui/xaringan")
```

---

# Appendix

最后一张幻灯片(即附录)编号为Slide 6 of 9(而不是Slide 6 of 5),9 是mylastslideurl 索引。 (我在slideNumberFormat 函数中使用了+ 1,因为索引从0 开始。)

谢谢。

【问题讨论】:

    标签: r r-markdown xaringan remarkjs


    【解决方案1】:

    您当然可以将最后一张幻灯片的格式设置为固定值,例如%current% / 34。但您也可以将其设置为 Javascript 函数。 (编辑添加:)棘手的部分是您还需要包括通常出现在nature 参数中的所有选项。所以你想要类似的东西

    <script>
    var slideshow = remark.create({slideNumberFormat : function (current, total) {
      return 'Slide ' + current + ' of ' + this.getSlideByName("mylastslide").getSlideIndex(); },
      highlightStyle: "github",
      highlightLines: true,
      countIncrementalSlides: false});
    </script>
    

    您可以通过放置文本来命名幻灯片

    name: mylastslide
    

    在幻灯片底部--- 标记之后。所以这里有一个完整的例子,基于xaringan模板的前几张幻灯片:

    ---
    title: "Presentation Ninja"
    subtitle: "⚔<br/>with xaringan"
    author: "Yihui Xie"
    institute: "RStudio, PBC"
    date: "2016/12/12 (updated: `r Sys.Date()`)"
    output:
      xaringan::moon_reader:
        lib_dir: libs
    ---
    
    background-image: url(https://upload.wikimedia.org/wikipedia/commons/b/be/Sharingan_triple.svg)
    
    ```{r setup, include=FALSE}
    options(htmltools.dir.version = FALSE)
    ```
    
    <script>
    var slideshow = remark.create({slideNumberFormat : function (current, total) {
      return 'Slide ' + current + ' of ' + this.getSlideByName("mylastslide").getSlideIndex(); },
      highlightStyle: "github",
      highlightLines: true,
      countIncrementalSlides: false});
    </script>
    
    ???
    
    Image credit: [Wikimedia Commons](https://commons.wikimedia.org/wiki/File:Sharingan_triple.svg)
    
    ---
    class: center, middle
    
    # xaringan
    
    ### /ʃaː.'riŋ.ɡan/
    
    ---
    class: inverse, center, middle
    
    # Get Started
    
    ---
    name: mylastslide
    
    # Hello World
    
    Install the **xaringan** package from [Github](https://github.com/yihui/xaringan):
    
    ```{r eval=FALSE, tidy=FALSE}
    devtools::install_github("yihui/xaringan")
    ```
    

    这有 5 张幻灯片,编号为“1 of 4”到“5 of 4”。

    编辑添加:如 cmets 中所述,这不能正确处理增量幻灯片:getSlideIndex() 单独计算增量幻灯片。我们想使用getSlideNumber(),当我们使用选项countIncrementalSlides: false 时,它们都保持不变。但是remark-latest.min.js的在线版没有getSlideNumber(),你需要问remark-0.15.0.min.js

    您使用以下 YAML 执行此操作:

          xaringan::moon_reader:
            lib_dir: libs
            chakra: https://remarkjs.com/downloads/remark-0.15.0.min.js
    

    在此之后,下面的代码运行良好:

    <script>
    var slideshow = remark.create({slideNumberFormat : function (current, total) {
      return 'Slide ' + current + ' of ' + this.getSlideByName("mylastslide").getSlideNumber(); },
      highlightStyle: "github",
      highlightLines: true,
      countIncrementalSlides: false});
    </script>
    

    【讨论】:

    • 谢谢,这是我想要的输出。但是,它没有考虑增量幻灯片(使用--)[也许它是getSlideIndex之外的另一个函数],并且nature:中的其他参数没有考虑到(如ratioscroll)[我试图在yaml中指定像slideNumberFormat: "slidenumber.js"这样的js脚本,但它不起作用]
    • 第一个问题是第二个问题的结果,我认为countIncrementalSlides 参数。
    • 是的,很抱歉。我找不到更新幻灯片选项的方法,因此您需要将“自然”中出现的任何内容放入remark.create 调用中。我会更新我的答案。
    • 是的,这行得通。但是(我很抱歉),getSlideIndex 给出了幻灯片的索引(在 url 中使用),而不是预期的显示数字(可能与增量幻灯片不同)。我最后尝试了.getSlideNumber(),但这使得演示文稿在第一张幻灯片上被阻止......
    • 我现在添加了一个更简单的修复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2019-01-18
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2020-02-11
    • 2020-02-03
    相关资源
    最近更新 更多