Use a specific slide number for slideNumberFormat in xaringan - r

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.

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>

Related

Can I remove the [code] button in a rmarkdown html_document while still displaying the code with the [Show/Hide All Code] button

While I like the possibility to show/hide all code that is allowed by the code_folding property of the html_document output in rmarkdown, I do not like that I have to see a Code button everywhere there is some code because it creates "empty line(s)" in the document.
Is there a way to remove this/these Code button(s) with a property or some javascript magic
without altering the Rmd document (ie. not writing some additional code in the document itself)
while still allowing to show/hide all code through the Show/Hide All Code toggle
Here is an example:
---
title: "Test"
date: '`r Sys.Date()`'
output:
html_document:
code_download: true
code_folding: 'hide'
---
# Title 1
chunk 1
```{r, eval = FALSE}
1+1
```
```{r, eval = FALSE}
1+2
```
```{r}
1+3
```
Which renders like this:
You can hide the code-folding buttons with a css rule.
---
title: "Test"
date: '`r Sys.Date()`'
output:
html_document:
css: "style.css"
code_download: true
code_folding: 'hide'
---
The style.css
.code-folding-btn {
display: none;
}

Xaringan: center image within one of two columns in a two columns layout

I am using a two-column layout and I was wondering if I could center the image within the second column.
This is the YAML:
---
title: "Title"
subtitle:
author:
date: " </br> `r Sys.Date()`"
output:
xaringan::moon_reader:
lib_dir: libs
css: ["default", "custom.css", "custom-fonts.css"]
nature:
beforeInit: "http://www.jvcasillas.com/ru_xaringan/js/ru_xaringan.js"
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
ratio: "16:9"
---
The slide:
---
# Introduction
.pull-left[
- One bullet.
- Another bullet.
- another
- another
- another
]
.pull-right[
<img src="img/book.jpg" alt="drawing" width="200"/>
]
And this is how it looks like:
As you can see, the image is "hanging" from the top left corner of the second column. I was wondering if I could have it centered within the second column.
Following Tomas Capretto's suggestion:
---
# Introduction
.pull-left[
- One bullet.
- Another bullet.
- another
- another
- another
]
.pull-right[ .center[ <img src="img/book.jpg" alt="drawing" width="200"/> ] ]
Here is the result:

xaringan set the document title dynamically

In r-markdown is is an option to move the title: out of the main yaml, as described in the R Markdown Cookbook.
But, in a xaringan slide set the new --- seems to conflict with the idea of new slide.
The below code works, but when move line #2, title: "Presentation Ninja" outside the main yaml, and inset it as title: "The name of the dog is r dog_name!", by removing all my <!-- ... --> code in line #17-19, it does not work as expected. I do no longer get a title page. I guess I need to work around the --- in xaringan?
Any help would be appreciated!
---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
output:
xaringan::moon_reader:
lib_dir: libs
---
# xaringan set the document title dynamically
```{r, code=xfun::read_utf8('script_with_many_dog_names.R')}
```
The name of the dog is `r dog_name`!
<!-- --- -->
<!-- title: "The name of the dog is `r dog_name`!" -->
<!-- --- -->
Some bullets
- Foo
- Bar
You could use R Markdown parameters :
Create a template file Template.Rmd
---
title: "The name of the dog is `r params$dog_name`"
subtitle: "⚔<br/>with xaringan"
author: "John Doe"
output:
xaringan::moon_reader:
lib_dir: libs
params:
dog_name: NA
---
# xaringan set the document title dynamically
Some bullets
- Foo
- Bar
Render the template after setting dog_name parameter :
source('script_with_many_dog_names.R')
# As an example, but it could be the result of previous script
params <- list(dog_name = 'Charles')
rmarkdown::render('Template.Rmd', output_file = 'presentation.html',
params = params,
envir = new.env(parent = globalenv())
)
You can add your logic directly to the title, that seems to work fine for me:
---
title: "The number is `r round(100 * runif(1), 2)`"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
output:
xaringan::moon_reader:
lib_dir: libs
---

Do not indent the first paragraph of a new section

I would like to indent every paragraph but the first paragraph of every section in my article. The Rmd file has a pdf_output and it is referencing child files containing the sections of my article.
This is what I have:
---
bibliography: bibliography.bib
header-includes:
- \usepackage{setspace}\doublespacing
- \usepackage{float}
- \setlength{\parskip}{12pt}
#- \setlength{\parindent}{30pt}
fontsize: 12pt
indent: true
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
```
```{r 01_Introduction, child = here::here("WriteUp", "Child", "Introduction.Rmd")}
```
\newpage
# References
I would like to indent every paragraph but the first paragraph of every section in my article.
You use indent in the YAML. See this answer.
Here is an example. First the main file:
---
title: "test"
author: "me"
date: "11/3/2020"
output: pdf_document
indent: true
---
```{r, child=c('sec1.Rmd', 'sec2.Rmd')}
```
Here is the sec1.Rmd file:
## My section
Some text.
Some more text.
and here is the sec2.Rmd file:
# That other section
Even more text.
And the last line.
The result is:
Update
It is still indented after I have added:
header-includes:
- \usepackage{setspace}\doublespacing
- \usepackage{float}
- \setlength{\parskip}{12pt}
to the YAML.

How can I add space between the table of contents and the YAML title?

A basic R markdown script:
---
title: "Untitled"
author: "Example example"
output:
html_document:
df_print: paged
toc: true
number_sections: true
---
# ALPHA Header
## Alpha sub-1
## Alpha sub-2
## Alpha sub-3
Will generate something akin to this:
I'd like to pad the space between the YAML headers and the table of contents - but I'm not sure how. I can specify CSS selector and styles but this only applies to Markdown after the auto generated ToC.
Just add HTML line breaks in your author or title fields:
author: "Example example<br><br>"
Or a div with a certain height:
author: "Example example<div style=\"height: 100px;\"></div>"

Resources