add column background to latex
diff --git a/R/column_spec.R b/R/column_spec.R
index 1e2f13a..3af165b 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -16,6 +16,8 @@
#' need to be emphasized.
#' @param monospace A T/F value to control whether the text of the selected column
#' need to be monospaced (verbatim)
+#' @param color A character string for column text color.
+#' @param background A character string for column background color.
#'
#' @examples x <- knitr::kable(head(mtcars), "html")
#' column_spec(x, 1, width = "20em", bold = TRUE, italic = TRUE)
@@ -23,7 +25,8 @@
#' @export
column_spec <- function(kable_input, column,
width = NULL, bold = FALSE, italic = FALSE,
- monospace = FALSE, color = NULL, background = NULL) {
+ monospace = FALSE, color = NULL, background = NULL,
+ border_left = FALSE, border_right = FALSE) {
if (!is.numeric(column)) {
stop("column must be a numeric value")
}
@@ -35,18 +38,21 @@
if (kable_format == "html") {
return(column_spec_html(kable_input, column, width,
bold, italic, monospace,
- color, background))
+ color, background,
+ border_left, border_right))
}
if (kable_format == "latex") {
return(column_spec_latex(kable_input, column, width,
bold, italic, monospace,
- color, background))
+ color, background,
+ border_left, border_right))
}
}
column_spec_html <- function(kable_input, column, width,
bold, italic, monospace,
- color, background) {
+ color, background,
+ border_left, border_right) {
kable_attrs <- attributes(kable_input)
kable_xml <- read_kable_as_xml(kable_input)
kable_tbody <- xml_tpart(kable_xml, "tbody")
@@ -67,6 +73,18 @@
group_header_rows]
}
+ # Border css
+ border_l_css <- "1px solid"
+ border_r_css <- "1px solid"
+ if (is.character(border_left)) {
+ border_l_css <- border_left
+ border_left <- T
+ }
+ if (is.character(border_right)) {
+ border_r_css <- border_right
+ border_right <- T
+ }
+
for (i in all_contents_rows) {
target_cell <- xml_child(xml_child(kable_tbody, i), all_contents_array[i])
if (!is.null(width)) {
@@ -94,6 +112,14 @@
"background-color: ",
background, ";")
}
+ if (border_left) {
+ xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
+ "border-left:", border_l_css, ";")
+ }
+ if (border_right) {
+ xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
+ "border-right:", border_r_css, ";")
+ }
}
out <- as_kable_xml(kable_xml)
attributes(out) <- kable_attrs
@@ -102,7 +128,8 @@
column_spec_latex <- function(kable_input, column, width,
bold, italic, monospace,
- color, background) {
+ color, background,
+ border_left, border_right) {
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,",
@@ -116,7 +143,7 @@
table_info$align_vector[column] <- latex_column_align_builder(
table_info$align_vector[column], width, bold, italic, monospace,
- color, background)
+ color, background, border_left, border_right)
kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
@@ -134,7 +161,8 @@
}
latex_column_align_builder <- function(x, width, bold, italic, monospace,
- color, background) {
+ color, background,
+ border_left, border_right) {
extra_align <- ""
if (!is.null(width)) {
extra_align <- switch(x,
@@ -148,13 +176,24 @@
color <- sprintf("\\\\color{%s}", color)
}
+ if (!is.null(color)) {
+ background <- sprintf("\\\\columncolor{%s}", background)
+ }
+
latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[
c(bold, italic, monospace)]
- latex_array_options <- c(latex_array_options, extra_align, color)
+ latex_array_options <- c(latex_array_options, extra_align,
+ color, background)
latex_array_options <- paste0(
"\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
)
x <- paste0(latex_array_options, x)
+ if (border_left) {
+ x <- paste0("|", x)
+ }
+ if (border_right) {
+ x <- paste0(x, "|")
+ }
return(x)
}
diff --git a/R/zzz.R b/R/zzz.R
index fd83d76..e716b96 100644
--- a/R/zzz.R
+++ b/R/zzz.R
@@ -5,4 +5,5 @@
usepackage_latex("multirow")
usepackage_latex("xcolor", "table")
usepackage_latex("wrapfig")
+ usepackage_latex("colortbl")
}
diff --git a/man/column_spec.Rd b/man/column_spec.Rd
index 9b24b01..81986e4 100644
--- a/man/column_spec.Rd
+++ b/man/column_spec.Rd
@@ -5,7 +5,8 @@
\title{Specify the look of the selected column}
\usage{
column_spec(kable_input, column, width = NULL, bold = FALSE,
- italic = FALSE, monospace = FALSE)
+ italic = FALSE, monospace = FALSE, color = NULL, background = NULL,
+ border_left = FALSE, border_right = FALSE)
}
\arguments{
\item{kable_input}{Output of \code{knitr::kable()} with \code{format} specified}
@@ -25,6 +26,10 @@
\item{monospace}{A T/F value to control whether the text of the selected column
need to be monospaced (verbatim)}
+
+\item{color}{A character string for column text color.}
+
+\item{background}{A character string for column background color.}
}
\description{
This function allows users to select a column and then specify
diff --git a/man/kable_styling.Rd b/man/kable_styling.Rd
index 6bb43a2..8402227 100644
--- a/man/kable_styling.Rd
+++ b/man/kable_styling.Rd
@@ -57,7 +57,7 @@
\item \code{repeat_header_method} can either be \code{append}(default) or \code{replace}
\item \code{repeat_header_text} is just a text string you want to append on or
replace the caption.
-\item \code{strip_color} allows users to pick a different color for their strip lines.
+\item \code{stripe_color} allows users to pick a different color for their strip lines.
}
}
\examples{
diff --git a/man/row_spec.Rd b/man/row_spec.Rd
index 8ad0c7d..a344479 100644
--- a/man/row_spec.Rd
+++ b/man/row_spec.Rd
@@ -5,7 +5,7 @@
\title{Specify the look of the selected row}
\usage{
row_spec(kable_input, row, bold = FALSE, italic = FALSE,
- monospace = FALSE)
+ monospace = FALSE, color = NULL, background = NULL)
}
\arguments{
\item{kable_input}{Output of \code{knitr::kable()} with \code{format} specified}
diff --git a/tests/visual_tests/column_row_spec_html.Rmd b/tests/visual_tests/column_row_spec_html.Rmd
index d63ab49..277e338 100644
--- a/tests/visual_tests/column_row_spec_html.Rmd
+++ b/tests/visual_tests/column_row_spec_html.Rmd
@@ -40,8 +40,8 @@
kable(dt, "html") %>%
kable_styling(full_width = F) %>%
row_spec(1, bold = T) %>%
- column_spec(2, "5cm", bold = T, color = "white", background = "red") %>%
- column_spec(3, monospace = T) %>%
+ column_spec(2, "5cm", bold = T, color = "white", background = "gray") %>%
+ column_spec(3, monospace = T, border_right = T) %>%
column_spec(4, "3cm", italic = T)
```
diff --git a/tests/visual_tests/column_row_spec_pdf.Rmd b/tests/visual_tests/column_row_spec_pdf.Rmd
index b727f0f..52c1b0a 100644
--- a/tests/visual_tests/column_row_spec_pdf.Rmd
+++ b/tests/visual_tests/column_row_spec_pdf.Rmd
@@ -40,9 +40,9 @@
)
kable(dt, "latex", booktabs = T, align = "r") %>%
- column_spec(2, "3cm", bold = T) %>%
+ column_spec(2, "3cm", bold = T, border_right = T) %>%
column_spec(3, monospace = T)%>%
- column_spec(4, "3cm", italic = T, color = "red") %>%
+ column_spec(4, "3cm", italic = T, color = "red", border_left = T, background = "blue") %>%
row_spec(3, monospace = T, color = "yellow", background = "black")
```