blob: dc715240432e56837ccc983155c489c233431541 [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) {
48 strsplit(x, " \\& ")
49}
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 Zhu2ce42b92017-06-15 17:15:33 -040065 return(x)
66}
Hao Zhu96a50b52017-06-14 18:09:35 -040067
Hao Zhuf2dfd142017-07-24 14:43:28 -040068as_kable_xml <- function(x) {
Hao Zhuf01139c2017-08-03 09:08:20 -040069 # tmp <- tempfile(fileext = ".xml")
70 # write_xml(x, tmp, options = c("no_declaration", "format_whitespace", "as_html"))
71 # out <- read_file(tmp)
72 # out <- structure(out, format = "html", class = "knitr_kable")
73 out <- structure(as.character(x), format = "html", class = "knitr_kable")
Hao Zhuf2dfd142017-07-24 14:43:28 -040074 return(out)
75}
Hao Zhu9bab1532017-07-24 15:08:41 -040076
77read_kable_as_xml <- function(x) {
78 kable_html <- read_html(as.character(x))
79 xml_child(xml_child(kable_html, 1), 1)
80}
Hao Zhu356ca9f2017-09-05 16:10:26 -040081
82#' LaTeX Packages
83#' @description This function shows all LaTeX packages that is supposed to be
84#' loaded for this package in a rmarkdown yaml format.
85#'
86#' @export
87kableExtra_latex_packages <- function() {
88
Hao Zhu2a6256d2017-09-14 14:54:40 -040089 pkg_list <- paste0(" - ", latex_pkg_list())
Hao Zhu356ca9f2017-09-05 16:10:26 -040090
91 pkg_text <- paste0(
92 "header-includes:\n",
93 paste0(pkg_list, collapse = "\n")
94 )
95
96 cat(pkg_text)
97}
Hao Zhu2a6256d2017-09-14 14:54:40 -040098
99latex_pkg_list <- function() {
100 return(c(
101 "\\usepackage{booktabs}",
102 "\\usepackage{longtable}",
103 "\\usepackage{array}",
104 "\\usepackage{multirow}",
105 "\\usepackage[table]{xcolor}",
106 "\\usepackage{wrapfig}",
107 "\\usepackage{float}",
108 "\\usepackage{colortbl}",
109 "\\usepackage{pdflscape}",
110 "\\usepackage{tabu}",
111 "\\usepackage{threeparttable}"
112 ))
113}