haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.
https://haozhu233.github.io/kableExtra/
Other
688 stars 147 forks source link

`linesep` isn't applied in correct colors to striped booktabs LaTeX tables #616

Open mine-cetinkaya-rundel opened 3 years ago

mine-cetinkaya-rundel commented 3 years ago

When using kable_styling(latex_options = "striped") along with linesep = "\\addlinespace" in kbl(), the additional space between lines are all white. This makes gray rows appear thinner than white rows. I'm not sure if this is a bug or if there is a workaround I've been unable to find, but the current output doesn't look as visually pleasing as it could.

Below is a reprex. I made the stripe color red to provide more visual contrast, even though I'm using the default gray in my document.

---
output: pdf_document
---

```{r}
library(kableExtra)

mtcars %>%
  head(n = 10) %>%
  kable(linesep = "\\addlinespace", booktabs = TRUE) %>%
  kable_styling(latex_options = "striped", stripe_color = "red")


![image](https://user-images.githubusercontent.com/5965649/118415843-9ffd4700-b6a4-11eb-88a9-e9852f31e40c.png)
vincentarelbundock commented 9 months ago

For reference, here is the LaTeX output. To fully solve this, there should a way to color \addlinespace, but this would have to be a completely different mechanism than the \cellcolor we currently use. Frankly, my guess is that this would take a lot of developer hours for a rather niche feature.

\begin{table}
\centering
\begin{tabular}[t]{lrrrrrrrrrrr}
\toprule
  & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
\midrule
\cellcolor{red}{Mazda RX4} & \cellcolor{red}{21.0} & \cellcolor{red}{6} & \cellcolor{red}{160.0} & \cellcolor{red}{110} & \cellcolor{red}{3.90} & \cellcolor{red}{2.620} & \cellcolor{red}{16.46} & \cellcolor{red}{0} & \cellcolor{red}{1} & \cellcolor{red}{4} & \cellcolor{red}{4}\\
\addlinespace
Mazda RX4 Wag & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4\\
\addlinespace
\cellcolor{red}{Datsun 710} & \cellcolor{red}{22.8} & \cellcolor{red}{4} & \cellcolor{red}{108.0} & \cellcolor{red}{93} & \cellcolor{red}{3.85} & \cellcolor{red}{2.320} & \cellcolor{red}{18.61} & \cellcolor{red}{1} & \cellcolor{red}{1} & \cellcolor{red}{4} & \cellcolor{red}{1}\\
\addlinespace
Hornet 4 Drive & 21.4 & 6 & 258.0 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1\\
\addlinespace
\cellcolor{red}{Hornet Sportabout} & \cellcolor{red}{18.7} & \cellcolor{red}{8} & \cellcolor{red}{360.0} & \cellcolor{red}{175} & \cellcolor{red}{3.15} & \cellcolor{red}{3.440} & \cellcolor{red}{17.02} & \cellcolor{red}{0} & \cellcolor{red}{0} & \cellcolor{red}{3} & \cellcolor{red}{2}\\
\addlinespace
Valiant & 18.1 & 6 & 225.0 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1\\
\addlinespace
\cellcolor{red}{Duster 360} & \cellcolor{red}{14.3} & \cellcolor{red}{8} & \cellcolor{red}{360.0} & \cellcolor{red}{245} & \cellcolor{red}{3.21} & \cellcolor{red}{3.570} & \cellcolor{red}{15.84} & \cellcolor{red}{0} & \cellcolor{red}{0} & \cellcolor{red}{3} & \cellcolor{red}{4}\\
\addlinespace
Merc 240D & 24.4 & 4 & 146.7 & 62 & 3.69 & 3.190 & 20.00 & 1 & 0 & 4 & 2\\
\addlinespace
\cellcolor{red}{Merc 230} & \cellcolor{red}{22.8} & \cellcolor{red}{4} & \cellcolor{red}{140.8} & \cellcolor{red}{95} & \cellcolor{red}{3.92} & \cellcolor{red}{3.150} & \cellcolor{red}{22.90} & \cellcolor{red}{1} & \cellcolor{red}{0} & \cellcolor{red}{4} & \cellcolor{red}{2}\\
\addlinespace
Merc 280 & 19.2 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.30 & 1 & 0 & 4 & 4\\
\bottomrule
\end{tabular}
\end{table}