blob: 2ce27cbb803837aded7857f4b825181ffcc6a073 [file] [log] [blame]
Hao Zhu93e19a82017-08-26 10:48:49 -04001#' @export
2kable_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}