remove xcolor latex dependency
diff --git a/R/save_kable.R b/R/save_kable.R
index 5a5cbc0..85ed54a 100644
--- a/R/save_kable.R
+++ b/R/save_kable.R
@@ -4,35 +4,59 @@
#' kableExtra
#' @param file save to files. If the input table is in HTML and the output file
#' ends with `.png`, `.pdf` and `.jpeg`, `webshot` will be used to do the
-#' conversion
-#'
+#' conversion.
#' @param bs_theme Which Bootstrap theme to use
#' @param self_contained Will the files be self-contained?
-#' @param ... Additional variables being passed to `webshot::webshot`.`
+#' @param extra_dependencies Additional HTML dependencies. For example,
+#' `list(`
+#' @param ... Additional variables being passed to `webshot::webshot`. This
+#' is for HTML only.
+#' @param latex_header_includes A character vector of extra LaTeX header stuff.
+#' Each element is a row. You can have things like
+#' `c("\\\\usepackage{threeparttable}", "\\\\usepackage{icons}")` You could
+#' probably add your language package here if you use non-English text in your
+#' table, such as `\\\\usepackage[magyar]{babel}`.
+#' @param keep_tex A T/F option to control if the latex file that is initially created
+#' should be kept. Default is `FALSE`.
#'
#' @export
save_kable <- function(x, file,
- bs_theme = "simplex", self_contained = TRUE, ...) {
+ bs_theme = "simplex", self_contained = TRUE,
+ extra_dependencies = NULL, ...,
+ latex_header_includes = NULL, keep_tex = FALSE) {
if (attr(x, "format") == "latex") {
- return(save_kable_latex(x, file))
+ return(save_kable_latex(x, file, latex_header_includes, keep_tex))
}
- return(save_kable_html(x, file, bs_theme, self_contained, ...))
+ return(save_kable_html(x, file, bs_theme, self_contained,
+ extra_dependencies, ...))
}
-save_kable_html <- function(x, file, bs_theme, self_contained, ...) {
- html_header <- htmltools::tags$head(
+save_kable_html <- function(x, file, bs_theme, self_contained,
+ extra_dependencies, ...) {
+ dependencies <- list(
rmarkdown::html_dependency_jquery(),
rmarkdown::html_dependency_bootstrap(theme = bs_theme),
html_dependency_kePrint()
)
+ if (!is.null(extra_dependencies)) {
+ dependencies <- append(dependencies, extra_dependencies)
+ }
+
+ html_header <- htmltools::tag("head", dependencies)
html_table <- htmltools::HTML(as.character(x))
html_result <- htmltools::tagList(html_header, html_table)
# Use webshot if necessary
if (tools::file_ext(file) %in% c("png", "jpg", "jpeg", "pdf")) {
+ message("Putting together a HTML file...")
file_html <- paste0(tools::file_path_sans_ext(file), ".html")
htmltools::save_html(html_result, file = file_html)
+ message("Converting HTML to ", tools::file_ext(file), "...")
webshot::webshot(file_html, file, ...)
+ message("Done. ")
+ if (tools::file_ext(file) == "pdf") {
+ message("Note that HTML color may not be displayed on PDF properly.")
+ }
unlink(file_html)
unlink("lib", recursive = TRUE)
} else {
@@ -44,6 +68,53 @@
}
}
-save_kable_latex <- function(x, file) {
+save_kable_latex <- function(x, file, latex_header_includes, keep_tex) {
+ temp_tex <- c(
+ "\\documentclass[border=1mm, preview]{standalone}",
+ "\\usepackage[active,tightpage]{preview}",
+ "\\usepackage{varwidth}",
+ "\\usepackage{amssymb, amsmath}",
+ "\\usepackage{ifxetex,ifluatex}",
+ "\\usepackage{fixltx2e}",
+ "\\usepackage{polyglossia}",
+ "\\setmainlanguage{$mainlang$}",
+ latex_pkg_list(),
+ "\\usepackage{graphicx}",
+ "\\usepackage{mathspec}",
+ "\\usepackage{xltxtra,xunicode}",
+ latex_header_includes,
+ "\\begin{document}",
+ solve_enc(x),
+ "\\end{document}"
+ )
+ temp_tex <- paste(temp_tex, collapse = "\n")
+ temp_tex_file <- tools::file_path_sans_ext(file)
+ writeLines(temp_tex, temp_tex_file, useBytes = T)
+ system(paste0("xelatex -interaction=batchmode ", temp_tex_file))
+ if (!keep_tex) {
+ temp_file_delete <- paste0(tools::file_path_sans_ext(file),
+ c(".tex", ".aux", ".log"))
+ unlink(temp_file_delete)
+ }
+
+ table_img_pdf <- try(magick::image_read(paste0(temp_file, ".pdf"),
+ density = density),
+ silent = T)
+ if (class(table_img_pdf) == "try-error") {
+ stop("Ghostscript is required to read PDF on windows. ",
+ "Please download it here: https://ghostscript.com/")
+ }
+ if (!keep_pdf) {
+ unlink(paste0(temp_file, ".pdf"))
+ }
+ table_img <- magick::image_convert(table_img_pdf, file_format)
+ if (!is.null(filename)) {
+ temp_img <- paste0(filename, ".", file_format)
+ } else {
+ temp_img <- tempfile(fileext = paste0(".", file_format))
+ }
+ magick::image_write(table_img, temp_img)
+
+ include_graphics(temp_img)
}