haozhu233 / kableExtra

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

Wrap-text doesn't work when rows are collapsed and rendered as PDF through Quarto #752

Open russ-dvm opened 1 year ago

russ-dvm commented 1 year ago

Hello,

I am rendering a table in a PDF file using Quarto. If there is text is a row that is collapsed, it doesn't wrap properly when the column_spec width is specified. The table that I am trying to create has several columns and will overflow the page if the text in the first column is not wrapped.

Below is an example - I hope I have provided it in a useful way.

---
title: "test"

format:
  html:
    theme: cosmo
  pdf:
    documentclass: scrreprt
    include-in-header: 
      text: |
        \usepackage{booktabs}
        \usepackage{longtable}
        \usepackage{array}
        \usepackage{multirow}
        \usepackage{wrapfig}
        \usepackage{float}
        \usepackage{colortbl}
        \usepackage{pdflscape}
        \usepackage{tabu}
        \usepackage{threeparttable}
        \usepackage{threeparttablex}
        \usepackage[normalem]{ulem}
        \usepackage{makecell}
        \usepackage{xcolor}
  docx: default
editor: visual
---

```{r, echo = F, message = F, warning=F}

text_tbl <- data.frame(
    Items = c("Really really really really long column item here 1", "Really really really really long column item here 1", "Item 2"), Features = c(
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex.",
    "In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus",
    "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat"
    ))

kbl(text_tbl, booktabs = T) %>% collapse_rows(columns = 1) %>% 
kable_styling(full_width = T) %>% 
column_spec(1, width = "10em")

Running the above in Quarto creates: image

Thanks in advance for any insight

vincentarelbundock commented 11 months ago

Not entirely sure about this, but perhaps we need \multirow{-2}{=} instead of \multirow{-2}{*} as suggested here:

https://tex.stackexchange.com/a/332782/16188

Screenshot and LaTeX document example below:

Screenshot 2023-12-04 204452

\documentclass{article}
\usepackage{tabu}
\usepackage{booktabs}
\usepackage{multirow}

\begin{document}

\begin{tabu} to \linewidth {>{\raggedright\arraybackslash}p{10em}>{\raggedright}X}
\toprule
Items & Features\\
\midrule
 & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex.\\
\cmidrule{2-2}
\multirow{-2}{*}{\raggedright\arraybackslash Really really really really long column item here 1} & In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus\\
\cmidrule{1-2}
Item 2 & Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat\\
\bottomrule
\end{tabu}

\begin{tabu} to \linewidth {>{\raggedright\arraybackslash}p{10em}>{\raggedright}X}
\toprule
Items & Features\\
\midrule
 & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex.\\
\cmidrule{2-2}
\multirow{-2}{=}{\raggedright\arraybackslash Really really really really long column item here 1} & In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus\\
\cmidrule{1-2}
Item 2 & Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat\\
\bottomrule
\end{tabu}

\end{document}