Debian Codegraph @

vignettes/slides.Rmd

raw
1 ---
2 title: Making HTML Slides with the **litedown** Package
3 author: "[Yihui Xie](https://yihui.org)"
4 date: "`{r} Sys.Date()`"
5 output:
6 html:
7 meta:
8 css: ["@default", "@snap", "@key-buttons", "@heading-anchor"]
9 js: ["@snap", "@key-buttons", "@external-link", "@heading-anchor"]
10 options:
11 toc: true
12 number_sections: true
13 vignette: >
14 %\VignetteEngine{litedown::vignette}
15 %\VignetteIndexEntry{Making HTML Slides with the litedown Package}
16 ---
17
18 ## Get started
19
20 Specify at least one CSS file (`snap.css`) and one JS script (`snap.js`) in YAML
21 metadata:
22
23 ``` yaml
24 ---
25 output:
26 html:
27 meta:
28 css: [default, snap]
29 js: [snap]
30 ---
31 ```
32
33 You will learn more about `snap.css` and `snap.js` at the end of this
34 presentation.
35
36 --------------------------------------------------------------------------------
37
38 <!--# class="fade" -->
39
40 Equivalently, you can specify them in the arguments of `litedown::mark()`:
41
42 ``` r
43 litedown::mark("test.md", meta = list(
44 css = c("default", "snap"),
45 js = "snap"
46 ))
47 ```
48
49 but I recommend that you specify these options in YAML metadata instead.
50
51 --------------------------------------------------------------------------------
52
53 ## Create slides
54
55 There are two ways to create a new slide:
56
57 1. Insert a horizontal rule (`---`).
58 2. Or use the level-two section heading (`##`).
59
60 You must choose only one way, i.e., either use horizontal rules to separate all
61 slides, or avoid horizontal rules but only use section headings.
62
63 The first way is more flexible---you don't have to start a slide with a section
64 heading.
65
66 --------------------------------------------------------------------------------
67
68 ### Example (`---`)
69
70 ``` markdown
71 ## First slide
72
73 **Content**.
74
75 ---
76
77 More _content_ on the next page.
78
79 ---
80
81 ## Third slide
82 ```
83
84 --------------------------------------------------------------------------------
85
86 ### Example (`##`)
87
88 ``` markdown
89 ## First slide
90
91 Content.
92
93 ## Second slide
94
95 Content.
96
97 ## Third slide
98 ```
99
100 --------------------------------------------------------------------------------
101
102 ## Keyboard shortcuts
103
104 - <kbd>f</kbd>: Fullscreen mode (press `Esc` to exit). The scroll bar will be
105 hidden in the fullscreen mode. You can also use the keyboard shortcut of
106 your browser, e.g., `fn + F` in Chrome on macOS.
107
108 - <kbd>o</kbd>: Overview mode
109
110 - Slides become thumbnails, to make it easier to glance over multiple
111 slides at once.
112
113 - Press <kbd>o</kbd> again to go back to presentation mode.
114
115 - Alternatively, you can click on a slide while holding the `Alt` key to
116 toggle the overview mode and navigate to the clicked slide. Basically,
117 `Alt + Click` is like zoom in/out.
118
119 --------------------------------------------------------------------------------
120
121 - <kbd>m</kbd>: Mirror mode
122
123 - Slides are mirrored.[^1]
124
125 - Press <kbd>m</kbd> again to resume.
126
127 [^1]: See [this post](https://yihui.org/en/2020/04/xaringan-mirror/) for a
128 possible application of this odd feature.
129
130 --------------------------------------------------------------------------------
131
132 ## CSS and styling
133
134 You can pass more CSS files to the `css` option, e.g., if you have `extra.css`
135 under the same directory as the Markdown input file:
136
137 ``` yaml
138 ---
139 output:
140 html:
141 meta:
142 css: [default, snap, extra.css]
143 js: [snap]
144 ---
145 ```
146
147 --------------------------------------------------------------------------------
148
149 If your input document is `.Rmd`, you can also embed CSS directly in a `css`
150 code chunk:
151
152 ```` markdown
153 ```{css}
154 #| echo = FALSE
155
156 /* your CSS rules */
157 ```
158 ````
159
160 --------------------------------------------------------------------------------
161
162 Below is a CSS code chunk in which we defined the font families for this
163 presentation:
164
165 ```{css}
166 body {
167 font-family: Georgia, serif;
168 }
169 .slide h1, .slide h2 {
170 font-family: Baskerville, Garamond, serif;
171 }
172 code {
173 font-family: Consolas, "Andale Mono", monospace;
174 font-weight: bold;
175 }
176 ```
177
178 --------------------------------------------------------------------------------
179
180 ### Example: section numbers
181
182 When the Markdown option `number_sections` is enabled, all sections are
183 numbered. You can hide all numbers via CSS:
184
185 ```{css}
186 .section-number { display: none; }
187 ```
188
189 For this presentation, only section numbers for level-two headings are
190 displayed:
191
192 ```{css}
193 #TOC > ul > li > .section-number,
194 h2 > .section-number {
195 display: inline-block;
196 }
197 ```
198
199 --------------------------------------------------------------------------------
200
201 ### Example: TOC
202
203 If you enable the table of contents (TOC) by setting the option `toc: true`, you
204 will get a TOC slide after the title slide. It uses a two-column layout by
205 default. You can custom its styles via the CSS selector `#TOC`. For example, you
206 can use three columns:
207
208 ``` css
209 #TOC { columns: 3; }
210 ```
211
212 --------------------------------------------------------------------------------
213
214 Or define the TOC title by:
215
216 ```{css}
217 #TOC::before { content: "Outline"; }
218 ```
219
220 Or shorten TOC (hide lower-level headings):
221
222 ``` css
223 #TOC li ul { display: none; }
224 ```
225
226 --------------------------------------------------------------------------------
227
228 For this presentation, we don't hide lower-level headings in TOC but just make
229 them more compact (`display: inline;`):
230
231 ```{css}
232 #TOC li ul li {
233 display: inline;
234 border-left: 0.2em dotted #ccc;
235 padding-left: 0.2em;
236 }
237 #TOC li ul li a {
238 color: #666;
239 text-decoration: none;
240 }
241 ```
242
243 --------------------------------------------------------------------------------
244
245 ### Responsive layout
246
247 | Media | Width | Mode | Font size | Overview columns |
248 |---------------------|----------|:-------:|:---------:|:----------------:|
249 | Super large devices | ≥ 1800px | ↓ | ↓ | 4 |
250 | Larger desktops | ≥ 1400px | ↓ | ↓ | 3 |
251 | Desktops | ≥ 992px | Slides | 200% | 2 |
252 | Phones and tablets | \< 992px | Article | 100% | N/A |
253
254 You can resize your browser window to see the effect (also try to press
255 <kbd>o</kbd> and test the overview mode). If you are on a mobile device, you
256 should see a normal continuous page, since you cannot adjust the window size.
257
258 --------------------------------------------------------------------------------
259
260 ### Printing
261
262 - When the slides are printed, the font size will be reduced.
263
264 - To save space, the presentation is printed as a *continuous* document
265 instead of slides.
266
267 - If you want borders on slides, print them from the overview mode.
268
269 - To customize style for printing, you may define CSS rules in:
270
271 ``` css
272 @media print {
273 }
274 ```
275
276 --------------------------------------------------------------------------------
277
278 If you need to print each slide on a separate page, you may include the
279 following CSS and may need to tweak the paper size and font size. The PDF will
280 not have exactly the same appearance as HTML.
281
282 ``` css
283 @page {
284 size: a4 landscape;
285 margin: 0;
286 }
287
288 @media print {
289 .slide {
290 font-size: 1.8em;
291 page-break-after: always;
292 }
293 }
294 ```
295
296 --------------------------------------------------------------------------------
297
298 ## Slide attributes
299
300 You can add more attributes to a slide via an HTML comment that starts with `#`
301 (`Cmd / Ctrl + Shift + C` in RStudio's visual Markdown editor), e.g.,
302
303 ``` html
304 <!--# class="inverse" style="color: red;"
305 contenteditable -->
306 ```
307
308 The syntax is just HTML. These attributes will be added to the slide container:
309
310 ``` html
311 <div class="slide inverse" style="color: red;" contenteditable>
312 </div>
313 ```
314
315 --------------------------------------------------------------------------------
316
317 ### Built-in classes
318
319 - `inverse`: Apply a dark background and invert the slide color.
320
321 - `center`: Center all elements horizontally.
322
323 - `middle`: Center all elements vertically.
324
325 - `fade`: Fade a slide (50% opacity) and apply grid lines to the background
326 (can be useful for de-emphasizing a slide).
327
328 You can define your own arbitrary class names (e.g., `<!--# class="large" -->`)
329 and corresponding CSS rules (e.g., `.large { font-size: 150%; }`).
330
331 --------------------------------------------------------------------------------
332
333 ### Example: an inverse slide
334
335 <!--# class="inverse" style="font-size: 130%;" -->
336
337 ``` html
338 <!--# class="inverse" style="font-size: 130%;" -->
339 ```
340
341 - The background is (nearly) black and the text is white.
342
343 - The font size is 30% larger.
344
345 --------------------------------------------------------------------------------
346
347 ### Example: center content
348
349 <!--# class="middle center" -->
350
351 Everything is centered both vertically and horizontally.
352
353 ``` html
354 <!--# class="middle center" -->
355 ```
356
357 Of course, you don't have to use both classes at the same time. Depending on how
358 you want to center content, you can use one of these classes.
359
360 --------------------------------------------------------------------------------
361
362 ### Example: fade a slide
363
364 <!--# class="fade" -->
365
366 ``` html
367 <!--# class="fade" -->
368 ```
369
370 This slide is not important. You do not need to read it carefully. You can even
371 take a nap, since the speaker is boring.
372
373 --------------------------------------------------------------------------------
374
375 ### Example: a background image
376
377 <!--# style="background-image: url(https://user-images.githubusercontent.com/163582/219277796-6d2dd826-fb11-4970-b07c-dd920a694e3b.jpg); background-size: cover; color: lightgreen;" -->
378
379 ``` html
380 <!--#
381 style="background-image: url(path/to/image);"
382 -->
383 ```
384
385 We use the `style` attribute to introduce a background image to this slide. You
386 can learn more about the `background-image` property
387 [here](https://www.w3schools.com/cssref/pr_background-image.php).
388
389 --------------------------------------------------------------------------------
390
391 ### Example: an editable slide
392
393 <!--# contenteditable style="font-family: fantasy, monospace;" -->
394
395 ``` html
396 <!--# contenteditable -->
397 ```
398
399 Believe it or not, this slide is editable because we have enabled the
400 `contenteditable` attribute. If you find any mistake on your slide during your
401 presentation, you can click on it and edit any text.
402
403 Note that your edits *will not* be saved, though.
404
405 --------------------------------------------------------------------------------
406
407 ## Miscellaneous elements
408
409 ### Page numbers
410
411 They are placed in `<span class="page-number"></span>` at the bottom right of
412 all slides.
413
414 If you click on a page number, the URL of the presentation will be appended by a
415 hash of the form `#N`, where `N` is the page number. You can share this URL with
416 other people when you want to point them to a specific page in the slides.
417
418 --------------------------------------------------------------------------------
419
420 ### Timer
421
422 A timer is added to the bottom left in `<span class="timer"></span>` by default.
423 If you want a *countdown* timer, you can add a custom `<span>` to your document
424 (in Markdown, you can use a raw HTML block ```` ```{=html} ````), and specify
425 the total number of seconds in the attribute `data-total`, e.g.,
426
427 ``` html
428 <span class="timer" data-total="1200"></span>
429 ```
430
431 The timer will start when the presentation becomes fullscreen. To restart the
432 timer, click on it.
433
434 For the countdown timer, when the time is up, the timer will start to blink.
435
436 ```{=html}
437 <span class="timer" data-total="1200"></span>
438 ```
439
440 --------------------------------------------------------------------------------
441
442 ## Caveats
443
444 ### Lengthy slides
445
446 When the height of a slide exceeds the window height, you need to be careful
447 because it can be easy to accidentally scroll to the next page as you approach
448 the bottom of the slide. If you move from an oversized slide to the next slide
449 by accident, you *will not* be able to move back to the bottom of the previous
450 slide directly! Instead, you will always be navigated to the top of the previous
451 slide if you want to go back. When you are on a long slide, I recommend that you
452 use your mouse wheel or the `Down` arrow key to scroll at small steps, instead
453 of using the `PageDown` key to scroll over to the next screen at once.
454
455 Because of this hassle, you may not really want to make a slide lengthy, but it
456 may be unavoidable when you have lengthy content to show on one slide and it
457 cannot be broken. Below is a toy example (the output block cannot be split onto
458 two slides):
459
460 ```{r}
461 #| comment = ''
462
463 cat(1:10, sep = "\n")
464 ```
465
466 --------------------------------------------------------------------------------
467
468 ### Page mode
469
470 The page will switch between the *slides* and *article* mode depending on the
471 ratio of the window height to width, $R_w=H_w/W_w$.
472
473 - If $R_w$ is greater than the screen height/width ratio $R_s=H_s/W_s$, the
474 page will be in the article mode.
475
476 - If $R_w \leq R_s$, the page will be in the slides mode.
477
478 - That means when the window is too narrow, the page will be a continuous
479 article. If the window is wide enough (at least 992px), it will show the
480 slides.
481
482 --------------------------------------------------------------------------------
483
484 ### Aspect ratio
485
486 The aspect ratio of slides is determined by your screen aspect ratio by default.
487 The content will fit all available space on the screen when the slides are
488 full-screen (press <kbd>f</kbd>). If you present the slides directly with your
489 own computer, this may not be a problem since you know if all content fits well
490 on your own screen. However, if you connect your computer to a projector or
491 present the slides on another computer, you'd better know the screen resolution
492 beforehand, because the aspect ratio may be different, and your slides can look
493 different on that screen.
494
495 You can fix the aspect ratio by setting the CSS variable `--slide-ratio`, e.g.,
496
497 ``` css
498 :root { --slide-ratio: .75; }
499 ```
500
501 --------------------------------------------------------------------------------
502
503 ### Zooming
504
505 For best results in the slide mode, set the browser zooming level to 100%.
506 Zooming out (\< 100%) is usually okay. Zooming in (\> 100%) may lead to slides
507 being cut off at the margin.
508
509 --------------------------------------------------------------------------------
510
511 ### Cross-browser/device issues
512
513 Unlike PDF, HTML slides may not look exactly the same in different web browsers
514 or on different devices. For example, text may be rendered via system fonts, and
515 different systems may have different fonts. To ensure the same fonts are used on
516 different devices, you may use web fonts (e.g., Google Fonts).
517
518 You may print the slides to PDF in advance. In case you have trouble with HTML
519 slides when presenting from a different device, you may use the PDF copy (of
520 course, you will lose features that rely on HTML/CSS/JS).
521
522 --------------------------------------------------------------------------------
523
524 ## Technical notes
525
526 How does this HTML presentation work under the hood?
527
528 ### The original HTML
529
530 ``` html
531 <h2>First slide</h2>
532 <p>Content</p>
533 <hr>
534 <h2>Second slide</h2>
535 <p>More content</p>
536 <hr>
537 ```
538
539 This can be generated from Markdown.
540
541 --------------------------------------------------------------------------------
542
543 ### snap.js
544
545 The script [`snap.js`](https://github.com/yihui/lite.js/blob/main/js/snap.js)
546 converts HTML to a structure more convenient to style as slides:
547
548 ``` html
549 <div class="slide-container">
550 <div class="slide">
551 <h2>First slide</h2>
552 <p>Content</p>
553 </div>
554 <div class="slide">
555 <h2>Second slide</h2>
556 <p>More content</p>
557 </div>
558 </div>
559 ```
560
561 --------------------------------------------------------------------------------
562
563 ### snap.css
564
565 The core technique is [CSS Scroll
566 Snap](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Scroll_Snap/Basic_concepts).
567 The full CSS is
568 [`snap.css`](https://github.com/yihui/lite.js/blob/main/css/snap.css), and the
569 essential rules are:
570
571 ``` css
572 html {
573 scroll-snap-type: y mandatory;
574 }
575 .slide {
576 min-height: 100vh;
577 scroll-snap-align: start;
578 }
579 ```
580
581 --------------------------------------------------------------------------------
582
583 The JS and CSS code can be used outside the R package **litedown**, too. You
584 just need to have the correct HTML structure. Then in your HTML document:
585
586 ``` html
587 <script src="https://cdn.jsdelivr.net/npm/@xiee/utils@VERSION/js/snap.min.js" defer></script>
588 <link rel="stylesheet" href="https://cdn.jsdelivr.net/combine/npm/@xiee/utils@VERSION/css/default.min.css,npm/@xiee/utils@VERSION/css/snap.min.css">
589 ```
590
591 Remember to substitute `VERSION` with [an appropriate
592 version](https://github.com/yihui/lite.js/tags) (e.g., `1.11.0`). You can omit
593 `@VERSION` to use the latest version but it is not recommended because future
594 updates might break your old slides.
595
596 Both the JS and CSS are quite lightweight (total size is
597 `{r} xfun::format_bytes(sum(file.size(pres_files <- litedown:::pkg_file('resources', c('default.css', 'snap.css', 'snap.js')))))`
598 when uncompressed) and have no dependencies. They were written from scratch.
599
600 --------------------------------------------------------------------------------
601
602 ## Enjoy!
603
604 <!--# style="background-image: url(https://upload.wikimedia.org/wikipedia/commons/7/7e/Mudra-Naruto-KageBunshin.svg); background-position: bottom right; background-repeat: no-repeat; background-size: 25%;" -->
605
606 - I developed the [xaringan](https://github.com/yihui/xaringan) in package
607 2016 and have used it since then. It still works perfectly, but now I prefer
608 more lightweight frameworks.
609
610 - Most of presentation features that I need are included in
611 `{r} length(xfun::read_all(pres_files))` lines of JS/CSS code (`default.css`
612 / `snap.js` / `snap.css`).
613
614 - This simple presentation framework is highly customizable. Customizing
615 slides can be addictive if you know HTML/CSS/JS.
616
617 - Learn more about the Markdown syntax at <https://yihui.org/litedown/>.
618
619 - Github repo: <https://github.com/yihui/litedown> (you can find the [Rmd
620 source](https://github.com/yihui/litedown/blob/main/vignettes/slides.Rmd) of
621 this presentation in the repo)