blob: 34eaf3d4a900980352497d7f9c744072e4096827 [file] [log] [blame]
Hao Zhu2a64dc62017-08-28 10:57:57 -04001#' Convert a LaTeX table to an image and place it in a rmarkdown document
2#'
3#' @description This is a LaTeX-only function. This function will render the
4#' raw LaTeX code (could be codes generated by other table packages like
5#' `xtable`) to generate a table, convert it to an image and put it back to a
6#' rmarkdown environment. It is a "better than nothing" solution to print high
7#' quality tables in rmarkdown Word document. By using this, you need to take
8#' the responsibility of explaining to your collaborators why they can't edit
9#' the tables they see in the Word document they received. 😂
10#'
11#' Also, if a filename is provided, user has the option to "save" the table to
12#' an image file like `ggplot2::ggsave()`.
13#'
14#' The idea of this function was coming from [this StackOverflow question](https://stackoverflow.com/questions/44711313/save-rmarkdowns-report-tables-and-figures-to-file).
15#' The approach was learned and adopted from the [texpreview](https://github.com/metrumresearchgroup/texPreview)
16#' package, which allows you to preview the results of TeX code in the Viewer panel.
17#'
18#' @param kable_input Raw LaTeX code to generate a table. It doesn't have to
19#' came from `kable` or `kableExtra`.
20#' @param filename Character String. If specified, the image will be saved under
21#' the specified (path &) name. You don't need to put file format like ".png"
22#' here.
23#' @param file_format Character String to specify image format, such as `png`,
24#' `jpeg`, `gif`, `tiff`, etc. Default is `png`.
25#' @param latex_header_includes A character vector of extra LaTeX header stuff.
26#' Each element is a row. You can have things like
27#' `c("\\usepackage{threeparttable}", "\\usepackage{icons}")`
28#' @param keep_pdf A T/F option to control if the mid-way standalone pdf should
29#' be kept. Default is `FALSE`.
30#'
31#' @examples kable_as_image(kable(mtcars, "latex"), "mtcars")
32#' @export
33kable_as_image <- function(kable_input, filename = NULL,
34 file_format = "png",
35 latex_header_includes = NULL,
36 keep_pdf = FALSE) {
37 temp_tex <- c(
38 "\\documentclass[border=1mm, preview]{standalone}",
39 "\\usepackage[active,tightpage]{preview}",
40 "\\usepackage{varwidth}",
41 "\\usepackage{amssymb,amsmath}",
42 "\\usepackage{ifxetex,ifluatex}",
43 "\\usepackage{fixltx2e}",
44 "\\usepackage{polyglossia}",
45 "\\setmainlanguage{$mainlang$}",
46 "\\usepackage{booktabs}",
47 "\\usepackage{longtable}",
48 "\\usepackage{array}",
49 "\\usepackage{multirow}",
50 "\\usepackage[table]{xcolor}",
51 "\\usepackage{wrapfig}",
52 "\\usepackage{colortbl}",
53 "\\usepackage{graphicx}",
54 "\\usepackage{mathspec}",
55 "\\usepackage{xltxtra,xunicode}",
56 latex_header_includes,
57 "\\begin{document}",
58 enc2utf8(as.character(kable_input)),
59 "\\end{document}"
60 )
61 temp_tex <- paste(temp_tex, collapse = "\n")
62 temp_file <- paste0("table_", format(Sys.time(), "%Y-%m-%d_%H:%M:%S"))
63 write_file(temp_tex, paste0(temp_file, ".tex"))
64 system(paste0("xelatex -interaction=batchmode ", temp_file, ".tex"))
65 temp_file_delete <- paste0(temp_file, c(".tex", ".aux", ".log"))
66 unlink(temp_file_delete)
67
68 table_img_pdf <- image_read(paste0(temp_file, ".pdf"), density = 300)
69 if (!keep_pdf) {
70 unlink(paste0(temp_file, ".pdf"))
71 }
72 table_img <- image_convert(table_img_pdf, file_format)
73 if (!is.null(filename)) {
74 temp_img <- paste0(filename, ".", file_format)
75 } else {
76 temp_img <- tempfile(fileext = paste0(".", file_format))
77 }
78 image_write(table_img, temp_img)
79
80 include_graphics(temp_img)
81}