update documentation
diff --git a/R/cell_spec.R b/R/cell_spec.R
index 0f55d31..4fa5895 100644
--- a/R/cell_spec.R
+++ b/R/cell_spec.R
@@ -1,37 +1,41 @@
-#' Specify Cell format
+#' Specify Cell/Text format
#'
#' @description Specify Cell format before it gets into kable
#'
#' @param x Things to be formated. It could be a vector of numbers or strings.
#' @param format Either "html" or "latex". It can also be set through
#' `option(knitr.table.format)`, same as `knitr::kable()`.
-#' @param bold A T/F value to control whether the text of the selected column
-#' 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)
-#' @param color A character string for column text color. Here please pay
+#' @param bold T/F for font bold.
+#' @param italic T/F for font italic.
+#' @param monospace T/F for font monospaced (verbatim)
+#' @param color A character string for text color. Here please pay
#' attention to the differences in color codes between HTML and LaTeX.
-#' @param background A character string for column background color. Here please
-#' pay attention to the differences in color codes between HTML and LaTeX.
-#' @param align A character string for cell alignment. For HTML, possible values could
-#' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit`
-#' while for LaTeX, you can only choose from `l`, `c` & `r`.
-#' @param font_size Only if you want to specify font size locally in HTML.
-#' This feature is not available in LaTeX
+#' @param background A character string for background color. Here please
+#' pay attention to the differences in color codes between HTML and LaTeX. Also
+#' note that in HTML, background defined in cell_spec won't cover the whole
+#' cell.
+#' @param align A character string for cell alignment. For HTML, possible
+#' values could be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`,
+#' `initial` and `inherit` while for LaTeX, you can only choose
+#' from `l`, `c` & `r`.
+#' @param font_size j
#' @param angle 0-360, degree that the text will rotate. Can be a vector.
#' @param tooltip A vector of strings to be displayed as tooltip.
-#' Of course, this feature is only available in HTML.
+#' Obviously, this feature is only available in HTML.
#' @param background_as_tile T/F value indicating if you want to have round
-#' cornered tile as background.
+#' cornered tile as background in HTML.
+#' @param latex_background_in_cell T/F value. It only takes effect in LaTeX
+#' when `background` provided, Default value is `TRUE`. If it's `TRUE`, the
+#' background only works in a table cell. If it's `FALSE`, it works outside of a
+#' table environment.
#'
#' @export
cell_spec <- function(x, format,
bold = F, italic = F, monospace = F,
color = NULL, background = NULL,
align = NULL, font_size = NULL, angle = NULL,
- tooltip = NULL, background_as_tile = TRUE) {
+ tooltip = NULL, background_as_tile = TRUE,
+ latex_background_in_cell = TRUE) {
if (missing(format) || is.null(format)) format = getOption('knitr.table.format')
if (is.null(format)) {
@@ -46,7 +50,8 @@
}
if (tolower(format) == "latex") {
return(cell_spec_latex(x, bold, italic, monospace,
- color, background, align, angle))
+ color, background, align, font_size, angle,
+ latex_background_in_cell))
}
}
@@ -72,32 +77,34 @@
cell_style <- paste0(cell_style, "text-align: ", align, ";")
}
if (!is.null(font_size)) {
- cell_style <- paste0(cell_style, "font-size: ", font_size, "px;")
+ if (is.numeric) font_size <- paste0(font_size, "px")
+ cell_style <- paste0(cell_style, "font-size: ", font_size, ";")
}
- if (!is.null(angle)) {
- cell_style <- paste0(cell_style,
- "-webkit-transform: rotate(", angle,
- "deg); -moz-transform: rotate(", angle,
- "deg); -ms-transform: rotate(", angle,
- "deg); -o-transform: rotate(", angle,
- "deg); transform: rotate(", angle,
- "deg);")
- }
-
if (!is.null(tooltip)) {
tooltip <- gsub("\n", "
", tooltip)
tooltip <- paste0("data-toggle='tooltip' title='", tooltip, "'")
}
out <- paste0(
- '<div style="', cell_style, '"', tooltip, '>', x, '</div>'
+ '<span style="', cell_style, '"', tooltip, '>', x, '</span>'
)
+
+ if (!is.null(angle)) {
+ rotate_css <- paste0("-webkit-transform: rotate(", angle,
+ "deg); -moz-transform: rotate(", angle,
+ "deg); -ms-transform: rotate(", angle,
+ "deg); -o-transform: rotate(", angle,
+ "deg); transform: rotate(", angle,
+ "deg);")
+ out <- paste0('<div style="', rotate_css, '">', out, '</div>')
+ }
return(out)
}
cell_spec_latex <- function(x, bold, italic, monospace,
- color, background, align, angle) {
+ color, background, align, font_size, angle,
+ latex_background_in_cell) {
if (bold) x <- paste0("\\bfseries{", x, "}")
- if (italic) x <-paste0("\\em{", x, "}")
+ if (italic) x <- paste0("\\em{", x, "}")
if (monospace) x <- paste0("\\ttfamily{", x, "}")
if (!is.null(color)) {
color <- latex_color(color)
@@ -105,9 +112,27 @@
}
if (!is.null(background)) {
background <- latex_color(background)
- x <- paste0("\\cellcolor", background, "{", x, "}")
+ background_env <- ifelse(latex_background_in_cell, "cellcolor", "colorbox")
+ x <- paste0("\\", background_env, background, "{", x, "}")
}
- if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}")
+ if (!is.null(font_size)) {
+ x <- paste0("\\begingroup\\fontsize{", font_size, "}{", as.numeric(font_size) + 2,
+ "}\\selectfont ", x, "\\endgroup")
+ }
if (!is.null(angle)) x <- paste0("\\rotatebox{", angle, "}{", x, "}")
+ if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}")
return(x)
}
+
+#' @rdname cell_spec
+#' @export
+text_spec <- function(x, format,
+ bold = F, italic = F, monospace = F,
+ color = NULL, background = NULL,
+ align = NULL, font_size = NULL, angle = NULL,
+ tooltip = NULL, background_as_tile = TRUE,
+ latex_background_in_cell = FALSE) {
+ cell_spec(x, format, bold, italic, monospace, color, background, align,
+ font_size, angle, tooltip, background_as_tile,
+ latex_background_in_cell)
+}