-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
_utils.qmd
68 lines (56 loc) · 2.71 KB
/
_utils.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
```{r authors, include = FALSE, cache = FALSE}
## Adds chapter authors to TOC and chapter header
## x - Named list, names are author name, entries are affiliations
authors = function(chaptitle) {
authors = unlist(strsplit(data.table::fread(here::here("book/common/chap_auths.csv"))[Title == chaptitle, Authors], ", "))
if (is.null(authors)) stop(sprintf("Chapter %s not found", chaptitle))
affils = data.table::fread(here::here("book/common/authors.csv"))[authors, on = "Author", Affiliations]
quarto_fmt = "**%s** \\newline <br> *%s* \n "
latex_fmt = "\\addtocontents{toc}{\\textit{%s}}"
quarto_out = paste0(sprintf(quarto_fmt, authors, affils), collapse = "\n ")
if (length(authors) == 1) {
latex_out = sprintf(latex_fmt, authors)
} else {
latex_out = paste0(authors, collapse = ", ")
ind = tail(gregexpr(",", latex_out)[[1]], 1)
latex_out = paste(substr(latex_out, 1, ind - 1), "and", substr(latex_out, ind + 2, nchar(latex_out)))
latex_out = sprintf(latex_fmt, latex_out)
}
paste0("\\vspace{-15mm}", latex_out, quarto_out, "\\newline \\newline <br><br> ", collapse = " ")
}
citeas = function(chaptitle) {
authors = unlist(strsplit(data.table::fread(here::here("book/common/chap_auths.csv"))[Title == chaptitle, Authors], ", "))
if (is.null(authors)) {
stop(sprintf("Chapter %s not found", chaptitle))
}
authors_short = paste0(vapply(strsplit(authors, " "), function(.x) {
paste(.x[[length(.x)]], paste(substr(.x[seq(1, length(.x) - 1)], 1, 1), collapse = ""))
}, character(1)), collapse = ", ")
chaphtml = paste0(paste0(tolower(strsplit(chaptitle, " ")[[1]]), collapse = "_"), ".html")
txt = sprintf('## Citation
Please cite this chapter as:
%s. (2024). %s. In Bischl B, Sonabend R, Kotthoff L, Lang M, (Eds.), *Applied Machine Learning Using mlr3 in R*. CRC Press. https://mlr3book.mlr-org.com/%s.', authors_short, chaptitle, chaphtml)
tex = sprintf('
@incollection{citekey,
author = "%s",
title = "%s",
booktitle = "Applied Machine Learning Using {m}lr3 in {R}",
publisher = "CRC Press", year = "2024",
editor = "Bernd Bischl and Raphael Sonabend and Lars Kotthoff and Michel Lang",
url = "https://mlr3book.mlr-org.com/%s"
}',
paste0(authors, collapse = " and "), chaptitle, chaphtml)
# Wrap in fenced code block for formatting, note that tex starts with newline
# hence we place %s directly after the ``` to avoid unnecessary empty line in output
sprintf("%s
```bibtex%s
```
", txt, tex)
}
```
```{r include_multi_graphics, include = FALSE, cache = FALSE}
include_multi_graphics = function(fig_name) {
fmt = if (knitr::is_latex_output()) "png" else "svg"
knitr::include_graphics(sprintf("Figures/%s.%s", fig_name, fmt), dpi = 600)
}
```