Merge branch 'master' of https://github.com/haozhu233/kableExtra
diff --git a/DESCRIPTION b/DESCRIPTION
index fcd7c8b..48dc40e 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -29,7 +29,8 @@
     xml2,
     rvest,
     rmarkdown (>= 1.6.0),
-    readr
+    readr,
+    magick
 Suggests:
     testthat
 VignetteBuilder: knitr
diff --git a/NAMESPACE b/NAMESPACE
index 17f7593..9a9826b 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -7,6 +7,7 @@
 export(collapse_rows)
 export(column_spec)
 export(group_rows)
+export(kable_as_image_latex)
 export(kable_styling)
 export(landscape)
 export(magic_mirror)
@@ -14,10 +15,13 @@
 export(row_spec)
 export(scroll_box)
 export(usepackage_latex)
+import(magick)
+importFrom(knitr,include_graphics)
 importFrom(knitr,knit_meta_add)
 importFrom(magrittr,"%>%")
 importFrom(readr,read_file)
 importFrom(readr,read_lines)
+importFrom(readr,write_file)
 importFrom(rmarkdown,latex_dependency)
 importFrom(rmarkdown,metadata)
 importFrom(rvest,html_table)
diff --git a/R/column_spec.R b/R/column_spec.R
index fc73b53..409ea0c 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -135,9 +135,6 @@
     message("Usually it is recommended to use column_spec before collapse_rows,",
             " especially in LaTeX, to get a desired result. ")
   }
-  if (!is.null(background)) {
-    warning("Column background color for LaTeX has not yet been implemented.")
-  }
   align_collapse <- ifelse(table_info$booktabs, "", "\\|")
   kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
 
diff --git a/R/kableExtra-package.R b/R/kableExtra-package.R
index 4690022..2664c4d 100644
--- a/R/kableExtra-package.R
+++ b/R/kableExtra-package.R
@@ -61,11 +61,12 @@
 #' xml_child xml_children xml_name xml_add_sibling xml_add_child xml_text
 #' xml_remove write_xml xml_text<-
 #' @importFrom rvest html_table
-#' @importFrom knitr knit_meta_add
+#' @importFrom knitr knit_meta_add include_graphics
 #' @importFrom rmarkdown latex_dependency
 #' @importFrom magrittr %>%
 #' @importFrom utils read.csv
-#' @importFrom readr read_lines read_file
+#' @importFrom readr read_lines read_file write_file
+#' @import magick
 #' @name kableExtra-package
 #' @aliases kableExtra
 #' @docType package
diff --git a/R/tex2image.R b/R/tex2image.R
new file mode 100644
index 0000000..2ce27cb
--- /dev/null
+++ b/R/tex2image.R
@@ -0,0 +1,41 @@
+#' @export
+kable_as_image_latex <- function(kable_input, width = NULL,
+                                 latex_header_includes = NULL,
+                                 keep_pdf = FALSE) {
+  temp_tex <- c(
+    "\\documentclass[border=1mm, preview]{standalone}",
+    "\\usepackage[active,tightpage]{preview}",
+    "\\usepackage{varwidth}",
+    "\\usepackage{booktabs}",
+    "\\usepackage{longtable}",
+    "\\usepackage{array}",
+    "\\usepackage{multirow}",
+    "\\usepackage[table]{xcolor}",
+    "\\usepackage{wrapfig}",
+    "\\usepackage{colortbl}",
+    "\\usepackage{graphicx}",
+    latex_header_includes,
+    "\\begin{document}",
+    as.character(kable_input),
+    "\\end{document}"
+  )
+  temp_tex <- paste(temp_tex, collapse = "\n")
+  temp_file <- paste0("table_", format(Sys.time(), "%Y-%m-%d_%H:%M:%S"))
+  write_file(temp_tex, paste0(temp_file, ".tex"))
+  system(paste0("xelatex -interaction=batchmode ", temp_file, ".tex"))
+  temp_file_delete <- paste0(temp_file, c(".tex", ".aux", ".log"))
+  unlink(temp_file_delete)
+
+  table_img_pdf <- image_read(paste0(temp_file, ".pdf"), density = 300)
+  if (!keep_pdf) {
+    unlink(paste0(temp_file, ".pdf"))
+  }
+  table_img <- image_convert(table_img_pdf, "png")
+  if (!is.null(width)) {
+    table_img <- image_scale(table_img, as.character(300*width))
+  }
+  temp_img <- tempfile(fileext = ".png")
+  image_write(table_img, temp_img)
+
+  include_graphics(temp_img)
+}
diff --git a/tests/visual_tests/column_row_spec_pdf.Rmd b/tests/visual_tests/column_row_spec_pdf.Rmd
index 52c1b0a..711c813 100644
--- a/tests/visual_tests/column_row_spec_pdf.Rmd
+++ b/tests/visual_tests/column_row_spec_pdf.Rmd
@@ -39,7 +39,7 @@
   )
 )
 
-kable(dt, "latex", booktabs = T, align = "r") %>%
+kable_input <- kable(dt, "latex", booktabs = T, align = "r") %>%
   column_spec(2, "3cm", bold = T, border_right = T) %>%
   column_spec(3, monospace = T)%>%
   column_spec(4, "3cm", italic = T, color = "red", border_left = T, background = "blue")  %>%
diff --git a/tests/visual_tests/kable_as_image_pdf.Rmd b/tests/visual_tests/kable_as_image_pdf.Rmd
new file mode 100644
index 0000000..51b4c16
--- /dev/null
+++ b/tests/visual_tests/kable_as_image_pdf.Rmd
@@ -0,0 +1,17 @@
+---
+title: "kable_as_image"
+author: "Hao"
+date: "8/22/2017"
+output:
+  word_document: default
+---
+
+```{r, fig.width=3}
+library(knitr)
+library(kableExtra)
+
+dt <- structure(list(unbeb = c("individuelle Bauweise", "Geschosswohnungsbau", "klassisches Gewerbe", "tertiäres Gewerbe", "Nichtbauland", "Summe unbebaute Grundstücke"
+), 2011 = c(" 87", " 29", " 10", " 25", "126", "277"), 2012 = c(" 77", " 26", " 18", " 12", "108", "241"), 2013 = c(" 95", " 49", " 22", " 4", " 91", "261"), 2014 = c("101", " 29", " 13", " 9", "122", "274"), 2015 = c(" 60", " 45", " 13", " 4", "103", "225"), 2016 = c("127", " 36", " 11", " 5", " 93", "272")), .Names = c("unbeb", "2011", "2012", "2013", "2014", "2015", "2016"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
+
+kable(Daten_Tabelle_unbeb, format = "latex", booktabs=TRUE, caption="hallo") %>% kable_styling(latex_options=c("striped","scale_down", "hold_position"))
+```
diff --git a/tests/visual_tests/kable_as_image_pdf.docx b/tests/visual_tests/kable_as_image_pdf.docx
new file mode 100644
index 0000000..201447c
--- /dev/null
+++ b/tests/visual_tests/kable_as_image_pdf.docx
Binary files differ