-
Notifications
You must be signed in to change notification settings - Fork 5
/
tutorial_pdf.R
69 lines (55 loc) · 2.67 KB
/
tutorial_pdf.R
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
#' Convert to a tutorial PDF document
#'
#' Format for converting from R Markdown to a tutorial PDF document.
#'
#' @inheritParams rmarkdown::pdf_document
#'
#' @details See the inherited `rmarkdown::pdf_document` help page for additional arguments.
#'
#' @param solution Turn ON or OFF the rendering of solution chunks (default is \code{FALSE})
#'
#' @param suffix Suffix which is added to the filename (default is '_question' for 'unilur::tutorial_pdf' and '_solution' for 'unilur::tutorial_pdf_solution')
#'
#' @return R Markdown output format to pass to \code{\link[rmarkdown]{render}}
#'
#' @export
tutorial_pdf <- function(solution = FALSE,
suffix = "_question",
pandoc_args = NULL,
includes = NULL,
...
) {
header <- system.file("rmd", "tutorial_pdf", "boxes.tex",
package = "unilur")
#pandoc_args <- c(pandoc_args, "--variable", "geometry:margin=1in") # Adjusts the margin
#pandoc_args <- c(pandoc_args, "--variable", "graphics=yes") # Enables rescaling of too big graphics
format <- rmarkdown::pdf_document(pandoc_args = pandoc_args, includes = merge_includes(list(in_header = header), includes), ...)
format <- unilur_base(format, isTRUE(solution), suffix)
format
}
#' @rdname tutorial_pdf
#' @export
tutorial_pdf_solution <- function(suffix = "_solution", ...) {
tutorial_pdf(solution = TRUE, suffix = suffix, ...)
}
boxify_latex <- function(x, options, box_theme) {
colour_names <- c("body.fill", "body.colour", "header.fill", "header.colour")
xcolor_def <- rapply(box_theme,
function(i) apply(i, 2, to_latex_rgb),
classes = "box_colour",
how = "unlist")[colour_names]
xcolor_def <- paste0("\\definecolor{", options[["label"]], ".", colour_names, "}", xcolor_def)
alpha <- round(100 * rapply(box_theme, function(i) i["alpha",], classes = "box_colour", how = "unlist")[colour_names] / 255, 0)
colour_names <- paste0(options[["label"]], ".", colour_names, "!", alpha)
stopifnot(length(colour_names) == 4)
box_begin <- do.call(sprintf, as.list(c("\n\\cboxs[%s]{%s}{%s}{%s}{%s}\n", paste0(box_theme[["title"]], ""), colour_names)))
paste0(c("\n", xcolor_def, "\n", box_begin, x, "\n\\cboxe\n"), collapse = "\n")
}
#' Helper function to construct the colour definition in latex (xcolor)
#'
#' Builds up the string `\{RGB\}\{r, g, b\}` which needs to be completed: `\\definecolor\{name\}\{RGB\}\{r, g, b\}`
#'
#' @param x a vector containing the rgb colour values
to_latex_rgb <- function(x) {
do.call(sprintf, as.list(c("{RGB}{%s, %s, %s}", x)))
}