Hao Zhu | e0a36a8 | 2015-11-23 15:35:20 -0500 | [diff] [blame] | 1 | #' Rmarkdown Format |
| 2 | #' |
| 3 | #' @description If the export format of the Rmarkdown document exist, |
| 4 | #' |
| 5 | #' @importFrom rmarkdown metadata |
| 6 | #' |
| 7 | #' @export |
| 8 | |
| 9 | rmd_format <- function(){ |
| 10 | rmd_output_metadata <- metadata$output |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 11 | return(names(rmd_output_metadata)) |
| 12 | } |
| 13 | |
| 14 | #' Load a LaTeX package |
| 15 | #' |
| 16 | #' @description Load a LaTeX package using R code. Just like `\\usepackage{}` |
| 17 | #' in LaTeX |
| 18 | #' |
| 19 | #' @param name The LaTeX package name |
| 20 | #' @param options The LaTeX options for the package |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 21 | #' |
| 22 | #' @examples usepackage_latex("xcolor") |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 23 | #' @export |
| 24 | usepackage_latex <- function(name, options = NULL) { |
| 25 | invisible(knit_meta_add(list(latex_dependency(name, options)))) |
Hao Zhu | e0a36a8 | 2015-11-23 15:35:20 -0500 | [diff] [blame] | 26 | } |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 27 | |
| 28 | # Find the right xml section. Since xml_child + search name will result in a |
| 29 | # crash (at least on my machine), here is a helper function. |
| 30 | xml_tpart <- function(x, part) { |
| 31 | xchildren <- xml_children(x) |
| 32 | children_names <- xml_name(xchildren) |
Hao Zhu | fc14c9b | 2017-05-22 14:03:22 -0400 | [diff] [blame] | 33 | if(!part %in% children_names) return(NULL) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 34 | return(xchildren[[which(children_names == part)]]) |
| 35 | } |
| 36 | |
| 37 | positions_corrector <- function(positions, group_header_rows, n_row) { |
| 38 | pc_matrix <- data.frame(row_id = 1:n_row) |
| 39 | pc_matrix$group_header <- pc_matrix$row_id %in% group_header_rows |
| 40 | pc_matrix$adj <- cumsum(pc_matrix$group_header) |
| 41 | pc_matrix$old_id <- cumsum(!pc_matrix$group_header) |
| 42 | pc_matrix$old_id[duplicated(pc_matrix$old_id)] <- NA |
| 43 | adjust_numbers <- pc_matrix$adj[pc_matrix$old_id %in% positions] |
| 44 | return(adjust_numbers + positions) |
| 45 | } |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 46 | |
| 47 | latex_row_cells <- function(x) { |
| 48 | strsplit(x, " \\& ") |
| 49 | } |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 50 | |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 51 | regex_escape <- function(x, double_backslash = FALSE) { |
| 52 | if (double_backslash) { |
| 53 | x <- gsub("\\\\", "\\\\\\\\", x) |
| 54 | } |
| 55 | x <- gsub("\\$", "\\\\\\$", x) |
| 56 | x <- gsub("\\(", "\\\\(", x) |
| 57 | x <- gsub("\\)", "\\\\)", x) |
Hao Zhu | 9b05ce1 | 2017-08-26 11:02:23 -0400 | [diff] [blame] | 58 | x <- gsub("\\[", "\\\\[", x) |
| 59 | x <- gsub("\\]", "\\\\]", x) |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 60 | x <- gsub("\\{", "\\\\{", x) |
| 61 | x <- gsub("\\}", "\\\\}", x) |
| 62 | x <- gsub("\\*", "\\\\*", x) |
Hao Zhu | f451dbb | 2017-08-22 16:51:38 -0400 | [diff] [blame] | 63 | x <- gsub("\\+", "\\\\+", x) |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 64 | return(x) |
| 65 | } |
Hao Zhu | 96a50b5 | 2017-06-14 18:09:35 -0400 | [diff] [blame] | 66 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 67 | as_kable_xml <- function(x) { |
Hao Zhu | f01139c | 2017-08-03 09:08:20 -0400 | [diff] [blame] | 68 | # tmp <- tempfile(fileext = ".xml") |
| 69 | # write_xml(x, tmp, options = c("no_declaration", "format_whitespace", "as_html")) |
| 70 | # out <- read_file(tmp) |
| 71 | # out <- structure(out, format = "html", class = "knitr_kable") |
| 72 | out <- structure(as.character(x), format = "html", class = "knitr_kable") |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 73 | return(out) |
| 74 | } |
Hao Zhu | 9bab153 | 2017-07-24 15:08:41 -0400 | [diff] [blame] | 75 | |
| 76 | read_kable_as_xml <- function(x) { |
| 77 | kable_html <- read_html(as.character(x)) |
| 78 | xml_child(xml_child(kable_html, 1), 1) |
| 79 | } |
Hao Zhu | 356ca9f | 2017-09-05 16:10:26 -0400 | [diff] [blame] | 80 | |
| 81 | #' LaTeX Packages |
| 82 | #' @description This function shows all LaTeX packages that is supposed to be |
| 83 | #' loaded for this package in a rmarkdown yaml format. |
| 84 | #' |
| 85 | #' @export |
| 86 | kableExtra_latex_packages <- function() { |
| 87 | |
Hao Zhu | 2a6256d | 2017-09-14 14:54:40 -0400 | [diff] [blame] | 88 | pkg_list <- paste0(" - ", latex_pkg_list()) |
Hao Zhu | 356ca9f | 2017-09-05 16:10:26 -0400 | [diff] [blame] | 89 | |
| 90 | pkg_text <- paste0( |
| 91 | "header-includes:\n", |
| 92 | paste0(pkg_list, collapse = "\n") |
| 93 | ) |
| 94 | |
| 95 | cat(pkg_text) |
| 96 | } |
Hao Zhu | 2a6256d | 2017-09-14 14:54:40 -0400 | [diff] [blame] | 97 | |
| 98 | latex_pkg_list <- function() { |
| 99 | return(c( |
| 100 | "\\usepackage{booktabs}", |
| 101 | "\\usepackage{longtable}", |
| 102 | "\\usepackage{array}", |
| 103 | "\\usepackage{multirow}", |
| 104 | "\\usepackage[table]{xcolor}", |
| 105 | "\\usepackage{wrapfig}", |
| 106 | "\\usepackage{float}", |
| 107 | "\\usepackage{colortbl}", |
| 108 | "\\usepackage{pdflscape}", |
| 109 | "\\usepackage{tabu}", |
| 110 | "\\usepackage{threeparttable}" |
| 111 | )) |
| 112 | } |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame^] | 113 | |
| 114 | latex_color <- function(color) { |
| 115 | if (substr(color, 1, 1) != "#") { |
| 116 | return(paste0("{", color, "}")) |
| 117 | } else { |
| 118 | color <- sub("#", "", color) |
| 119 | if (nchar(color) == 8) color <- substr(color, 1, 6) |
| 120 | return(paste0("[HTML]{", color, "}")) |
| 121 | } |
| 122 | } |