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) { |
Hao Zhu | 78e4386 | 2019-01-07 17:37:30 -0500 | [diff] [blame] | 48 | stringr::str_split(x, " \\& ") |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 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) |
energien | 35f01b1 | 2017-10-14 21:06:21 +0200 | [diff] [blame] | 64 | x <- gsub("\\?", "\\\\?", x) |
Hao Zhu | cfab7f1 | 2018-04-21 11:04:52 -0400 | [diff] [blame] | 65 | x <- gsub("\\|", "\\\\|", x) |
Hao Zhu | 5d53dad | 2018-05-20 19:02:53 -0400 | [diff] [blame] | 66 | x <- gsub("\\^", "\\\\^", x) |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 67 | return(x) |
| 68 | } |
Hao Zhu | 96a50b5 | 2017-06-14 18:09:35 -0400 | [diff] [blame] | 69 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 70 | as_kable_xml <- function(x) { |
Hao Zhu | f01139c | 2017-08-03 09:08:20 -0400 | [diff] [blame] | 71 | out <- structure(as.character(x), format = "html", class = "knitr_kable") |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 72 | return(out) |
| 73 | } |
Hao Zhu | 9bab153 | 2017-07-24 15:08:41 -0400 | [diff] [blame] | 74 | |
| 75 | read_kable_as_xml <- function(x) { |
Hao Zhu | 0a98df0 | 2017-12-27 14:52:37 -0500 | [diff] [blame] | 76 | kable_html <- read_html(as.character(x), options = c("RECOVER", "NOERROR")) |
Hao Zhu | 9bab153 | 2017-07-24 15:08:41 -0400 | [diff] [blame] | 77 | xml_child(xml_child(kable_html, 1), 1) |
| 78 | } |
Hao Zhu | 356ca9f | 2017-09-05 16:10:26 -0400 | [diff] [blame] | 79 | |
| 80 | #' LaTeX Packages |
| 81 | #' @description This function shows all LaTeX packages that is supposed to be |
| 82 | #' loaded for this package in a rmarkdown yaml format. |
| 83 | #' |
| 84 | #' @export |
| 85 | kableExtra_latex_packages <- function() { |
| 86 | |
Hao Zhu | 2a6256d | 2017-09-14 14:54:40 -0400 | [diff] [blame] | 87 | pkg_list <- paste0(" - ", latex_pkg_list()) |
Hao Zhu | 356ca9f | 2017-09-05 16:10:26 -0400 | [diff] [blame] | 88 | |
| 89 | pkg_text <- paste0( |
| 90 | "header-includes:\n", |
| 91 | paste0(pkg_list, collapse = "\n") |
| 92 | ) |
| 93 | |
| 94 | cat(pkg_text) |
| 95 | } |
Hao Zhu | 2a6256d | 2017-09-14 14:54:40 -0400 | [diff] [blame] | 96 | |
| 97 | latex_pkg_list <- function() { |
| 98 | return(c( |
| 99 | "\\usepackage{booktabs}", |
| 100 | "\\usepackage{longtable}", |
| 101 | "\\usepackage{array}", |
| 102 | "\\usepackage{multirow}", |
Hao Zhu | 2a6256d | 2017-09-14 14:54:40 -0400 | [diff] [blame] | 103 | "\\usepackage{wrapfig}", |
| 104 | "\\usepackage{float}", |
| 105 | "\\usepackage{colortbl}", |
| 106 | "\\usepackage{pdflscape}", |
| 107 | "\\usepackage{tabu}", |
Hao Zhu | 5ece06e | 2018-01-19 23:18:02 -0500 | [diff] [blame] | 108 | "\\usepackage{threeparttable}", |
Hao Zhu | 6107f37 | 2018-05-21 00:23:26 -0400 | [diff] [blame] | 109 | "\\usepackage{threeparttablex}", |
| 110 | "\\usepackage[normalem]{ulem}", |
antaldaniel | 719e711 | 2018-05-30 21:40:32 +0200 | [diff] [blame] | 111 | "\\usepackage[normalem]{ulem}", |
| 112 | "\\usepackage[utf8]{inputenc}", |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 113 | "\\usepackage{makecell}", |
| 114 | "\\usepackage{xcolor}" |
Hao Zhu | 2a6256d | 2017-09-14 14:54:40 -0400 | [diff] [blame] | 115 | )) |
| 116 | } |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 117 | |
| 118 | # Fix duplicated rows in LaTeX tables |
| 119 | fix_duplicated_rows_latex <- function(kable_input, table_info) { |
| 120 | # Since sub/string_replace start from beginning, we count unique value from |
| 121 | # behind. |
| 122 | rev_contents <- rev(table_info$contents) |
| 123 | dup_index <- rev(ave(seq_along(rev_contents), rev_contents, |
| 124 | FUN = seq_along)) |
| 125 | for (i in which(dup_index != 1)) { |
| 126 | dup_row <- table_info$contents[i] |
| 127 | empty_times <- dup_index[i] - 1 |
Leo | 83f0513 | 2018-05-10 14:12:16 +0800 | [diff] [blame] | 128 | # insert empty_times before last non whitespace characters |
Hao Zhu | bdc59ba | 2018-04-05 14:24:56 -0400 | [diff] [blame] | 129 | new_row <- str_replace( |
Leo | 83f0513 | 2018-05-10 14:12:16 +0800 | [diff] [blame] | 130 | dup_row, "(?<=\\s)([\\S]+[\\s]*)$", |
| 131 | paste0("\\\\\\\\vphantom\\\\{", empty_times, "\\\\} \\1")) |
| 132 | kable_input <- sub(dup_row, new_row, kable_input) |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 133 | table_info$contents[i] <- new_row |
| 134 | } |
| 135 | table_info$duplicated_rows <- FALSE |
| 136 | return(list(kable_input, table_info)) |
| 137 | } |
| 138 | |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 139 | # Solve enc issue for LaTeX tables |
| 140 | solve_enc <- function(x) { |
jokorn | 84f1023 | 2019-11-11 13:34:16 +0100 | [diff] [blame] | 141 | if (Encoding(x) == "UTF-8"){ |
| 142 | out <- x |
| 143 | } else { |
antaldaniel | 719e711 | 2018-05-30 21:40:32 +0200 | [diff] [blame] | 144 | #may behave differently based on Sys.setlocale settings with respect to characters |
jokorn | 84f1023 | 2019-11-11 13:34:16 +0100 | [diff] [blame] | 145 | out <- enc2utf8(as.character(base::format(x, trim = TRUE, justify = 'none'))) |
| 146 | } |
Hao Zhu | 905b31a | 2019-01-07 11:00:05 -0500 | [diff] [blame] | 147 | mostattributes(out) <- attributes(x) |
| 148 | return(out) |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 149 | } |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 150 | |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 151 | input_escape <- function(x, latex_align) { |
| 152 | x <- escape_latex2(x) |
| 153 | x <- linebreak(x, align = latex_align, double_escape = TRUE) |
| 154 | } |
| 155 | |