| 1 |
#' Define a LaTeX package dependency |
| 2 |
#' @param name The LaTeX package name |
| 3 |
#' @param options The LaTeX options for the package |
| 4 |
#' @param extra_lines LaTeX code related to the package added to the preamble |
| 5 |
#' @export |
| 6 |
latex_dependency <- function(name, options = NULL, extra_lines = NULL) { |
| 7 |
output <- list(name = name, options = options, extra_lines = extra_lines) |
| 8 |
class(output) <- "latex_dependency" |
| 9 |
validate_latex_dependency(output) |
| 10 |
} |
| 11 |
|
| 12 |
#' Provide common LaTeX dependencies |
| 13 |
#' |
| 14 |
#' These functions provide common LaTeX dependencies (e.g. tikz) |
| 15 |
#' for R Markdown formats that use LaTeX. |
| 16 |
#' |
| 17 |
#' @inheritParams latex_dependency |
| 18 |
#' @name latex-dependencies |
| 19 |
NULL |
| 20 |
|
| 21 |
# Create an LaTeX dependency for tikz |
| 22 |
#' @rdname latex-dependencies |
| 23 |
#' @param libraries A character vector of tikz libraries to load |
| 24 |
#' @export |
| 25 |
latex_dependency_tikz <- function(libraries, options = NULL, extra_lines = NULL) { |
| 26 |
libraries <- sprintf("\\usetikzlibrary{%s}", paste(libraries, collapse = ", ")) |
| 27 |
latex_dependency("tikz", options = options, extra_lines = c(libraries, extra_lines)) |
| 28 |
} |
| 29 |
|
| 30 |
latex_dependencies <- function(x = list()) { |
| 31 |
if (length(x) == 0) return() |
| 32 |
if (is_latex_dependency(x)) return(list(x)) |
| 33 |
nms <- names(x) |
| 34 |
if (is.list(x)) { |
| 35 |
if (is.null(nms)) return(x) # assume it is just a list of latex_dependency() |
| 36 |
# turn the named list to a named character vector |
| 37 |
x <- unlist(lapply(x, paste, collapse = ', ')) |
| 38 |
} |
| 39 |
if (is.character(x)) { |
| 40 |
if (is.null(nms)) { |
| 41 |
lapply(x, latex_dependency) |
| 42 |
} else { |
| 43 |
mapply(latex_dependency, nms, x, SIMPLIFY = FALSE, USE.NAMES = FALSE) |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
# return the LaTeX dependencies as a string suitable for inclusion |
| 49 |
# in the head of a document |
| 50 |
latex_dependencies_as_string <- function(dependencies) { |
| 51 |
lines <- sapply(dependencies, function(dep) { |
| 52 |
opts <- paste(dep$options, collapse = ",") |
| 53 |
if (opts != "") opts <- paste0("[", opts, "]") |
| 54 |
# \\usepackage[opt1,opt2]{pkgname} |
| 55 |
pkg <- paste0("\\usepackage", opts, "{", dep$name, "}") |
| 56 |
one_string(c(pkg, dep$extra_lines)) |
| 57 |
}) |
| 58 |
one_string(unique(lines)) |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
# flattens an arbitrarily nested list and returns all of the latex_dependency |
| 63 |
# objects it contains |
| 64 |
flatten_latex_dependencies <- function(knit_meta) { |
| 65 |
flatten_dependencies(knit_meta, is_latex_dependency) |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
# check class of passed list for 'latex_dependency' |
| 70 |
is_latex_dependency <- function(list) { |
| 71 |
inherits(list, "latex_dependency") |
| 72 |
} |
| 73 |
|
| 74 |
# validate that the passed list is a correctly formed latex_dependency |
| 75 |
validate_latex_dependency <- function(list) { |
| 76 |
|
| 77 |
# ensure it's the right class |
| 78 |
if (!is_latex_dependency(list)) |
| 79 |
stop2("passed object is not of class latex_dependency") |
| 80 |
|
| 81 |
# validate required fields |
| 82 |
if (is.null(list$name)) |
| 83 |
stop2("name (package name) for latex_dependency not provided") |
| 84 |
list |
| 85 |
} |
| 86 |
|
| 87 |
# check if the passed knit_meta has any latex dependencies |
| 88 |
has_latex_dependencies <- function(knit_meta) { |
| 89 |
has_dependencies(knit_meta, "latex_dependency") |
| 90 |
} |