Merge, rebuild man pages
Merge branch 'master' of https://github.com/haozhu233/kableExtra
# Conflicts:
# man/kable_styling.Rd
diff --git a/DESCRIPTION b/DESCRIPTION
index 201ba3d..d0ffdc2 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -15,7 +15,8 @@
person('Yoni', 'Sidi', role = 'ctb'),
person('Brian', 'Salzer', role = 'ctb'),
person('George', 'Gui', role = 'ctb'),
- person('Yeliang', 'Fan', role = 'ctb')
+ person('Yeliang', 'Fan', role = 'ctb'),
+ person('Duncan', 'Murdoch', role = 'ctb')
)
Description: Build complex HTML or 'LaTeX' tables using 'kable()' from 'knitr'
and the piping syntax from 'magrittr'. Function 'kable()' is a light weight
@@ -45,7 +46,8 @@
rstudioapi,
glue,
tools,
- webshot
+ webshot,
+ digest
Suggests:
testthat,
magick,
@@ -53,5 +55,5 @@
dplyr
VignetteBuilder: knitr
Encoding: UTF-8
-RoxygenNote: 6.1.0
+RoxygenNote: 6.1.1
Roxygen: list(markdown = TRUE)
diff --git a/NAMESPACE b/NAMESPACE
index ab0fb47..b247396 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -37,6 +37,7 @@
export(usepackage_latex)
export(xtable2kable)
import(htmltools)
+importFrom(digest,digest)
importFrom(glue,glue)
importFrom(grDevices,col2rgb)
importFrom(knitr,asis_output)
@@ -57,6 +58,7 @@
importFrom(rvest,html_table)
importFrom(scales,rescale)
importFrom(stats,ave)
+importFrom(stringr,fixed)
importFrom(stringr,str_count)
importFrom(stringr,str_detect)
importFrom(stringr,str_extract)
diff --git a/R/column_spec.R b/R/column_spec.R
index ae9edff..71a4b22 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -230,7 +230,8 @@
kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
- out <- sub(kable_align_old, kable_align_new,
+ out <- sub(paste0("\\{", kable_align_old, "\\}"),
+ paste0("\\{", kable_align_new, "\\}"),
solve_enc(kable_input),
perl = T)
diff --git a/R/kableExtra-package.R b/R/kableExtra-package.R
index a06fbcf..ea2a1da 100644
--- a/R/kableExtra-package.R
+++ b/R/kableExtra-package.R
@@ -55,7 +55,7 @@
#' vanilla rmarkdown. For customized rmarkdown templates, it is recommended to
#' load related LaTeX packages manually.
#'
-#' @importFrom stringr str_count str_split str_match str_detect str_match_all
+#' @importFrom stringr fixed str_count str_split str_match str_detect str_match_all
#' str_extract str_replace_all str_trim str_extract_all str_sub str_replace
#' @importFrom xml2 read_xml xml_attr xml_has_attr xml_attr<- read_html
#' xml_child xml_children xml_name xml_add_sibling xml_add_child xml_text
@@ -75,6 +75,7 @@
#' @importFrom glue glue
#' @importFrom tools file_ext file_path_sans_ext
#' @importFrom webshot webshot
+#' @importFrom digest digest
#' @import htmltools
#' @name kableExtra-package
#' @aliases kableExtra
diff --git a/R/kable_styling.R b/R/kable_styling.R
index ffa474c..bf0bd53 100644
--- a/R/kable_styling.R
+++ b/R/kable_styling.R
@@ -47,11 +47,19 @@
#' @param latex_table_env LaTeX option. A character string to define customized
#' table environment such as tabu or tabularx.You shouldn't expect all features
#' could be supported in self-defined environments.
+#' @param protect_latex If `TRUE`, LaTeX code embedded between dollar signs
+#' will be protected from HTML escaping.
#'
#' @details For LaTeX, if you use other than English environment
#' - all tables are converted to 'UTF-8'. If you use, for example, Hungarian
#' characters on a Windows machine, make sure to use
#' Sys.setlocale("LC_ALL","Hungarian") to avoid unexpected conversions.
+#' - `protect_latex = TRUE` has no effect.
+#'
+#' For HTML,
+#' - `protect_latex = TRUE` is for including complicated math in HTML output.
+#' The LaTeX may not include dollar signs even if they are escaped.
+#' Pandoc's rules for recognizing embedded LaTeX are used.
#'
#' @examples x_html <- knitr::kable(head(mtcars), "html")
#' kable_styling(x_html, "striped", position = "left", font_size = 7)
@@ -71,7 +79,8 @@
repeat_header_method = c("append", "replace"),
repeat_header_continued = FALSE,
stripe_color = "gray!6",
- latex_table_env = NULL) {
+ latex_table_env = NULL,
+ protect_latex = TRUE) {
if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
@@ -104,7 +113,8 @@
bootstrap_options = bootstrap_options,
full_width = full_width,
position = position,
- font_size = font_size))
+ font_size = font_size,
+ protect_latex = protect_latex))
}
if (kable_format == "latex") {
if (is.null(full_width)) {
@@ -125,13 +135,44 @@
}
}
+extract_latex_from_kable <- function(kable_input) {
+ kable_attrs <- attributes(kable_input)
+ regexp <- paste0("(?<!\\e)", # Not escaped
+ "([$]{1}(?![ ])[^$]+(?<![$\\\\ ])[$]{1}", # $...$
+ "|[$]{2}(?![ ])[^$]+(?<![$\\\\ ])[$]{2})", # $$...$$
+ "(?!\\d)") # Not followed by digit
+ latex <- character()
+ while (str_detect(kable_input, regexp)) {
+ block <- str_extract(kable_input, regexp)
+ name <- paste0("latex", digest(block))
+ latex[name] <- block
+ kable_input <- str_replace(kable_input, regexp, name)
+ }
+ kable_attrs$extracted_latex <- latex
+ attributes(kable_input) <- kable_attrs
+ kable_input
+}
+
+replace_latex_in_kable <- function(kable_input, latex) {
+ kable_attrs <- attributes(kable_input)
+ for (n in names(latex)) {
+ kable_input <- str_replace_all(kable_input, fixed(n), latex[n])
+ }
+ attributes(kable_input) <- kable_attrs
+ kable_input
+}
+
# htmlTable Styling ------------
htmlTable_styling <- function(kable_input,
bootstrap_options = "basic",
full_width = T,
position = c("center", "left", "right",
"float_left", "float_right"),
- font_size = NULL) {
+ font_size = NULL,
+ protect_latex = TRUE) {
+ if (protect_latex) {
+ kable_input <- extract_latex_from_kable(kable_input)
+ }
kable_attrs <- attributes(kable_input)
kable_xml <- read_kable_as_xml(kable_input)
@@ -189,6 +230,10 @@
}
out <- as_kable_xml(kable_xml)
+ if (protect_latex) {
+ out <- replace_latex_in_kable(out, kable_attrs$extracted_latex)
+ kable_attrs$extracted_latex <- NULL
+ }
attributes(out) <- kable_attrs
if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
return(out)
diff --git a/man/add_header_above.Rd b/man/add_header_above.Rd
index ee4ad8c..5174db8 100644
--- a/man/add_header_above.Rd
+++ b/man/add_header_above.Rd
@@ -16,7 +16,7 @@
\item{header}{A (named) character vector with \code{colspan} as values. For
example, \code{c(" " = 1, "title" = 2)} can be used to create a new header row
for a 3-column table with "title" spanning across column 2 and 3. For
-convenience, when \code{colspan} equals to 1, users can drop the \code{= 1} part.
+convenience, when \code{colspan} equals to 1, users can drop the \code{ = 1} part.
As a result, \code{c(" ", "title" = 2)} is the same as \code{c(" " = 1, "title" = 2)}.}
\item{bold}{A T/F value to control whether the text should be bolded.}
diff --git a/man/footnote_marker_number.Rd b/man/footnote_marker_number.Rd
index bc16375..599f284 100644
--- a/man/footnote_marker_number.Rd
+++ b/man/footnote_marker_number.Rd
@@ -20,7 +20,7 @@
default value from global option \code{knitr.table.format}.}
\item{double_escape}{T/F if output is in LaTeX, whether it should be double
-escaped. If you are using footnote_marker in \code{group_rows`` labeling row or}add_header_above\code{, you need to set this to be}TRUE`.}
+escaped. If you are using footnote_marker in \code{group_rows`` labeling row or }add_header_above\code{, you need to set this to be }TRUE`.}
}
\description{
Put footnote mark in superscription in table. Unless you are
diff --git a/man/kable_styling.Rd b/man/kable_styling.Rd
index 608e6f9..a3932fc 100644
--- a/man/kable_styling.Rd
+++ b/man/kable_styling.Rd
@@ -10,7 +10,7 @@
repeat_header_text = "\\\\textit{(continued)}",
repeat_header_method = c("append", "replace"),
repeat_header_continued = FALSE, stripe_color = "gray!6",
- latex_table_env = NULL)
+ latex_table_env = NULL, protect_latex = TRUE)
}
\arguments{
\item{kable_input}{Output of \code{knitr::kable()} with \code{format} specified}
@@ -65,6 +65,9 @@
\item{latex_table_env}{LaTeX option. A character string to define customized
table environment such as tabu or tabularx.You shouldn't expect all features
could be supported in self-defined environments.}
+
+\item{protect_latex}{If \code{TRUE}, LaTeX code embedded between dollar signs
+will be protected from HTML escaping.}
}
\description{
This function provides a cleaner approach to modify the style
@@ -78,6 +81,14 @@
\item all tables are converted to 'UTF-8'. If you use, for example, Hungarian
characters on a Windows machine, make sure to use
Sys.setlocale("LC_ALL","Hungarian") to avoid unexpected conversions.
+\item \code{protect_latex = TRUE} has no effect.
+}
+
+For HTML,
+\itemize{
+\item \code{protect_latex = TRUE} is for including complicated math in HTML output.
+The LaTeX may not include dollar signs even if they are escaped.
+Pandoc's rules for recognizing embedded LaTeX are used.
}
}
\examples{
diff --git a/man/save_kable.Rd b/man/save_kable.Rd
index 0b80071..562ac79 100644
--- a/man/save_kable.Rd
+++ b/man/save_kable.Rd
@@ -4,7 +4,9 @@
\alias{save_kable}
\title{Save kable to files}
\usage{
-save_kable(x, file, bs_theme = "simplex", self_contained = TRUE, ...)
+save_kable(x, file, bs_theme = "simplex", self_contained = TRUE,
+ extra_dependencies = NULL, ..., latex_header_includes = NULL,
+ keep_tex = FALSE)
}
\arguments{
\item{x}{A piece of HTML code for tables, usually generated by kable and
@@ -12,13 +14,26 @@
\item{file}{save to files. If the input table is in HTML and the output file
ends with \code{.png}, \code{.pdf} and \code{.jpeg}, \code{webshot} will be used to do the
-conversion}
+conversion.}
\item{bs_theme}{Which Bootstrap theme to use}
\item{self_contained}{Will the files be self-contained?}
-\item{...}{Additional variables being passed to \code{webshot::webshot}.`}
+\item{extra_dependencies}{Additional HTML dependencies. For example,
+\code{list(}}
+
+\item{...}{Additional variables being passed to \code{webshot::webshot}. This
+is for HTML only.}
+
+\item{latex_header_includes}{A character vector of extra LaTeX header stuff.
+Each element is a row. You can have things like
+\code{c("\\\\usepackage{threeparttable}", "\\\\usepackage{icons}")} You could
+probably add your language package here if you use non-English text in your
+table, such as \code{\\\\usepackage[magyar]{babel}}.}
+
+\item{keep_tex}{A T/F option to control if the latex file that is initially created
+should be kept. Default is \code{FALSE}.}
}
\description{
Save kable to files
diff --git a/man/scroll_box.Rd b/man/scroll_box.Rd
index 440dcc5..1cc6742 100644
--- a/man/scroll_box.Rd
+++ b/man/scroll_box.Rd
@@ -5,7 +5,8 @@
\title{Put a HTML table into a scrollable box}
\usage{
scroll_box(kable_input, height = NULL, width = NULL,
- box_css = "border: 1px solid #ddd; padding: 5px; ", extra_css = NULL)
+ box_css = "border: 1px solid #ddd; padding: 5px; ", extra_css = NULL,
+ fixed_thead = list(enabled = F, background = "#fff"))
}
\arguments{
\item{kable_input}{A HTML kable object}
@@ -17,6 +18,9 @@
\item{box_css}{CSS text for the box}
\item{extra_css}{Extra CSS styles}
+
+\item{fixed_thead}{A list of two named element. enabled and background.
+Default is F and white, e.g. "list(enabled = T, background = "#fff")"}
}
\description{
This function will put a HTML kable object in a fixed-height,
diff --git a/vignettes/awesome_table_in_pdf.Rmd b/vignettes/awesome_table_in_pdf.Rmd
index 5be9621..fa84b7f 100644
--- a/vignettes/awesome_table_in_pdf.Rmd
+++ b/vignettes/awesome_table_in_pdf.Rmd
@@ -371,7 +371,7 @@
collapse_rows(columns = 1:2, latex_hline = "major", valign = "middle")
```
-Right now, you can't automatically make striped rows based on collapsed rows but you can do it manually via the `extra_latex_after` option in `row_spec`. This feature is not officially supported. I'm only document it here if you want to give it a try.
+Right now, you can't automatically make striped rows based on collapsed rows but you can do it manually via the `extra_latex_after` option in `row_spec`. This feature is not officially supported. I'm only documenting it here if you want to give it a try.
```{r}
kable(collapse_rows_dt[-1], "latex", align = "c", booktabs = T) %>%
column_spec(1, bold = T, width = "5em") %>%
@@ -398,7 +398,7 @@
collapse_rows(1:3, row_group_label_position = 'stack')
```
-To better distinguish different layers, you can format the each layer using `row_group_label_fonts`. You can also customize the hlines to better differentiate groups.
+To better distinguish different layers, you can format each layer using `row_group_label_fonts`. You can also customize the hlines to better differentiate groups.
```{r}
row_group_label_fonts <- list(