add escape to footnote;
improve full_width in latex
add latex_table_env to latex
move some latex packages to zzz
update NEWS
diff --git a/R/kable_styling.R b/R/kable_styling.R
index a4fed07..1e52b03 100644
--- a/R/kable_styling.R
+++ b/R/kable_styling.R
@@ -41,6 +41,9 @@
 #' - `repeat_header_text` is just a text string you want to append on or
 #' replace the caption.
 #' - `stripe_color` allows users to pick a different color for their strip lines.
+#' - `latex_table_env` character string to define customized table environment
+#' such as tabu or tabularx.You shouldn't expect all features could be
+#' supported in self-defined environments.
 #'
 #' @examples x_html <- knitr::kable(head(mtcars), "html")
 #' kable_styling(x_html, "striped", position = "left", font_size = 7)
@@ -177,7 +180,8 @@
                              font_size = NULL,
                              repeat_header_text = "\\textit{(continued)}",
                              repeat_header_method = c("append", "replace"),
-                             stripe_color = "gray!6") {
+                             stripe_color = "gray!6",
+                             latex_table_env = NULL) {
 
   latex_options <- match.arg(
     latex_options,
@@ -215,13 +219,26 @@
   }
 
   if (full_width) {
-    out <- styling_latex_full_width(out, table_info)
+    latex_table_env <- ifelse(table_info$tabular == "longtable",
+                              "longtabu", "tabu")
+    full_width_return <- styling_latex_full_width(out, table_info)
+    out <- full_width_return[[1]]
+    table_info$align_vector <- full_width_return[[2]]
   }
 
   if (!is.null(font_size)) {
     out <- styling_latex_font_size(out, table_info, font_size)
   }
 
+  if (!is.null(latex_table_env)) {
+    out <- styling_latex_table_env(out, table_info$tabular, latex_table_env)
+    table_info$tabular <- latex_table_env
+    table_info$begin_tabular <- sub("tabular", latex_table_env,
+                                    table_info$begin_tabular)
+    table_info$end_tabular <- sub("tabular", latex_table_env,
+                                  table_info$end_tabular)
+  }
+
   position <- match.arg(position)
   out <- styling_latex_position(out, table_info, position, latex_options)
 
@@ -323,16 +340,18 @@
 }
 
 styling_latex_full_width <- function(x, table_info) {
-  size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
-  col_max_length <- apply(size_matrix, 1, max) + 4
-  col_ratio <- round(col_max_length / sum(col_max_length), 2)
-  col_align <- paste0("p{\\\\dimexpr", col_ratio,
-                      "\\\\columnwidth-2\\\\tabcolsep}")
-  col_align <- paste0("{", paste(col_align, collapse = ""), "}")
+  col_align <- as.character(factor(
+    table_info$align_vector, c("c", "l", "r"),
+    c(">{\\\\centering}X", ">{\\\\raggedright}X", ">{\\\\raggedleft}X")
+  ))
+  col_align[is.na(col_align)] <- table_info$align_vector[is.na(col_align)]
+  col_align_vector <- col_align
+  col_align <- paste0(" to \\\\linewidth {", paste(col_align, collapse = ""), "}")
   x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
            table_info$begin_tabular, x)
-  sub(table_info$begin_tabular,
+  x <- sub(table_info$begin_tabular,
       paste0(table_info$begin_tabular, col_align), x)
+  return(list(x, col_align_vector))
 }
 
 styling_latex_position <- function(x, table_info, position, latex_options) {
@@ -391,7 +410,7 @@
 
 styling_latex_font_size <- function(x, table_info, font_size) {
   row_height <- font_size + 2
-  if (table_info$tabular == "tabular" & table_info$table_env) {
+  if (table_info$tabular != "longtable" & table_info$table_env) {
     return(sub(table_info$begin_tabular,
                paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
                       "\\}\\\\selectfont\n", table_info$begin_tabular),
@@ -405,4 +424,8 @@
   ))
 }
 
+styling_latex_table_env <- function(x, current_env, latex_table_env) {
+  return(gsub(paste0("\\{", current_env, "\\}"),
+              paste0("\\{", latex_table_env, "\\}"), x))
+}