haozhu233 / kableExtra

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

collapse_rows() doesn't end up with vertically centred entry #56

Closed EhrmannS closed 7 years ago

EhrmannS commented 7 years ago

Hi!

I have the following example. It results in a table, where car 1 is to be found somewhere in the 4th fifth of the vertical distribution, rather than directly in the middle. This creates for more complex tables and longer values in this "collapsed cell" rather strange behaviour and sometimes the text even crosses the lower boundary of the group. It seems almost as if the value would be centred between the two maximum border of the table per page, and not between the two borders of the group that should be collapsed.

---
documentclass: book
lof: no
lot: no
output:
  bookdown::pdf_book:
    fig_caption: yes
    keep_tex: yes
    number_sections: yes
  bookdown::html_document2:
    fig_caption: yes
    theme: spacelab
subtitle: null
---
library(knitr)
library(magrittr)
library(bookdown)
library(captioner)
library(stringr)
library(dplyr)
library(kableExtra)
long_dt <- cbind(car = row.names(mtcars), mtcars)
rownames(long_dt) <- NULL
long_dt[,1] <- c(rep("car 1", 22), rep("car 2", 10))

kable(long_dt, 
      longtable = T,
      format = "latex",
      booktabs = T, 
      caption.short = "An awesome \\textsc{Longtable}.",
      caption = "An awesome Longtable, with a caption.") %>%
  add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6)) %>%
  kable_styling(latex_options = "repeat_header") %>%
  collapse_rows(columns = 1)

Even more strangely, if you set the line long_dt[,1] <- c(rep("car 1", 22), rep("car 2", 10)) to long_dt[,1] <- c(rep("car 1", 16), rep("car 2", 16)), car 2 is printed where the repeated header on the second page would be printed otherwise.

If I compile it to html it works without problems.

haozhu233 commented 7 years ago

Hi, @EhrmannS, it seems like booktabs' cmidrule messed up the multirow's calculation..

https://tex.stackexchange.com/questions/66564/vertical-alignment-using-multirow-and-booktabs

I don't know if I can find a good solution for this. I can certainly remove those midlines. I don't like them either but they are important for tables with multiple header columns. If we keep those midlines, TeX people say you can manually add adjustments to multirow. I can certainly simulate it via code but it seems like it will bring me more issues in the future.

EhrmannS commented 7 years ago

I tried out another package (https://github.com/hughjonesd/huxtable) and it seems as if a very similar problem exists that might be connected to latex (https://github.com/hughjonesd/huxtable/issues/9).

Just wanted to let you know. I will use a work-around for my table that doesn't require collapsing the column in the first place.

slyrus commented 4 years ago

Did anyone ever come up with a proper solution for this?

haozhu233 commented 4 years ago

@slyrus We are adding a page breaking mechanism in the latest version (not on cran yet). Check #314 for details.