Complete documentation for kable_as_image
diff --git a/NAMESPACE b/NAMESPACE
index 9a9826b..d20c746 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -7,7 +7,7 @@
 export(collapse_rows)
 export(column_spec)
 export(group_rows)
-export(kable_as_image_latex)
+export(kable_as_image)
 export(kable_styling)
 export(landscape)
 export(magic_mirror)
diff --git a/R/kable_as_image.R b/R/kable_as_image.R
new file mode 100644
index 0000000..34eaf3d
--- /dev/null
+++ b/R/kable_as_image.R
@@ -0,0 +1,81 @@
+#' Convert a LaTeX table to an image and place it in a rmarkdown document
+#'
+#' @description This is a LaTeX-only function. This function will render the
+#' raw LaTeX code (could be codes generated by other table packages like
+#' `xtable`) to generate a table, convert it to an image and put it back to a
+#' rmarkdown environment. It is a "better than nothing" solution to print high
+#' quality tables in rmarkdown Word document. By using this, you need to take
+#' the responsibility of explaining to your collaborators why they can't edit
+#' the tables they see in the Word document they received. 😂
+#'
+#' Also, if a filename is provided, user has the option to "save" the table to
+#' an image file like `ggplot2::ggsave()`.
+#'
+#' The idea of this function was coming from [this StackOverflow question](https://stackoverflow.com/questions/44711313/save-rmarkdowns-report-tables-and-figures-to-file).
+#' The approach was learned and adopted from the [texpreview](https://github.com/metrumresearchgroup/texPreview)
+#' package, which allows you to preview the results of TeX code in the Viewer panel.
+#'
+#' @param kable_input Raw LaTeX code to generate a table. It doesn't have to
+#' came from `kable` or `kableExtra`.
+#' @param filename Character String. If specified, the image will be saved under
+#' the specified (path &) name. You don't need to put file format like ".png"
+#' here.
+#' @param file_format Character String to specify image format, such as `png`,
+#' `jpeg`, `gif`, `tiff`, etc. Default is `png`.
+#' @param latex_header_includes A character vector of extra LaTeX header stuff.
+#' Each element is a row. You can have things like
+#' `c("\\usepackage{threeparttable}", "\\usepackage{icons}")`
+#' @param keep_pdf A T/F option to control if the mid-way standalone pdf should
+#' be kept. Default is `FALSE`.
+#'
+#' @examples kable_as_image(kable(mtcars, "latex"), "mtcars")
+#' @export
+kable_as_image <- function(kable_input, filename = NULL,
+                           file_format = "png",
+                           latex_header_includes = NULL,
+                           keep_pdf = FALSE) {
+  temp_tex <- c(
+    "\\documentclass[border=1mm, preview]{standalone}",
+    "\\usepackage[active,tightpage]{preview}",
+    "\\usepackage{varwidth}",
+    "\\usepackage{amssymb,amsmath}",
+    "\\usepackage{ifxetex,ifluatex}",
+    "\\usepackage{fixltx2e}",
+    "\\usepackage{polyglossia}",
+    "\\setmainlanguage{$mainlang$}",
+    "\\usepackage{booktabs}",
+    "\\usepackage{longtable}",
+    "\\usepackage{array}",
+    "\\usepackage{multirow}",
+    "\\usepackage[table]{xcolor}",
+    "\\usepackage{wrapfig}",
+    "\\usepackage{colortbl}",
+    "\\usepackage{graphicx}",
+    "\\usepackage{mathspec}",
+    "\\usepackage{xltxtra,xunicode}",
+    latex_header_includes,
+    "\\begin{document}",
+    enc2utf8(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, file_format)
+  if (!is.null(filename)) {
+    temp_img <- paste0(filename, ".", file_format)
+  } else {
+    temp_img <- tempfile(fileext = paste0(".", file_format))
+  }
+  image_write(table_img, temp_img)
+
+  include_graphics(temp_img)
+}
diff --git a/R/kable_styling.R b/R/kable_styling.R
index c957f5a..a4fed07 100644
--- a/R/kable_styling.R
+++ b/R/kable_styling.R
@@ -265,6 +265,7 @@
 }
 
 styling_latex_HOLD_position <- function(x) {
+  usepackage_latex("float")
   sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[H]", x)
 }
 
