kableExtra
kableExtra copied to clipboard
Background color for column does not work when using full_width
A column in a table should have a background color. When using full_width = TRUE
the background color does not appear. As you can see in the output the cells are marked with \cellcolor{green}
but in the pdf output the column is not green. Also I can see in the output that the first row of the colored column has not this tag \cellcolor{green}
, which will be most likely the next problem. When the full_width = TRUE
is removed the column gets coloured but not for the first row (as expected).
---
title: "Untitled"
output: pdf_document
---
```{r}
library(dplyr)
data(mtcars)
temp2 <- mtcars[1:5, 1:3]
```
```{r, results='asis'}
cat(
kableExtra::kbl(
x = temp2,
format = "latex",
digits = 2,
col.names = NULL,
booktabs = TRUE,
position = "h",
toprule = NULL,
bottomrule = NULL,
linesep = ""
) %>%
kableExtra::kable_styling(full_width = TRUE) %>%
kableExtra::column_spec(column = 2, background = "green") %>%
kableExtra::row_spec(row = nrow(temp2) - 2, hline_after = TRUE) %>%
kableExtra::row_spec(row = nrow(temp2) - 1, bold = TRUE, extra_latex_after = "\\hline \\hline")
)
```
So glad to see this issue! It seems full_width and stripes or cell background edits don't work well together.
I found the same problem with full_width and striped rows... Was going crazy trying to add the stripes. They only appear when kable_styling(full_width = F)
.
Strangely, something like: row_spec(seq(1,nrow(data_prospective),2), **color**="red")
works, paired with kable_styling (full_width = T)
but row_spec(seq(1,nrow(data_prospective),2), **background**="red")
doesn't
This has stripes showing when rendered with pdf:
data_prospective %>%
kbl(., booktabs = T, longtable = T) %>%
kable_styling(latex_options = c("striped","repeat_header"),
full_width = F,
font_size = 8) %>%
column_spec(column = 4, width = "5cm")
The same issue of incompatibility between kable_styling
and column_spec
happens for me. A somewhat simplified example is the following:
---
title: "DEMO"
output:
pdf_document:
latex_engine: xelatex
---
```{r, echo=FALSE}
library(kableExtra)
options(knitr.table.format = "latex")
kbl(mtcars[1:7, ], align = "c", booktabs = TRUE, linesep = "") %>%
kable_styling(latex_options = c("striped", "HOLD_position"), full_width = TRUE) %>%
column_spec(1, width = "4cm")
This produces a table without the striping:
The striping is correctly present when full_width = FALSE
.
I believe that this is an upstream problem with the tabu
package in LaTeX:
https://tex.stackexchange.com/questions/496541/using-cellcolor-with-tabu https://tex.stackexchange.com/questions/494689/color-rows-on-table-tabu-package