xaringan
xaringan copied to clipboard
Reference slide number (question)
Hi all, I have a set of slides arranged in a folder with the following structure
slides/
lecture1/
index.Rmd
...
lecture2/
index.Rmd
...
...
(with a view of publishing the whole lot under the static/slides
folder of a course website).
I can cross-ref to another lecture (eg using the code
See [Lecture 1](../lecture1/index.html)
I know I can point the link to the #name-of-the-slide
tag, say
See [here](../lecture1/#some-slide)
using the code
name: some-slide
in the relevant .Rmd file.
But is there a way for me to have as output something like
See slide 5 in Lecture 1
where the "5" is automatically mapped from the link I'm giving (say, ../lecture1/#some-slide
)?
I hope this is clear enough and I'm using the right format to ask the question! :-)
Thanks Gianluca
That's a great question, but I don't have an answer. It might be possible to implement an R function to calculate the page number based on a name
attribute, but I don't have time for that. If anyone wants to submit a PR, you may take a look at the function xaringan:::slide_context
, in which I have a rough implementation of finding the slide page number corresponding to the cursor position in the editor. If you treat the name: id
as the "cursor position", you can get the page number similarly.
ref_slide = function(input, name) {
ctx = slide_context(......) # you'll need to extend this function
n = ctx$n
sprintf('[Page %d](%s#%d)', n, with_ext(input, '.html'), n)
}
Then you can write an inline R expression in the Rmd source:
See `r xaringan::ref_slide('../lecture1/index.Rmd', 'some-slide')`
Thanks, @yihui. In the end, I made an R
function to create the "syllabus" (with the list of lectures) and then use that to reference the actual lecture and, possibly, the slide.
syllabus=tibble(
title=c(
"intro_bayes",
"bugs",
"mcmc",
"intro_he",
"ild",
"survival",
"ald",
"nma",
"mm",
"missing",
"voi"
),
number=c(
1,2,3,4,5,6,7,8,9,10,11
),
path=c(
"01_Intro",
"02_BUGS",
"03_MCMC",
"04_Intro_HE",
"05_ILD",
"06_Survival",
"07_ALD",
"08_NMA",
"09_MM",
"10_Missing",
"11_VoI"
)
)
#' Then this function formats the link-out to the lecture file
#' @param lecture The title of the lecture (as given in the table 'syllabus' in the column 'title')
#' @param name The name of a given slide (if given in the preamble to the slide, eg 'name: a-given-name')
ref_lecture=function(lecture,name=NULL) {
if(!is.null(name)) {
url=paste0("/#",name)
} else {
url="/index.html"
}
paste0('<a href="../',syllabus %>% filter(title==lecture) %>% pull(path),url,'">Lecture ',syllabus %>% filter(title==lecture) %>% pull(number),'</a>')
}
That's a bit hacky and, more importantly, I think very specialised to what I need to do, rather than a general function that people may use... But perhaps it may give inspiration for something better?