diff --git a/R/tex2image.R b/R/tex2image.R
deleted file mode 100644
index 2ce27cb..0000000
--- a/R/tex2image.R
+++ /dev/null
@@ -1,41 +0,0 @@
-#' @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/man/kable_as_image.Rd b/man/kable_as_image.Rd
new file mode 100644
index 0000000..ac0e71e
--- /dev/null
+++ b/man/kable_as_image.Rd
@@ -0,0 +1,46 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/kable_as_image.R
+\name{kable_as_image}
+\alias{kable_as_image}
+\title{Convert a LaTeX table to an image and place it in a rmarkdown document}
+\usage{
+kable_as_image(kable_input, filename = NULL, file_format = "png",
+  latex_header_includes = NULL, keep_pdf = FALSE)
+}
+\arguments{
+\item{kable_input}{Raw LaTeX code to generate a table. It doesn't have to
+came from \code{kable} or \code{kableExtra}.}
+
+\item{filename}{Character String. If specified, the image will be saved under
+the specified (path &) name. You don't need to put file format like ".png"
+here.}
+
+\item{file_format}{Character String to specify image format, such as \code{png},
+\code{jpeg}, \code{gif}, \code{tiff}, etc. Default is \code{png}.}
+
+\item{latex_header_includes}{A character vector of extra LaTeX header stuff.
+Each element is a row. You can have things like
+\code{c("\\usepackage{threeparttable}", "\\usepackage{icons}")}}
+
+\item{keep_pdf}{A T/F option to control if the mid-way standalone pdf should
+be kept. Default is \code{FALSE}.}
+}
+\description{
+This is a LaTeX-only function. This function will render the
+raw LaTeX code (could be codes generated by other table packages like
+\code{xtable}) to generate a table, convert it to an image and put it back to a
+rmarkdown environment. It is a "better than nothing" solution to print high
+quality tables in rmarkdown Word document. By using this, you need to take
+the responsibility of explaining to your collaborators why they can't edit
+the tables they see in the Word document they received. 😂
+
+Also, if a filename is provided, user has the option to "save" the table to
+an image file like \code{ggplot2::ggsave()}.
+
+The idea of this function was coming from \href{https://stackoverflow.com/questions/44711313/save-rmarkdowns-report-tables-and-figures-to-file}{this StackOverflow question}.
+The approach was learned and adopted from the \href{https://github.com/metrumresearchgroup/texPreview}{texpreview}
+package, which allows you to preview the results of TeX code in the Viewer panel.
+}
+\examples{
+kable_as_image(kable(mtcars, "latex"), "mtcars")
+}
diff --git a/man/kable_styling.Rd b/man/kable_styling.Rd
index 8402227..1bc5c47 100644
--- a/man/kable_styling.Rd
+++ b/man/kable_styling.Rd
@@ -19,12 +19,13 @@
 
 \item{latex_options}{A character vector for LaTeX table options. Please see
 package vignette for more information. Possible options include
-\code{basic}, \code{striped}, \code{hold_position}, \code{scale_down} & \code{repeat_header}.
+\code{basic}, \code{striped}, \code{hold_position}, \code{HOLD_position}, \code{scale_down} & \code{repeat_header}.
 \code{striped} will add alternative row colors to the table. It will imports
 \code{LaTeX} package \code{xcolor} if enabled. \code{hold_position} will "hold" the floating
 table to the exact position. It is useful when the \code{LaTeX} table is contained
 in a \code{table} environment after you specified captions in \code{kable()}. It will
 force the table to stay in the position where it was created in the document.
+A stronger version: \code{HOLD_position} requires the \code{float} package and specifies \link{H}.
 \code{scale_down} is useful for super wide table. It will automatically adjust
 the table to page width. \code{repeat_header} in only meaningful in a longtable
 environment. It will let the header row repeat on every page in that long
diff --git a/tests/visual_tests/kable_as_image_pdf.Rmd b/tests/visual_tests/kable_as_image_pdf.Rmd
index 51b4c16..5b2c553 100644
--- a/tests/visual_tests/kable_as_image_pdf.Rmd
+++ b/tests/visual_tests/kable_as_image_pdf.Rmd
@@ -10,8 +10,8 @@
 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"))
+kable(mtcars, format = "latex", booktabs=TRUE, caption="hello") %>% 
+  kable_styling(latex_options=c("scale_down", "hold_position")) %>%
+  add_header_above(c(" ", "Group A" = 5, "Group B" = 6)) %>%
+  kable_as_image("sss")
 ```
diff --git a/tests/visual_tests/kable_as_image_pdf.docx b/tests/visual_tests/kable_as_image_pdf.docx
index 201447c..c209cb0 100644
--- a/tests/visual_tests/kable_as_image_pdf.docx
+++ b/tests/visual_tests/kable_as_image_pdf.docx
Binary files differ