- Fix a bug in collapse_row on removing addlinespace
- remove addlinespace from group_rows
- add monospace to column_spec
diff --git a/R/collapse_rows.R b/R/collapse_rows.R
index 72ee31c..d260362 100644
--- a/R/collapse_rows.R
+++ b/R/collapse_rows.R
@@ -127,7 +127,7 @@
}
out <- sub(contents[i + 1], new_contents[i], out)
}
- out <- sub("\\\\addlinespace\n", "", out)
+ out <- gsub("\\\\addlinespace\n", "", out)
out <- structure(out, format = "latex", class = "knitr_kable")
table_info$collapse_rows <- TRUE
diff --git a/R/column_spec.R b/R/column_spec.R
index a177174..f936305 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -14,13 +14,16 @@
#' need to be bolded.
#' @param italic A T/F value to control whether the text of the selected column
#' need to be emphasized.
+#' @param monospace A T/F value to control whether the text of the selected column
+#' need to be monospaced (verbatim)
#'
#' @examples x <- knitr::kable(head(mtcars), "html")
#' column_spec(x, 1, width = "20em", bold = TRUE, italic = TRUE)
#'
#' @export
column_spec <- function(kable_input, column,
- width = NULL, bold = FALSE, italic = FALSE) {
+ width = NULL, bold = FALSE, italic = FALSE,
+ monospace = FALSE) {
if (!is.numeric(column)) {
stop("column must be a numeric value")
}
@@ -30,14 +33,14 @@
return(kable_input)
}
if (kable_format == "html") {
- return(column_spec_html(kable_input, column, width, bold, italic))
+ return(column_spec_html(kable_input, column, width, bold, italic, monospace))
}
if (kable_format == "latex") {
- return(column_spec_latex(kable_input, column, width, bold, italic))
+ return(column_spec_latex(kable_input, column, width, bold, italic, monospace))
}
}
-column_spec_html <- function(kable_input, column, width, bold, italic) {
+column_spec_html <- function(kable_input, column, width, bold, italic, monospace) {
kable_attrs <- attributes(kable_input)
kable_xml <- read_xml(as.character(kable_input), options = "COMPACT")
kable_tbody <- xml_tpart(kable_xml, "tbody")
@@ -72,6 +75,10 @@
xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
"font-style: italic;")
}
+ if (monospace) {
+ xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
+ "font-family: monospace;")
+ }
}
out <- structure(as.character(kable_xml), format = "html",
class = "knitr_kable")
@@ -79,7 +86,7 @@
return(out)
}
-column_spec_latex <- function(kable_input, column, width, bold, italic) {
+column_spec_latex <- function(kable_input, column, width, bold, italic, monospace) {
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,",
@@ -89,7 +96,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)
+ table_info$align_vector[column], width, bold, italic, monospace)
kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
@@ -106,7 +113,7 @@
return(out)
}
-latex_column_align_builder <- function(x, width, bold, italic) {
+latex_column_align_builder <- function(x, width, bold, italic, monospace) {
extra_align <- ""
if (!is.null(width)) {
extra_align <- switch(x,
@@ -116,8 +123,9 @@
x <- paste0("p\\{", width, "\\}")
}
- if (bold | italic | extra_align != "") {
- latex_array_options <- c("\\\\bfseries", "\\\\em")[c(bold, italic)]
+ 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 = ""), "\\}"
diff --git a/R/group_rows.R b/R/group_rows.R
index 7b2ad33..c38aaae 100644
--- a/R/group_rows.R
+++ b/R/group_rows.R
@@ -98,6 +98,7 @@
)
}
out <- sub(rowtext, new_rowtext, out)
+ out <- gsub("\\\\addlinespace\n", "", out)
table_info$group_rows_used <- TRUE
attr(out, "kable_meta") <- table_info
out <- add_indent(out, seq(start_row, end_row))
diff --git a/man/column_spec.Rd b/man/column_spec.Rd
index fdedeac..9b24b01 100644
--- a/man/column_spec.Rd
+++ b/man/column_spec.Rd
@@ -5,7 +5,7 @@
\title{Specify the look of the selected column}
\usage{
column_spec(kable_input, column, width = NULL, bold = FALSE,
- italic = FALSE)
+ italic = FALSE, monospace = FALSE)
}
\arguments{
\item{kable_input}{Output of \code{knitr::kable()} with \code{format} specified}
@@ -22,6 +22,9 @@
\item{italic}{A T/F value to control whether the text of the selected column
need to be emphasized.}
+
+\item{monospace}{A T/F value to control whether the text of the selected column
+need to be monospaced (verbatim)}
}
\description{
This function allows users to select a column and then specify
diff --git a/tests/visual_tests/column_row_spec_html.Rmd b/tests/visual_tests/column_row_spec_html.Rmd
index 919e4e7..7bbe229 100644
--- a/tests/visual_tests/column_row_spec_html.Rmd
+++ b/tests/visual_tests/column_row_spec_html.Rmd
@@ -41,6 +41,7 @@
kable_styling(full_width = F) %>%
row_spec(1, bold = T) %>%
column_spec(2, "5cm", bold = T) %>%
+ column_spec(3, monospace = T) %>%
column_spec(4, "3cm", italic = T)
```
@@ -55,17 +56,3 @@
column_spec(7, bold = T) %>%
row_spec(5, bold = T, italic = T)
```
-
-```{r}
-dt <- mtcars[1:5, 1:6]
-dt$wt <- paste(dt$wt, "%")
-dt$mpg <- paste(dt$mpg, "&")
-dt$cyl <- paste(dt$cyl, "$")
-
-kable(dt, "html") %>%
- kable_styling(full_width = F) %>%
- add_header_left(c("aaa" = 4, "bbb"), "A") %>%
- add_header_left(c("cccasjof jsafjiosadjfxclxz cjoiwdjosfns daflasfj asiofjsdfio" = 2, "ddd" = 3), "B") %>%
- column_spec(1, bold = T, width = "2cm") %>%
- column_spec(2, italic = T)
-```
diff --git a/tests/visual_tests/column_row_spec_pdf.Rmd b/tests/visual_tests/column_row_spec_pdf.Rmd
index b639092..8ffe566 100644
--- a/tests/visual_tests/column_row_spec_pdf.Rmd
+++ b/tests/visual_tests/column_row_spec_pdf.Rmd
@@ -28,7 +28,7 @@
"Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. "
),
contents2 = c(
- "Lorem ipsum dolor",
+ "Lorem {ipsum} dolor",
"In eu urna ",
"Vivamus venenatis"
),
@@ -41,6 +41,7 @@
kable(dt, "latex", booktabs = T, align = "r") %>%
column_spec(2, "3cm", bold = T) %>%
- column_spec(4, "3cm", italic = T)
+ column_spec(3, monospace = T)%>%
+ column_spec(4, "3cm", italic = T)
```
diff --git a/tests/visual_tests/indent_and_row_group.Rmd b/tests/visual_tests/indent_and_row_group.Rmd
index 789fff8..b9e35bd 100644
--- a/tests/visual_tests/indent_and_row_group.Rmd
+++ b/tests/visual_tests/indent_and_row_group.Rmd
@@ -27,10 +27,6 @@
mtcars$wt[i] <- paste0(mtcars$wt[i], " &")
mtcars$mpg[i] <- paste0(mtcars$mpg[i], "%")
}
-kable(head(mtcars, n = 20), "latex", caption = "test", booktabs = T) %>%
- kable_styling() %>%
- # group_rows("these rows", 4, 16) %>%
- # group_rows("Those rows", 17,20) %>%
- # add_header_above(c(" ", "aaa$" = 5, "bbb%" = 6)) %>%
- add_header_left(c("jj", "aaa" = 5, "bbb" = 6))
+kable(head(iris, n = 20), "latex", caption = "test", booktabs = T,
+ align = c("ccccc"))
```