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))
+}
diff --git a/R/kable_styling.R b/R/kable_styling.R
index 3d488fc..b7677df 100644
--- a/R/kable_styling.R
+++ b/R/kable_styling.R
@@ -332,7 +332,7 @@
return(x)
}
x <- sub(table_info$begin_tabular,
- paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{",
+ paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{\n",
table_info$begin_tabular),
x)
sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
diff --git a/R/row_spec.R b/R/row_spec.R
index fcbd226..3108de8 100644
--- a/R/row_spec.R
+++ b/R/row_spec.R
@@ -194,7 +194,13 @@
underline, strikeout,
color, background, align, font_size, angle,
hline_after, extra_latex_after)
- out <- sub(paste0(target_row, "\\\\\\\\"), new_row, out, perl = T)
+ if (length(new_row) == 1) {
+ out <- sub(target_row, new_row, out, perl = T)
+ } else {
+ out <- sub(paste0(target_row, "\\\\\\\\"),
+ paste(new_row, collapse = ""), out, perl = T)
+ }
+ table_info$contents[i] <- new_row
}
out <- structure(out, format = "latex", class = "knitr_kable")
@@ -209,49 +215,49 @@
new_row <- latex_row_cells(target_row)
if (bold) {
new_row <- lapply(new_row, function(x) {
- paste0("\\\\textbf{", x, "}")
+ paste0("\\\\textbf\\{", x, "\\}")
})
}
if (italic) {
new_row <- lapply(new_row, function(x) {
- paste0("\\\\em{", x, "}")
+ paste0("\\\\em\\{", x, "\\}")
})
}
if (monospace) {
new_row <- lapply(new_row, function(x) {
- paste0("\\\\ttfamily{", x, "}")
+ paste0("\\\\ttfamily\\{", x, "\\}")
})
}
if (underline) {
new_row <- lapply(new_row, function(x) {
- paste0("\\\\underline{", x, "}")
+ paste0("\\\\underline\\{", x, "\\}")
})
}
if (strikeout) {
new_row <- lapply(new_row, function(x) {
- paste0("\\\\sout{", x, "}")
+ paste0("\\\\sout\\{", x, "\\}")
})
}
if (!is.null(color)) {
new_row <- lapply(new_row, function(x) {
- paste0("\\\\textcolor", latex_color(color), "{", x, "}")
+ paste0("\\\\textcolor", latex_color(color), "\\{", x, "\\}")
})
}
if (!is.null(font_size)) {
new_row <- lapply(new_row, function(x) {
- paste0("\\\\begingroup\\\\fontsize{", font_size, "}{",
+ paste0("\\\\begingroup\\\\fontsize\\{", font_size, "\\}\\{",
as.numeric(font_size) + 2,
- "}\\\\selectfont ", x, "\\\\endgroup")})
+ "\\}\\\\selectfont ", x, "\\\\endgroup")})
}
if (!is.null(align)) {
new_row <- lapply(new_row, function(x) {
- paste0("\\\\multicolumn{1}{", align, "}{", x, "}")
+ paste0("\\\\multicolumn\\{1\\}\\{", align, "\\}\\{", x, "\\}")
})
}
if (!is.null(angle)) {
new_row <- lapply(new_row, function(x) {
- paste0("\\\\rotatebox{", angle, "}{", x, "}")
+ paste0("\\\\rotatebox\\{", angle, "\\}\\{", x, "\\}")
})
}
@@ -261,14 +267,18 @@
new_row <- paste0("\\\\rowcolor", latex_color(background), " ", new_row)
}
- new_row <- paste0(new_row, "\\\\\\\\")
-
- if (hline_after) {
- new_row <- paste0(new_row, "\n\\\\hline")
+ if (!hline_after & is.null(extra_latex_after)) {
+ return(new_row)
+ } else {
+ latex_after <- "\\\\\\\\"
+ if (hline_after) {
+ latex_after <- paste0(latex_after, "\n\\\\hline")
+ }
+ if (!is.null(extra_latex_after)) {
+ latex_after <- paste0(latex_after, "\n",
+ regex_escape(extra_latex_after,
+ double_backslash = TRUE))
+ }
+ return(c(new_row, latex_after))
}
- if (!is.null(extra_latex_after)) {
- new_row <- paste0(new_row, "\n",
- regex_escape(extra_latex_after, double_backslash = TRUE))
- }
- return(new_row)
}