improve the look of striped rows in longtable
diff --git a/R/kable_styling.R b/R/kable_styling.R
index a721c34..98e6039 100644
--- a/R/kable_styling.R
+++ b/R/kable_styling.R
@@ -182,7 +182,7 @@
table_info <- magic_mirror(kable_input)
if ("striped" %in% latex_options) {
- out <- styling_latex_striped(out)
+ out <- styling_latex_striped(out, table_info)
}
# hold_position is only meaningful in a table environment
@@ -214,11 +214,15 @@
return(out)
}
-styling_latex_striped <- function(x) {
+styling_latex_striped <- function(x, table_info) {
usepackage_latex("xcolor", "table")
- paste0(
- # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
- "\\rowcolors{2}{gray!6}{white}\n", x, "\n\\rowcolors{2}{white}{white}")
+ # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
+ if (table_info$tabular == "longtable" & !is.na(table_info$caption)) {
+ row_color <- "\\rowcolors{2}{white}{gray!6}\n"
+ } else {
+ row_color <- "\\rowcolors{2}{gray!6}{white}\n"
+ }
+ return(paste0(row_color, x, "\n\\rowcolors{2}{white}{white}"))
}
styling_latex_hold_position <- function(x) {
diff --git a/tests/visual_tests/longtable.Rmd b/tests/visual_tests/longtable.Rmd
index fa0668a..1c27632 100644
--- a/tests/visual_tests/longtable.Rmd
+++ b/tests/visual_tests/longtable.Rmd
@@ -16,15 +16,14 @@
dt <- rbind(mtcars, mtcars)
-kable(dt, "latex", longtable = T, caption="test table") %>%
- add_header_above(c(" ", "a" = 6, "b" = 5)) %>%
- kable_styling(latex_options = "repeat_header")
+# kable(dt, "latex", longtable = T, caption="test table") %>%
+# add_header_above(c(" ", "a" = 6, "b" = 5)) %>%
+# kable_styling(latex_options = "repeat_header")
```
```{r}
-kable(dt, "latex", longtable = T, caption="table2", booktabs = T) %>%
- add_header_above(c(" ", "a" = 6, "b" = 5)) %>%
- kable_styling(latex_options = "repeat_header", repeat_header_text = "(...)")
+kable(dt, "latex", longtable = T, caption = "test", booktabs = T) %>%
+ kable_styling(latex_options = c("striped", "repeat_header"), repeat_header_text = "(...)")
```
```{r}