add auto convert from makecell to newline in column_spec
diff --git a/R/column_spec.R b/R/column_spec.R
index 21eb99c..377ceb5 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -180,6 +180,13 @@
out <- sub(kable_align_old, kable_align_new,
solve_enc(kable_input),
perl = T)
+
+ if (!is.null(width)) {
+ fix_newline <- replace_makecell_with_newline(out, table_info, column)
+ out <- fix_newline[[1]]
+ table_info <- fix_newline[[2]]
+ }
+
out <- structure(out, format = "latex", class = "knitr_kable")
if (!is.null(width)) {
if (is.null(table_info$column_width)) {
@@ -232,3 +239,36 @@
return(x)
}
+
+replace_makecell_with_newline <- function(kable_input, table_info, column) {
+ if (!str_detect(kable_input, "makecell")) return(list(kable_input, table_info))
+ contents_table <- data.frame(sapply(table_info$contents,
+ function(x) {str_split(x, " \\& ")[[1]]}),
+ stringsAsFactors = F)
+ names(contents_table) <- paste0("x", 1:table_info$nrow)
+ rows_check_makecell <- str_detect(contents_table[column, ], "makecell")
+ if (sum(rows_check_makecell) == 0) return(list(kable_input, table_info))
+ rows_to_replace <- which(rows_check_makecell)
+
+ for (i in column) {
+ target_column <- contents_table[i, ]
+ for (j in which(str_detect(target_column, "\\\\\\\\makecell"))) {
+ contents_table[i, j] <- str_replace(
+ contents_table[i, j], "\\\\\\\\makecell\\\\\\[.\\\\\\]\\\\\\{", "")
+ contents_table[i, j] <- str_replace(
+ contents_table[i, j], "\\}$", "")
+ contents_table[i, j] <- str_replace_all(
+ contents_table[i, j], "\\\\\\\\\\\\\\\\", "\\\\\\\\newline "
+ )
+ }
+ }
+
+ new_contents <- unlist(lapply(contents_table, paste, collapse = " & "))
+ for (i in rows_to_replace) {
+ kable_input <- sub(table_info$contents[i], new_contents[i], kable_input,
+ perl = T)
+ table_info$contents[i] <- new_contents[i]
+ }
+
+ return(list(kable_input, table_info))
+}