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/add_footnote.R b/R/add_footnote.R
index abb618a..ce0a4a9 100644
--- a/R/add_footnote.R
+++ b/R/add_footnote.R
@@ -15,6 +15,8 @@
#' @param threeparttable Boolean value indicating if a
#' \href{https://www.ctan.org/pkg/threeparttable}{threeparttable} scheme should
#' be used.
+#' @param escape Logical value controlling if the label needs to be escaped.
+#' Default is TRUE.
#'
#' @examples x <- knitr::kable(head(mtcars), "html")
#' add_footnote(x, c("footnote 1", "footnote 2"), notation = "symbol")
@@ -22,7 +24,8 @@
#' @export
add_footnote <- function(input, label = NULL,
notation = "alphabet",
- threeparttable = FALSE) {
+ threeparttable = FALSE,
+ escape = TRUE) {
if (is.null(label)) return(input)
if (notation == "alphabet") {
@@ -96,7 +99,9 @@
# LaTeX Tables --------------------------------
if (attr(input, "format") == "latex") {
# Clean the entry for labels
- label <- escape_latex(label)
+ if (escape) {
+ label <- escape_latex(label)
+ }
label <- gsub("\\\\", "\\\\\\\\", label)
export <- enc2utf8(export)
@@ -175,8 +180,9 @@
"\\\\begin{threeparttable}\n\\\\caption{",
export)
} else {
- export <- sub("\\\\begin\\{tabular\\}",
- "\\\\begin{threeparttable}\n\\\\begin{tabular}",
+ export <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"),
+ paste0("\\\\begin{threeparttable}\n\\\\begin{",
+ table_info$tabular, "}"),
export)
}
export <- gsub(
@@ -213,7 +219,9 @@
if (attr(input, "format") == "html") {
# Clean the entry for labels
table_info <- magic_mirror(input)
- label <- escape_html(label)
+ if (escape) {
+ label <- escape_html(label)
+ }
# Replace in-table notation with appropriate symbol
for (i in 1:count.intablenote) {
export <- sub("\\[note\\]",
diff --git a/R/column_spec.R b/R/column_spec.R
index 409ea0c..a2d89d6 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -139,7 +139,7 @@
kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
table_info$align_vector[column] <- latex_column_align_builder(
- table_info$align_vector[column], width, bold, italic, monospace,
+ table_info$align_vector_origin[column], width, bold, italic, monospace,
color, background, border_left, border_right)
kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
diff --git a/R/kable_as_image.R b/R/kable_as_image.R
index 34eaf3d..d01d941 100644
--- a/R/kable_as_image.R
+++ b/R/kable_as_image.R
@@ -5,8 +5,8 @@
#' `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. 😂
+#' the responsibility of explaining to your collaborators why they can't make
+#' edits to the tables in Word. 😂
#'
#' Also, if a filename is provided, user has the option to "save" the table to
#' an image file like `ggplot2::ggsave()`.
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))
+}
diff --git a/R/landscape.R b/R/landscape.R
index fd7789e..01ef2bb 100644
--- a/R/landscape.R
+++ b/R/landscape.R
@@ -26,7 +26,6 @@
landscape_latex <- function(kable_input, margin) {
kable_attrs <- attributes(kable_input)
- usepackage_latex("pdflscape")
out <- paste0(
"\n\\begin{landscape}",
enc2utf8(as.character(kable_input)),
diff --git a/R/zzz.R b/R/zzz.R
index e716b96..78d4020 100644
--- a/R/zzz.R
+++ b/R/zzz.R
@@ -6,4 +6,7 @@
usepackage_latex("xcolor", "table")
usepackage_latex("wrapfig")
usepackage_latex("colortbl")
+ usepackage_latex("pdflscape")
+ usepackage_latex("tabu")
+ usepackage_latex("threeparttable")
}