add text/bg color to latex except column bg color
diff --git a/R/column_spec.R b/R/column_spec.R
index e13e685..1e2f13a 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -71,7 +71,7 @@
target_cell <- xml_child(xml_child(kable_tbody, i), all_contents_array[i])
if (!is.null(width)) {
xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
- "width: ", width, "; ")
+ "width: ", width, "; ")
}
if (bold) {
xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
@@ -100,17 +100,23 @@
return(out)
}
-column_spec_latex <- function(kable_input, column, width, bold, italic, monospace) {
+column_spec_latex <- function(kable_input, column, width,
+ bold, italic, monospace,
+ color, background) {
table_info <- magic_mirror(kable_input)
if (!is.null(table_info$collapse_rows)) {
message("Usually it is recommended to use column_spec before collapse_rows,",
" especially in LaTeX, to get a desired result. ")
}
+ if (!is.null(background)) {
+ warning("Column background color for LaTeX has not yet been implemented.")
+ }
align_collapse <- ifelse(table_info$booktabs, "", "\\|")
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[column], width, bold, italic, monospace,
+ color, background)
kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
@@ -127,7 +133,8 @@
return(out)
}
-latex_column_align_builder <- function(x, width, bold, italic, monospace) {
+latex_column_align_builder <- function(x, width, bold, italic, monospace,
+ color, background) {
extra_align <- ""
if (!is.null(width)) {
extra_align <- switch(x,
@@ -137,15 +144,17 @@
x <- paste0("p\\{", width, "\\}")
}
- if (bold | italic | monospace | extra_align != "") {
- latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[
- c(bold, italic, monospace)]
- latex_array_options <- c(latex_array_options, extra_align)
- latex_array_options <- paste0(
- "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
- )
- x <- paste0(latex_array_options, x)
+ if (!is.null(color)) {
+ color <- sprintf("\\\\color{%s}", color)
}
+ latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[
+ c(bold, italic, monospace)]
+ latex_array_options <- c(latex_array_options, extra_align, color)
+ latex_array_options <- paste0(
+ "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
+ )
+ x <- paste0(latex_array_options, x)
+
return(x)
}
diff --git a/R/row_spec.R b/R/row_spec.R
index 6615365..060c27f 100644
--- a/R/row_spec.R
+++ b/R/row_spec.R
@@ -82,7 +82,8 @@
return(out)
}
-row_spec_latex <- function(kable_input, row, bold, italic, monospace) {
+row_spec_latex <- function(kable_input, row, bold, italic, monospace,
+ color, background) {
table_info <- magic_mirror(kable_input)
target_row <- table_info$contents[row + 1]
new_row <- latex_row_cells(target_row)
@@ -101,8 +102,18 @@
paste0("\\\\ttfamily{", x, "}")
})
}
+
+ if (!is.null(color)) {
+ new_row <- lapply(new_row, function(x) {
+ paste0("\\\\textcolor{", color, "}{", x, "}")
+ })
+ }
new_row <- paste(unlist(new_row), collapse = " & ")
+ if (!is.null(background)) {
+ new_row <- paste0("\\\\rowcolor{", background, "} ", new_row)
+ }
+
out <- sub(target_row, new_row, as.character(kable_input), perl = T)
out <- structure(out, format = "latex", class = "knitr_kable")
attr(out, "kable_meta") <- table_info