Fix #9
diff --git a/R/magic_mirror.R b/R/magic_mirror.R
index 7a274bc..e00032c 100644
--- a/R/magic_mirror.R
+++ b/R/magic_mirror.R
@@ -70,7 +70,7 @@
)
# Contents
kable_info$contents <- str_match_all(kable_input, "\n(.*)\\\\\\\\")[[1]][,2]
- kable_info$contents <- sub("\\\\", "\\\\\\\\", kable_info$contents)
+ kable_info$contents <- latex_contents_escape(kable_info$contents)
if (kable_info$tabular == "longtable" & !is.na(kable_info$caption)) {
kable_info$contents <- kable_info$contents[-1]
}
@@ -86,6 +86,14 @@
return(kable_info)
}
+latex_contents_escape <- function(x) {
+ x <- gsub("\\\\", "\\\\\\\\", x)
+ x <- gsub("\\(", "\\\\(", x)
+ x <- gsub("\\)", "\\\\)", x)
+ x <- gsub("\\[", "\\\\]", x)
+ x <- gsub("\\[", "\\\\]", x)
+}
+
#' Magic Mirror for html table --------
#'
#' @param kable_input The output of kable
diff --git a/tests/visual_tests/add_indent_and_group_rows_pdf.Rmd b/tests/visual_tests/add_indent_and_group_rows_pdf.Rmd
index 406c695..d6f6a10 100644
--- a/tests/visual_tests/add_indent_and_group_rows_pdf.Rmd
+++ b/tests/visual_tests/add_indent_and_group_rows_pdf.Rmd
@@ -35,3 +35,21 @@
group_rows("Group 2", 8, 10)
```
+```{r}
+rbind(dt, dt, dt) %>%
+kable(format = "latex", booktabs = T) %>%
+ kable_styling() %>%
+ group_rows("Group 1", 4, 7) %>%
+ group_rows("Group 2", 8, 10) %>%
+ group_rows("Group 2", 20, 22)
+
+aa <- mtcars
+for (i in 1:nrow(aa)) {
+ aa$hp[i] <- paste0(aa$hp[i], " (", 1, ")")
+}
+kable(head(aa, n = 20), caption = "test", booktabs = T, format = "latex") %>%
+ kable_styling() %>%
+ group_rows("these rows", 4, 16) %>%
+ group_rows("Those rows", 17,20)
+```
+