2

In a xaringan presentation, the slideNumberFormat argument is often set to %current% / %total%. It is possible to use a specific slide number in this argument?

For instance, if I have a "final" slide like this:

---
name: mylastslide
# Thanks

with appendix slides behind, I want to display slide numbers like %current% / %mylastslide% with %mylastslide% the number of my slide called mylastslide.

Thanks.

[Edit after @user2554330 suggestion]

For this code, with an incremental slide,

---
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

The last slide (ie appendix) is numbered as Slide 6 of 9 (and not Slide 6 of 5) and 9 is the url index of mylastslide. (I used + 1 in the slideNumberFormat function because indexes start at 0.)

Thanks.

abichat
  • 2,317
  • 2
  • 21
  • 39

1 Answers1

2

You could certainly set the format to a fixed value for the last slide, e.g. %current% / 34. But you can also set it to a Javascript function. (Edited to add:) The tricky part is that you need to include all the options that would normally appear in the nature argument as well. So you want something like

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

You name a slide by putting the text

name: mylastslide

at the bottom of the slide after --- marks. So here's a complete example, based on the first few slides of the xaringan template:

---
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")
```

This has 5 slides, numbered "1 of 4" to "5 of 4".

Edited to add: As discussed in the comments, this doesn't handle incremental slides properly: getSlideIndex() counts incremental slides separately. We want to use getSlideNumber(), which stays the same on all of them when we use the option countIncrementalSlides: false. However, the online version of remark-latest.min.js doesn't have getSlideNumber() in it, you need to ask for remark-0.15.0.min.js.

You do this with the following YAML:

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

After this the code below worked fine:

<script>
var slideshow = remark.create({slideNumberFormat : function (current, total) {
  return 'Slide ' + current + ' of ' + this.getSlideByName("mylastslide").getSlideNumber(); },
  highlightStyle: "github",
  highlightLines: true,
  countIncrementalSlides: false});
</script>
user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Thanks, this is the output I wanted. However, it does not take into account incremental slides (with `--`) [maybe it's another function than `getSlideIndex`], and other argument in `nature:` are not taken into account (like `ratio` or `scroll`) [I tried to specify the js script like `slideNumberFormat: "slidenumber.js"` in the yaml but it does not work] – abichat Dec 04 '20 at 18:20
  • The first issue is a consequence of the second with the `countIncrementalSlides` argument I think. – abichat Dec 04 '20 at 18:21
  • 1
    Yes, sorry about that. I can't spot a way to update the slide options, so you'll need to put anything that would have appeared in "nature" into the `remark.create` call. I'll update my answer. – user2554330 Dec 04 '20 at 20:07
  • Yes, this works. But (I'm sorry), `getSlideIndex` gives the index of the slide (used in the url) but not the expected displayed number (which can be different with incremental slides). I tried `.getSlideNumber()` at the end but this make the presentation to block at the first slide... – abichat Dec 04 '20 at 20:50
  • I really appreciate your help, sorry for not demonstrating it... I updated my question with an example. Thanks. – abichat Dec 05 '20 at 09:14
  • Ok, thanks. Defining a custom custom JS function allows me to number differently main slides ("current/final") and annexes ("current"), and that's already really cool. Too bad for hard coding! – abichat Dec 05 '20 at 12:19
  • 1
    I've added a simpler fix now. – user2554330 Dec 05 '20 at 16:44