kableExtra icon indicating copy to clipboard operation
kableExtra copied to clipboard

Line-wrapping in column makes collapse_rows(valign) fail in latex

Open DesiQuintans opened this issue 5 years ago • 2 comments

This appears related to #297. If the width of a column is manually set with column_spec() and the contents of the column wrap, then the vertical alignment of items in collapsed rows is no longer correct.

Reprex at the bottom. Consider a table where the 3rd column is given a width, but the contents do not wrap. The text in columns a and b are top-aligned as requested:

image

Now take the same table but apply a width to the 4th column, whose contents will wrap. The alignments of columns a and b go to the middle instead:

image

Unlike #297, setting kable_styling(full_width = TRUE) does not fix the issue, it instead produces a new error:

## ! Extra }, or forgotten \endgroup.
## <recently read> \egroup 

Calling column_spec() before collapse_rows() also does not fix the issue.

To Reproduce

---
title: "Untitled"
output: 
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(knitr)
library(kableExtra)
library(magrittr)
```

```{r echo=FALSE}
df <- 
    data.frame(stringsAsFactors=FALSE,
               a = c("a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b"),
               b = c("a1", "a1", "a1", "a2", "a2", "a2", "b1", "b1", "b1", "b2",
                     "b2", "b2"),
               c = 1:12,
               d = c("Lorem ipsum dolor sit amet", "consectetur adipiscing elit", 
                     "sed do eiusmod tempor", "incididunt ut labore et", 
                     "dolore magna aliqua", "Ut enim ad minim veniam", 
                     "quis nostrud exercitation ullamco", "laboris nisi ut aliquip", 
                     "ex ea commodo consequat", "Lorem ipsum dolor sit amet", 
                     "consectetur adipiscing elit", "sed do eiusmod tempor"))
```

```{r}
kable(df, "latex", booktabs = TRUE, longtable = TRUE) %>% 
    column_spec(3, width = "10em") %>% 
    collapse_rows(columns = 1:2, valign = "top", latex_hline = "major")
```


```{r}
kable(df, "latex", booktabs = TRUE, longtable = TRUE) %>% 
    column_spec(4, width = "10em") %>% 
    collapse_rows(columns = 1:2, valign = "top", latex_hline = "major")
```
\pagebreak

```{r}
kable(df, "latex", booktabs = TRUE, longtable = TRUE) %>% 
    collapse_rows(columns = 1:2, valign = "top", latex_hline = "major") %>% 
    kable_styling(full_width = TRUE) 
```

DesiQuintans avatar Aug 22 '19 02:08 DesiQuintans

I came here to report the same bug. I should have checked the GitHub issues first before creating a reproducible example. I wish I could help debug this and submit a PR, but I have close to no knowledge of LaTeX.

BrunoGrandePhD avatar Sep 06 '19 15:09 BrunoGrandePhD

Exactly the same problem with me. Tried fiddling with setting column_specs for each row but that didn't help.

Below is the diff of the latex code which is generated from two versions of my rmarkdown report (with and without the column_spec line as in Desi's post above) - the first line gives a correctly formatted first column (the grouping variable) but no wrapping of the 5th column, and the second line is what the addition of column_spec(5, width = "6cm") uses, where the 5th column wraps correctly but the first column is not top aligned.

< \begin{longtable}[t]{llrlllr}
---
> \begin{longtable}[t]{llrl>{\raggedright\arraybackslash}p{6cm}lr}

My latex knowledge isn't enough to work out what is going on here.

p-phillips avatar Sep 25 '19 11:09 p-phillips