add monospae to row_spec as well. #22
diff --git a/R/row_spec.R b/R/row_spec.R
index 0c71060..55a8bed 100644
--- a/R/row_spec.R
+++ b/R/row_spec.R
@@ -11,13 +11,15 @@
#' need to be bolded.
#' @param italic A T/F value to control whether the text of the selected row
#' 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")
#' row_spec(x, 1, bold = TRUE, italic = TRUE)
#'
#' @export
row_spec <- function(kable_input, row,
- bold = FALSE, italic = FALSE) {
+ bold = FALSE, italic = FALSE, monospace = FALSE) {
if (!is.numeric(row)) {
stop("row must be a numeric value")
}
@@ -27,14 +29,14 @@
return(kable_input)
}
if (kable_format == "html") {
- return(row_spec_html(kable_input, row, bold, italic))
+ return(row_spec_html(kable_input, row, bold, italic, monospace))
}
if (kable_format == "latex") {
- return(row_spec_latex(kable_input, row, bold, italic))
+ return(row_spec_latex(kable_input, row, bold, italic, monospace))
}
}
-row_spec_html <- function(kable_input, row, bold, italic) {
+row_spec_html <- function(kable_input, row, bold, italic, monospace) {
kable_attrs <- attributes(kable_input)
kable_xml <- read_kable_as_xml(kable_input)
kable_tbody <- xml_tpart(kable_xml, "tbody")
@@ -57,13 +59,17 @@
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 <- as_kable_xml(kable_xml)
attributes(out) <- kable_attrs
return(out)
}
-row_spec_latex <- function(kable_input, row, bold, italic) {
+row_spec_latex <- function(kable_input, row, bold, italic, monospace) {
table_info <- magic_mirror(kable_input)
target_row <- table_info$contents[row + 1]
new_row <- latex_row_cells(target_row)
@@ -77,6 +83,11 @@
paste0("\\\\em{", x, "}")
})
}
+ if (monospace) {
+ new_row <- lapply(new_row, function(x) {
+ paste0("\\\\ttfamily{", x, "}")
+ })
+ }
new_row <- paste(unlist(new_row), collapse = " & ")
out <- sub(target_row, new_row, as.character(kable_input), perl = T)
diff --git a/man/row_spec.Rd b/man/row_spec.Rd
index d8237ad..8ad0c7d 100644
--- a/man/row_spec.Rd
+++ b/man/row_spec.Rd
@@ -4,7 +4,8 @@
\alias{row_spec}
\title{Specify the look of the selected row}
\usage{
-row_spec(kable_input, row, bold = FALSE, italic = FALSE)
+row_spec(kable_input, row, bold = FALSE, italic = FALSE,
+ monospace = FALSE)
}
\arguments{
\item{kable_input}{Output of \code{knitr::kable()} with \code{format} specified}
@@ -17,6 +18,9 @@
\item{italic}{A T/F value to control whether the text of the selected row
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 row and then specify
diff --git a/tests/visual_tests/column_row_spec_pdf.Rmd b/tests/visual_tests/column_row_spec_pdf.Rmd
index 8ffe566..686cf8c 100644
--- a/tests/visual_tests/column_row_spec_pdf.Rmd
+++ b/tests/visual_tests/column_row_spec_pdf.Rmd
@@ -42,6 +42,7 @@
kable(dt, "latex", booktabs = T, align = "r") %>%
column_spec(2, "3cm", bold = T) %>%
column_spec(3, monospace = T)%>%
- column_spec(4, "3cm", italic = T)
+ column_spec(4, "3cm", italic = T) %>%
+ row_spec(3, monospace = T)
```