blob: e01b4dc68b0c188566faeee500089e39f35e7cad [file] [log] [blame]
Hao Zhue0a36a82015-11-23 15:35:20 -05001#' Rmarkdown Format
2#'
3#' @description If the export format of the Rmarkdown document exist,
4#'
5#' @importFrom rmarkdown metadata
6#'
7#' @export
8
9rmd_format <- function(){
10 rmd_output_metadata <- metadata$output
Hao Zhuc1f38412017-02-23 12:13:48 -050011 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 Zhu78e61222017-05-24 20:53:35 -040021#'
22#' @examples usepackage_latex("xcolor")
Hao Zhuc1f38412017-02-23 12:13:48 -050023#' @export
24usepackage_latex <- function(name, options = NULL) {
25 invisible(knit_meta_add(list(latex_dependency(name, options))))
Hao Zhue0a36a82015-11-23 15:35:20 -050026}
Hao Zhu62cdde52017-05-20 22:16:03 -040027
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.
30xml_tpart <- function(x, part) {
31 xchildren <- xml_children(x)
32 children_names <- xml_name(xchildren)
Hao Zhufc14c9b2017-05-22 14:03:22 -040033 if(!part %in% children_names) return(NULL)
Hao Zhu62cdde52017-05-20 22:16:03 -040034 return(xchildren[[which(children_names == part)]])
35}
36
37positions_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 Zhu73604282017-06-11 22:08:48 -040046
47latex_row_cells <- function(x) {
Hao Zhu78e43862019-01-07 17:37:30 -050048 stringr::str_split(x, " \\& ")
Hao Zhu73604282017-06-11 22:08:48 -040049}
Hao Zhu2a87e8e2017-06-14 15:49:33 -040050
Hao Zhu2ce42b92017-06-15 17:15:33 -040051regex_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 Zhu9b05ce12017-08-26 11:02:23 -040058 x <- gsub("\\[", "\\\\[", x)
59 x <- gsub("\\]", "\\\\]", x)
Hao Zhu2ce42b92017-06-15 17:15:33 -040060 x <- gsub("\\{", "\\\\{", x)
61 x <- gsub("\\}", "\\\\}", x)
62 x <- gsub("\\*", "\\\\*", x)
Hao Zhuf451dbb2017-08-22 16:51:38 -040063 x <- gsub("\\+", "\\\\+", x)
energien35f01b12017-10-14 21:06:21 +020064 x <- gsub("\\?", "\\\\?", x)
Hao Zhucfab7f12018-04-21 11:04:52 -040065 x <- gsub("\\|", "\\\\|", x)
Hao Zhu5d53dad2018-05-20 19:02:53 -040066 x <- gsub("\\^", "\\\\^", x)
Hao Zhu2ce42b92017-06-15 17:15:33 -040067 return(x)
68}
Hao Zhu96a50b52017-06-14 18:09:35 -040069
Hao Zhuf2dfd142017-07-24 14:43:28 -040070as_kable_xml <- function(x) {
Hao Zhuf01139c2017-08-03 09:08:20 -040071 out <- structure(as.character(x), format = "html", class = "knitr_kable")
Hao Zhuf2dfd142017-07-24 14:43:28 -040072 return(out)
73}
Hao Zhu9bab1532017-07-24 15:08:41 -040074
75read_kable_as_xml <- function(x) {
Hao Zhu0a98df02017-12-27 14:52:37 -050076 kable_html <- read_html(as.character(x), options = c("RECOVER", "NOERROR"))
Hao Zhu9bab1532017-07-24 15:08:41 -040077 xml_child(xml_child(kable_html, 1), 1)
78}
Hao Zhu356ca9f2017-09-05 16:10:26 -040079
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
85kableExtra_latex_packages <- function() {
86
Hao Zhu2a6256d2017-09-14 14:54:40 -040087 pkg_list <- paste0(" - ", latex_pkg_list())
Hao Zhu356ca9f2017-09-05 16:10:26 -040088
89 pkg_text <- paste0(
90 "header-includes:\n",
91 paste0(pkg_list, collapse = "\n")
92 )
93
94 cat(pkg_text)
95}
Hao Zhu2a6256d2017-09-14 14:54:40 -040096
97latex_pkg_list <- function() {
98 return(c(
99 "\\usepackage{booktabs}",
100 "\\usepackage{longtable}",
101 "\\usepackage{array}",
102 "\\usepackage{multirow}",
Hao Zhu2a6256d2017-09-14 14:54:40 -0400103 "\\usepackage{wrapfig}",
104 "\\usepackage{float}",
105 "\\usepackage{colortbl}",
106 "\\usepackage{pdflscape}",
107 "\\usepackage{tabu}",
Hao Zhu5ece06e2018-01-19 23:18:02 -0500108 "\\usepackage{threeparttable}",
Hao Zhu6107f372018-05-21 00:23:26 -0400109 "\\usepackage{threeparttablex}",
110 "\\usepackage[normalem]{ulem}",
antaldaniel719e7112018-05-30 21:40:32 +0200111 "\\usepackage[normalem]{ulem}",
112 "\\usepackage[utf8]{inputenc}",
Hao Zhuf1873a42019-01-07 15:57:01 -0500113 "\\usepackage{makecell}",
114 "\\usepackage{xcolor}"
Hao Zhu2a6256d2017-09-14 14:54:40 -0400115 ))
116}
Hao Zhu064990d2017-10-17 18:08:42 -0400117
118# Fix duplicated rows in LaTeX tables
119fix_duplicated_rows_latex <- function(kable_input, table_info) {
Hao Zhu745e5452019-01-22 02:59:05 -0500120 # dup_items <- table(table_info$contents)
121 # dup_items <- dup_items[dup_items != 1]
122 #
123 # for (di in seq(length(dup_items))) {
124 # dup_row <- names(dup_items[di])
125 # di_index <- which(table_info$contents == dup_row)
126 # for (i in seq(dup_items[di])) {
127 # new_row <- str_replace(
128 # dup_row, "(?<=\\s)([\\S]+[\\s]*)$",
129 # paste0("\\\\\\\\vphantom\\\\{", i, "\\\\} \\1"))
130 # kable_input <- sub(dup_row, new_row, kable_input)
131 # table_info$contents[di_index[i]] <- new_row
132 # }
133 # }
Hao Zhu064990d2017-10-17 18:08:42 -0400134 # Since sub/string_replace start from beginning, we count unique value from
135 # behind.
136 rev_contents <- rev(table_info$contents)
137 dup_index <- rev(ave(seq_along(rev_contents), rev_contents,
138 FUN = seq_along))
139 for (i in which(dup_index != 1)) {
140 dup_row <- table_info$contents[i]
141 empty_times <- dup_index[i] - 1
Leo83f05132018-05-10 14:12:16 +0800142 # insert empty_times before last non whitespace characters
Hao Zhubdc59ba2018-04-05 14:24:56 -0400143 new_row <- str_replace(
Leo83f05132018-05-10 14:12:16 +0800144 dup_row, "(?<=\\s)([\\S]+[\\s]*)$",
145 paste0("\\\\\\\\vphantom\\\\{", empty_times, "\\\\} \\1"))
146 kable_input <- sub(dup_row, new_row, kable_input)
Hao Zhu064990d2017-10-17 18:08:42 -0400147 table_info$contents[i] <- new_row
148 }
149 table_info$duplicated_rows <- FALSE
150 return(list(kable_input, table_info))
151}
152
Hao Zhu3fc0e882018-04-03 16:06:41 -0400153# Solve enc issue for LaTeX tables
154solve_enc <- function(x) {
jokorn84f10232019-11-11 13:34:16 +0100155 if (Encoding(x) == "UTF-8"){
156 out <- x
157 } else {
antaldaniel719e7112018-05-30 21:40:32 +0200158 #may behave differently based on Sys.setlocale settings with respect to characters
jokorn84f10232019-11-11 13:34:16 +0100159 out <- enc2utf8(as.character(base::format(x, trim = TRUE, justify = 'none')))
160 }
Hao Zhu905b31a2019-01-07 11:00:05 -0500161 mostattributes(out) <- attributes(x)
162 return(out)
Hao Zhu3fc0e882018-04-03 16:06:41 -0400163}
Hao Zhu064990d2017-10-17 18:08:42 -0400164
Hao Zhuf94a26f2018-04-05 17:42:55 -0400165input_escape <- function(x, latex_align) {
166 x <- escape_latex2(x)
167 x <- linebreak(x, align = latex_align, double_escape = TRUE)
168}
169