Merge pull request #132 from bsalzer/master

fix centering when using threeparttable and a caption
diff --git a/R/add_header_above.R b/R/add_header_above.R
index 72fac20..fc2b805 100644
--- a/R/add_header_above.R
+++ b/R/add_header_above.R
@@ -16,6 +16,8 @@
 #' need to be monospaced (verbatim)
 #' @param escape A T/F value showing whether special characters should be
 #' escaped.
+#' @param line A T/F value to control whether a line will appear underneath the
+#' header
 #'
 #' @examples x <- knitr::kable(head(mtcars), "html")
 #' # Add a row of header with 3 columns on the top of the table. The column
@@ -25,7 +27,7 @@
 #' @export
 add_header_above <- function(kable_input, header = NULL,
                              bold = FALSE, italic = FALSE,
-                             monospace = FALSE, escape = TRUE) {
+                             monospace = FALSE, escape = TRUE,line = TRUE) {
   kable_format <- attr(kable_input, "format")
   if (!kable_format %in% c("html", "latex")) {
     warning("Please specify format in kable. kableExtra can customize either ",
@@ -35,17 +37,17 @@
   }
   if (kable_format == "html") {
     return(htmlTable_add_header_above(kable_input, header,
-                                      bold, italic, monospace, escape))
+                                      bold, italic, monospace, escape,line))
   }
   if (kable_format == "latex") {
     return(pdfTable_add_header_above(kable_input, header,
-                                     bold, italic, monospace, escape))
+                                     bold, italic, monospace, escape,line))
   }
 }
 
 # HTML
 htmlTable_add_header_above <- function(kable_input, header,
-                                       bold, italic, monospace, escape) {
+                                       bold, italic, monospace, escape,line) {
   if (is.null(header)) return(kable_input)
   kable_attrs <- attributes(kable_input)
   kable_xml <- read_kable_as_xml(kable_input)
@@ -66,7 +68,7 @@
   }
 
   new_header_row <- htmlTable_new_header_generator(header,
-                                                   bold, italic, monospace)
+                                                   bold, italic, monospace,line)
   xml_add_child(kable_xml_thead, new_header_row, .where = 0)
   out <- as_kable_xml(kable_xml)
   attributes(out) <- kable_attrs
@@ -89,7 +91,7 @@
   return(data.frame(header = names(header), colspan = header, row.names = NULL))
 }
 
-htmlTable_new_header_generator <- function(header_df, bold, italic, monospace) {
+htmlTable_new_header_generator <- function(header_df, bold, italic, monospace,line) {
   row_style <- paste0(
     ifelse(bold, "font-weight: bold; ", ""),
     ifelse(italic, "font-style: italic; ", ""),
@@ -103,7 +105,8 @@
              'padding-bottom:0; padding-left:3px;padding-right:3px;',
              row_style,
              '" colspan="',
-             x[2], '"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px;">',
+             x[2], '"><div style="',
+             ifelse(line,'border-bottom: 1px solid #ddd; padding-bottom: 5px;">','">'),
              x[1], '</div></th>')
     }
   })
@@ -114,7 +117,7 @@
 
 # Add an extra header row above the current header in a LaTeX table ------
 pdfTable_add_header_above <- function(kable_input, header,
-                                      bold, italic, monospace, escape) {
+                                      bold, italic, monospace, escape, line) {
   table_info <- magic_mirror(kable_input)
   header <- standardize_header_input(header)
   if (escape) {
@@ -124,7 +127,11 @@
   hline_type <- switch(table_info$booktabs + 1, "\\\\hline", "\\\\toprule")
   new_header_split <- pdfTable_new_header_generator(header, table_info$booktabs,
                                                     bold, italic, monospace)
-  new_header <- paste0(new_header_split[1], "\n", new_header_split[2])
+  if(line){
+    new_header <- paste0(new_header_split[1], "\n", new_header_split[2])
+  } else {
+    new_header <- new_header_split[1]
+  }
   out <- str_replace(enc2utf8(as.character(kable_input)),
                      hline_type,
                      paste0(hline_type, "\n", new_header))
diff --git a/R/footnote.R b/R/footnote.R
index 066e96f..d67382b 100644
--- a/R/footnote.R
+++ b/R/footnote.R
@@ -204,15 +204,10 @@
   footnote_text <- latex_tfoot_maker(footnote_table, footnote_as_chunk,
                                      table_info$ncol, threeparttable)
   if (threeparttable) {
-    if (grepl("\\\\caption\\{.*?\\}", out)) {
-      out <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{",
-                 out)
-    } else {
       out <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"),
                  paste0("\\\\begin{threeparttable}\n\\\\begin{",
                         table_info$tabular, "}"),
                  out)
-    }
     out <- sub(table_info$end_tabular,
                paste0("\\\\end{", table_info$tabular,
                       "}\n\\\\begin{tablenotes}",
diff --git a/R/kable_as_image.R b/R/kable_as_image.R
index 30c33f9..665f9d9 100644
--- a/R/kable_as_image.R
+++ b/R/kable_as_image.R
@@ -33,13 +33,16 @@
 #' be kept. Default is `FALSE`.
 #' @param density Resolution to read the PDF file. Default value is 300, which
 #' should be sufficient in most cases.
+#' @param keep_tex A T/F option to control if the latex file that is initially created
+#' should be kept. Default is `FALSE`.
 #'
 #' @export
 kable_as_image <- function(kable_input, filename = NULL,
                            file_format = "png",
                            latex_header_includes = NULL,
                            keep_pdf = FALSE,
-                           density = 300) {
+                           density = 300,
+                           keep_tex = FALSE) {
   if (!requireNamespace("magick", quietly = TRUE)) {
     stop('kable_as_image requires the magick package, which is not available ',
          'on all platforms. Please get it installed ',
@@ -70,7 +73,9 @@
     writeLines(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)
+    if(!keep_tex) {
+      unlink(temp_file_delete)
+    }
 
     table_img_pdf <- try(magick::image_read(paste0(temp_file, ".pdf"),
                                             density = density),
diff --git a/tests/.DS_Store b/tests/.DS_Store
deleted file mode 100644
index 93ddede..0000000
--- a/tests/.DS_Store
+++ /dev/null
Binary files differ
diff --git a/tests/visual_tests/.DS_Store b/tests/visual_tests/.DS_Store
deleted file mode 100644
index f5a2e96..0000000
--- a/tests/visual_tests/.DS_Store
+++ /dev/null
Binary files differ
diff --git a/tests/visual_tests/.gitignore b/tests/visual_tests/.gitignore
index c861d1f..7d7bcad 100644
--- a/tests/visual_tests/.gitignore
+++ b/tests/visual_tests/.gitignore
@@ -6,3 +6,5 @@
 *.tex
 *.synctex.gz
 *.lot
+*.docx
+.DS_store
diff --git a/tests/visual_tests/kable_as_image_pdf.docx b/tests/visual_tests/kable_as_image_pdf.docx
deleted file mode 100644
index c209cb0..0000000
--- a/tests/visual_tests/kable_as_image_pdf.docx
+++ /dev/null
Binary files differ