Better regex and documentation
diff --git a/R/kable_styling.R b/R/kable_styling.R
index 55a342c..bf33f82 100644
--- a/R/kable_styling.R
+++ b/R/kable_styling.R
@@ -48,13 +48,18 @@
#' 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 escaping. Has no effect unless HTML format
-#' is chosen: this is for including complicated math in HTML output.
+#' 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)
@@ -132,16 +137,16 @@
extract_latex_from_kable <- function(kable_input) {
kable_attrs <- attributes(kable_input)
- regexp <- "(^|[^\\\\])([$][^$]*[^$\\\\]+[$]|[$][$][^$]*[^$\\\\]+[$][$])"
+ regexp <- paste0("(?<!\\e)", # Not escaped
+ "([$]{1}(?![ ])[^$]+(?<![$\\\\ ])[$]{1}", # $...$
+ "|[$]{2}(?![ ])[^$]+(?<![$\\\\ ])[$]{2})", # $$...$$
+ "(?!\\d)") # Not followed by digit
latex <- character()
- while (grepl(regexp, kable_input)) {
- block <- str_replace(str_extract(kable_input, regexp),
- regexp,
- "\\2")
-
+ 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, paste0("\\1", name))
+ kable_input <- str_replace(kable_input, regexp, name)
}
kable_attrs$extracted_latex <- latex
attributes(kable_input) <- kable_attrs
diff --git a/man/kable_styling.Rd b/man/kable_styling.Rd
index 9a8f935..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, protect_latex = FALSE)
+ latex_table_env = NULL, protect_latex = TRUE)
}
\arguments{
\item{kable_input}{Output of \code{knitr::kable()} with \code{format} specified}
@@ -67,8 +67,7 @@
could be supported in self-defined environments.}
\item{protect_latex}{If \code{TRUE}, LaTeX code embedded between dollar signs
-will be protected from escaping. Has no effect unless HTML format
-is chosen: this is for including complicated math in HTML output.}
+will be protected from HTML escaping.}
}
\description{
This function provides a cleaner approach to modify the style
@@ -82,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{