Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug of striped table when there are two sets of grouped columns #31

Closed
stla opened this issue Jul 31, 2017 · 6 comments
Closed

Bug of striped table when there are two sets of grouped columns #31

stla opened this issue Jul 31, 2017 · 6 comments

Comments

@stla
Copy link

stla commented Jul 31, 2017

Hello,
The Rnw file below produces an unexpected result.

\documentclass[table]{article}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{xcolor}

\begin{document}

<<>>=
library(knitr)
library(kableExtra)
options(knitr.table.format = "latex")
dat <- rbind(mtcars, mtcars)
@

<<results='asis'>>=
kable(dat, booktabs=TRUE, longtable=TRUE, 
      caption="A long striped table") %>%
  add_header_above(c(" ", "Group" = 5, " ", "Other Group" = 5)) %>%
  kable_styling(latex_options = c("striped", "repeat_header"))
@

\end{document}

There are two problems. Firstly, the second line of the header on the first page is colored:

capture1

Secondly, on the second page the caption and the first line of the header are colored:

capture2

Below is the generated LaTeX code:

\rowcolors{2}{white}{gray!6}

\begin{longtable}[t]{lrrrrrrrrrrr}
\caption{\label{tab:unnamed-chunk-2}A long striped table}\\
\toprule
\multicolumn{1}{c}{ } & \multicolumn{5}{c}{Group} & \multicolumn{1}{c}{ } & \multicolumn{5}{c}{Other Group} \\
\cmidrule(l{2pt}r{2pt}){2-6} \cmidrule(l{2pt}r{2pt}){8-12}
  & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
\midrule
\endfirsthead
\caption{\label{tab:unnamed-chunk-2}A long striped table \textit{(continued)}}\\
\toprule
\multicolumn{1}{c}{ } & \multicolumn{5}{c}{Group} & \multicolumn{1}{c}{ } & \multicolumn{5}{c}{Other Group} \\
\cmidrule(l{2pt}r{2pt}){2-6} \cmidrule(l{2pt}r{2pt}){8-12}
  & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
\midrule
\endhead
Mazda RX4 & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4\\
Mazda RX4 Wag & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4\\
Datsun 710 & 22.8 & 4 & 108.0 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1\\
Hornet 4 Drive & 21.4 & 6 & 258.0 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1\\
Hornet Sportabout & 18.7 & 8 & 360.0 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2\\
\addlinespace
Valiant & 18.1 & 6 & 225.0 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1\\
Duster 360 & 14.3 & 8 & 360.0 & 245 & 3.21 & 3.570 & 15.84 & 0 & 0 & 3 & 4\\
@stla
Copy link
Author

stla commented Jul 31, 2017

In case if this is a LaTeX issue, I've opened the issue on tex.stackexchange: https://tex.stackexchange.com/questions/384100/bug-with-striped-long-table-when-there-are-two-groups-of-columns.

@stla
Copy link
Author

stla commented Jul 31, 2017

The LaTeX solution posted by Ulrike Fisher works, using hiderowcolors and showrowcolors:

\begin{longtable}[t]{lrrrrrrrrrrr}
\hiderowcolors
\caption{A long striped table}\\
\toprule
\multicolumn{1}{c}{ } & \multicolumn{5}{c}{Group} & \multicolumn{1}{c}{ } & \multicolumn{5}{c}{Other Group} \\
\cmidrule(l{2pt}r{2pt}){2-6} \cmidrule(l{2pt}r{2pt}){8-12}
 & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
\midrule
\endfirsthead
\hiderowcolors
\caption{A long striped table \textit{(continued)}}\\
\toprule
\multicolumn{1}{c}{ } & \multicolumn{5}{c}{Group} & \multicolumn{1}{c}{ } & \multicolumn{5}{c}{Other Group} \\
\cmidrule(l{2pt}r{2pt}){2-6} \cmidrule(l{2pt}r{2pt}){8-12}
  & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
\midrule
\endhead
\showrowcolors
Mazda RX4 & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4\\
.........

@stla
Copy link
Author

stla commented Jul 31, 2017

This works like this:

<<results='asis'>>=
x <- kable(dat, booktabs=TRUE, longtable=TRUE, 
      caption="A long striped table") %>%
  add_header_above(c(" ", "Group"=5, " ", "Other Group" = 5)) %>%
  kable_styling(latex_options = c("striped", "repeat_header"))
sub("\\\\endhead", "\\\\endhead\\\\showrowcolors", gsub("\\\\toprule", "\\\\hiderowcolors\\\\toprule", x))
@

So I believe you can fix the function styling_latex_striped like this:

styling_latex_striped <- function(x, table_info) {
  # 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"
  }
  x <- sub("\\\\endhead", "\\\\endhead\\\\showrowcolors", gsub("\\\\toprule", "\\\\hiderowcolors\\\\toprule", x))
  return(paste0(row_color, x, "\n\\rowcolors{2}{white}{white}"))
}

@haozhu233
Copy link
Owner

Awesome! Thank you so much for doing this! I will make a fix later today.

@stla
Copy link
Author

stla commented Jul 31, 2017

Great. Thanks. And big thanks for this nice package!

@haozhu233
Copy link
Owner

I put in some extra codes to control the behavior in all conditions. Thank you @stla for making it happening!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants