xaringan set the document title dynamically - r

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

Related

How to change the font color of title, author, date and make it bold in yaml of Rmarkdown?

---
title: "PDF Document"
author: "Sana"
date: "27-05-2022"
---
How to change the font color and style of title, author, date and make it bold or italic in yaml of Rmarkdown?
One way is to use a latex code chunk in the title.
---
title: |
```{=latex}
\textcolor{red}{\textbf{PDF Document}}
```
author: "Sana"
output: pdf_document
---

Create a hyperlink from the logo to the table of contents slide in an rmarkdown::beamer_presentation with a custom beamer theme

I use a custom LaTex beamer theme in an rmarkdown::beamer_presentation.
As per these SO answers (LaTex theme, colon, theme path), I used several modifications of the YAML header and beamerthemeTHEMENAME.sty.
These LaTex hacks are necessary to apply the LaTex Beamer theme smoothly in the rmarkdown::beamer_presentation.
For the foot line defined in beamerouterthemeTHEMENAME.sty, it would be very nice to have a hyperlink from the logo to the table of contents slide (like the slide numbers are linked to the appendix).
What I tried: define custom foot line in beamerouterthemeTHEMENAME.sty
\mode<presentation>
...
% Foot line
\setbeamertemplate{footline}{
\leavevmode%
\hyperlink{toc---table-of-contents}{\includegraphics[width=12mm,trim=0mm 0.4mm 0mm 0mm]{img/my_logo.png}}
\hfill
\hyperlinkappendixstart{\insertframenumber/\inserttotalframenumber}
\vspace{3mm}
}
\mode<all>
For a complete MWE, see my SO question here.
In normal beamer code, you could simply attach a label to the frame, but Rmarkdown seems to be too stupid to correctly parse the square brackets in \begin{frame}[label=outline].... annoying!
As a workaround you could use something like a section name for which markdown will automatically insert a label:
---
subtitle: "Beamer presnetation with R-markdown"
institute: "some place"
date: "`r format(Sys.time(), '%B %d, %Y')`"
author: "Donald Duck"
output:
# beamer_presentation: default
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
# includes:
# in_header: preamble.tex
theme: "THEMENAME"
latex_engine: xelatex
toc: false
slide_level: 2
keep_tex: true
header-includes:
- \AtBeginDocument{\title{MWE}\titleframe}
- \AtEndDocument{\begin{closingframe}lalala\end{closingframe}}
- \makeatletter\beamer#ignorenonframefalse\makeatother
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Outline {.unnumbered}
\tableofcontents
# section
## Slide with Bullets
<!-- ======================================================== -->
- Bullet 1
- Bullet 2
- Bullet 3
<!-- Appendix -->
<!-- ======================================================== -->
``` {=latex}
\end{frame}
\appendix
\begin{frame}
```
and use this target in the footline
% Footline
\setbeamertemplate{footline}{
\leavevmode%
\hyperlink{outline}{\includegraphics[width=12mm,trim=0mm 0.4mm 0mm 0mm]{example-image}}
\hfill
\hyperlinkappendixstart{\insertframenumber/\inserttotalframenumber}
\vspace{3mm}
}

Use a specific slide number for slideNumberFormat in xaringan

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>

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