Hao Zhu | 93e19a8 | 2017-08-26 10:48:49 -0400 | [diff] [blame] | 1 | #' @export |
| 2 | kable_as_image_latex <- function(kable_input, width = NULL, |
| 3 | latex_header_includes = NULL, |
| 4 | keep_pdf = FALSE) { |
| 5 | temp_tex <- c( |
| 6 | "\\documentclass[border=1mm, preview]{standalone}", |
| 7 | "\\usepackage[active,tightpage]{preview}", |
| 8 | "\\usepackage{varwidth}", |
| 9 | "\\usepackage{booktabs}", |
| 10 | "\\usepackage{longtable}", |
| 11 | "\\usepackage{array}", |
| 12 | "\\usepackage{multirow}", |
| 13 | "\\usepackage[table]{xcolor}", |
| 14 | "\\usepackage{wrapfig}", |
| 15 | "\\usepackage{colortbl}", |
| 16 | "\\usepackage{graphicx}", |
| 17 | latex_header_includes, |
| 18 | "\\begin{document}", |
| 19 | as.character(kable_input), |
| 20 | "\\end{document}" |
| 21 | ) |
| 22 | temp_tex <- paste(temp_tex, collapse = "\n") |
| 23 | temp_file <- paste0("table_", format(Sys.time(), "%Y-%m-%d_%H:%M:%S")) |
| 24 | write_file(temp_tex, paste0(temp_file, ".tex")) |
| 25 | system(paste0("xelatex -interaction=batchmode ", temp_file, ".tex")) |
| 26 | temp_file_delete <- paste0(temp_file, c(".tex", ".aux", ".log")) |
| 27 | unlink(temp_file_delete) |
| 28 | |
| 29 | table_img_pdf <- image_read(paste0(temp_file, ".pdf"), density = 300) |
| 30 | if (!keep_pdf) { |
| 31 | unlink(paste0(temp_file, ".pdf")) |
| 32 | } |
| 33 | table_img <- image_convert(table_img_pdf, "png") |
| 34 | if (!is.null(width)) { |
| 35 | table_img <- image_scale(table_img, as.character(300*width)) |
| 36 | } |
| 37 | temp_img <- tempfile(fileext = ".png") |
| 38 | image_write(table_img, temp_img) |
| 39 | |
| 40 | include_graphics(temp_img) |
| 41 | } |