Merge pull request #50 from robshep/master

Add option for using float package with [H] float specifier
diff --git a/DESCRIPTION b/DESCRIPTION
index 6ad0110..c1851fd 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/add_footnote.R b/R/add_footnote.R
index 3e1b130..abb618a 100644
--- a/R/add_footnote.R
+++ b/R/add_footnote.R
@@ -99,6 +99,7 @@
     label <- escape_latex(label)
     label <- gsub("\\\\", "\\\\\\\\", label)
 
+    export <- enc2utf8(export)
     table_info <- magic_mirror(input)
     if (table_info$tabular == "longtable") {
       if (notation != "number") {
diff --git a/R/add_header_above.R b/R/add_header_above.R
index 95e587d..e716b69 100644
--- a/R/add_header_above.R
+++ b/R/add_header_above.R
@@ -125,7 +125,7 @@
   new_header <- paste0(new_header_split[1], "\n", new_header_split[2])
   out <- sub(hline_type,
              paste0(hline_type, "\n", new_header),
-             as.character(kable_input))
+             enc2utf8(as.character(kable_input)))
   out <- structure(out, format = "latex", class = "knitr_kable")
   # new_header_row <- latex_contents_escape(new_header_split[1])
   if (is.null(table_info$new_header_row)) {
diff --git a/R/add_indent.R b/R/add_indent.R
index 4ae84da..8932eaa 100644
--- a/R/add_indent.R
+++ b/R/add_indent.R
@@ -35,7 +35,7 @@
          "add_indent_latex.")
   }
 
-  out <- kable_input
+  out <- enc2utf8(kable_input)
   for (i in positions) {
     rowtext <- table_info$contents[i + 1]
     out <- sub(rowtext, latex_indent_unit(rowtext), out, perl = TRUE)
diff --git a/R/collapse_rows.R b/R/collapse_rows.R
index 3b13997..8ba1250 100644
--- a/R/collapse_rows.R
+++ b/R/collapse_rows.R
@@ -89,7 +89,7 @@
   if (is.null(columns)) {
     columns <- seq(1, table_info$ncol)
   }
-  out <- as.character(kable_input)
+  out <- enc2utf8(as.character(kable_input))
   contents <- table_info$contents
   kable_dt <- kable_dt_latex(contents)
   collapse_matrix <- collapse_row_matrix(kable_dt, columns, html = F)
diff --git a/R/column_spec.R b/R/column_spec.R
index 3af165b..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)
 
@@ -147,7 +144,8 @@
 
   kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
 
-  out <- sub(kable_align_old, kable_align_new, as.character(kable_input),
+  out <- sub(kable_align_old, kable_align_new,
+             enc2utf8(as.character(kable_input)),
              perl = T)
   out <- structure(out, format = "latex", class = "knitr_kable")
   if (!is.null(width)) {
diff --git a/R/group_rows.R b/R/group_rows.R
index 47a02f0..1d848b7 100644
--- a/R/group_rows.R
+++ b/R/group_rows.R
@@ -85,7 +85,7 @@
 group_rows_latex <- function(kable_input, group_label, start_row, end_row,
                              gap_space, escape) {
   table_info <- magic_mirror(kable_input)
-  out <- kable_input
+  out <- enc2utf8(as.character(kable_input))
 
   if (escape) {
     group_label <- escape_latex(group_label)
@@ -110,6 +110,7 @@
   }
   out <- sub(rowtext, new_rowtext, out)
   out <- gsub("\\\\addlinespace\n", "", out)
+  out <- structure(out, format = "latex", class = "knitr_kable")
   table_info$group_rows_used <- TRUE
   attr(out, "kable_meta") <- table_info
   out <- add_indent(out, seq(start_row, end_row))
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/kable_styling.R b/R/kable_styling.R
index c3d24cc..c957f5a 100644
--- a/R/kable_styling.R
+++ b/R/kable_styling.R
@@ -188,7 +188,7 @@
   repeat_header_method <- match.arg(repeat_header_method)
 
   out <- NULL
-  out <- as.character(kable_input)
+  out <- enc2utf8(as.character(kable_input))
   table_info <- magic_mirror(kable_input)
 
   if ("striped" %in% latex_options) {
diff --git a/R/landscape.R b/R/landscape.R
index 75d9255..fd7789e 100644
--- a/R/landscape.R
+++ b/R/landscape.R
@@ -28,7 +28,9 @@
   kable_attrs <- attributes(kable_input)
   usepackage_latex("pdflscape")
   out <- paste0(
-    "\n\\begin{landscape}", kable_input, "\n\\end{landscape}"
+    "\n\\begin{landscape}",
+    enc2utf8(as.character(kable_input)),
+    "\n\\end{landscape}"
   )
 
   if (!is.null(margin)) {
diff --git a/R/row_spec.R b/R/row_spec.R
index 060c27f..1d5d500 100644
--- a/R/row_spec.R
+++ b/R/row_spec.R
@@ -114,7 +114,8 @@
     new_row <- paste0("\\\\rowcolor{", background, "}  ", new_row)
   }
 
-  out <- sub(target_row, new_row, as.character(kable_input), perl = T)
+  out <- sub(target_row, new_row, enc2utf8(as.character(kable_input)),
+             perl = T)
   out <- structure(out, format = "latex", class = "knitr_kable")
   attr(out, "kable_meta") <- table_info
   return(out)
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/R/util.R b/R/util.R
index 7f54e2a..7b7c158 100644
--- a/R/util.R
+++ b/R/util.R
@@ -55,8 +55,8 @@
   x <- gsub("\\$", "\\\\\\$", x)
   x <- gsub("\\(", "\\\\(", x)
   x <- gsub("\\)", "\\\\)", x)
-  x <- gsub("\\[", "\\\\]", x)
-  x <- gsub("\\[", "\\\\]", x)
+  x <- gsub("\\[", "\\\\[", x)
+  x <- gsub("\\]", "\\\\]", x)
   x <- gsub("\\{", "\\\\{", x)
   x <- gsub("\\}", "\\\\}", x)
   x <- gsub("\\*", "\\\\*", x)
diff --git a/docs/awesome_table_in_pdf.Rmd b/docs/awesome_table_in_pdf.Rmd
index f6e73e3..7bb5585 100644
--- a/docs/awesome_table_in_pdf.Rmd
+++ b/docs/awesome_table_in_pdf.Rmd
@@ -185,7 +185,7 @@
 ## Group rows via labeling
 Sometimes we want a few rows of the table being grouped together. They might be items under the same topic (e.g., animals in one species) or just different data groups for a categorical variable (e.g., age < 40, age > 40). With the new function `group_rows()` in `kableExtra`, this kind of task can be completed in one line. Please see the example below. Note that when you count for the start/end rows of the group, you don't need to count for the header rows nor other group label rows. You only need to think about the row numbers in the "original R dataframe".
 ```{r}
-kable(mtcars[1:10, 1:6], caption = "Group Rows", booktabs = T) %>%
+kable(mtcars[1:10, 1:6],"latex", caption = "Group Rows", booktabs = T) %>%
   kable_styling() %>%
   group_rows("Group 1", 4, 7) %>%
   group_rows("Group 2", 8, 10)
diff --git a/docs/awesome_table_in_pdf.pdf b/docs/awesome_table_in_pdf.pdf
index f7bace8..6e3ddd2 100644
--- a/docs/awesome_table_in_pdf.pdf
+++ b/docs/awesome_table_in_pdf.pdf
Binary files differ
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