Mark Davies
Mark Davies

Reputation: 940

How to split whole words across lines in kable pdf output to prevent text from overlapping next cell

I am producing a pdf using rmarkdown. My document contains a longtable with a lot of text. By setting the column width with column_spec I can get the text to wrap, but in some cells I need whole words to split, otherwise they flow into the next cell and overlap.

---
title: "Queries"
header-includes:
- \usepackage{pdflscape}
- \usepackage{longtable}
output: 
  pdf_document:
    toc: false
---

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

```{r genpop}
genpop = structure(list(`First author, Year` = c("Hsu  2009", "Hallan  2009", 
"Bash  2010", "Ryu  2009"), Population = c("Medical insurance check up", 
"General population", "Age 45-64", "Men, age 30-59"), `Mean age` = c(40, 
50, 54, 37), `Participants (n)` = c(177570, 65589, 15324, 10685
), `Follow up (years)` = c(25, 10, 16, 3.8), Outcome = c("ESRD", 
"ESRD/Death", "ESRD", "Incident CKD (eGFR<60)"), `1st` = c("BMI (category)", 
"Diabetes (present)", "Diabetes (present)", "Low HDL"), `2nd` = c("Hypertension (stage)", 
"Low physical activity", "Triglycerides", "Triglycerides"), `3rd` = c("Diabetes (Y/N)", 
"SBP", "SBP", " "), `4th` = c("Uric acid (highest quartile)", 
"Low HDL", "BMI", " "), `Other significant` = c("Dipstick proteinuria\\*, race, age, education, low Hb", 
"eGFR\\* \\& ACR\\* (in full model), antihypertensive treatment, male sex, age", 
"Race, smoking, age, CHD, male sex", "HOMA-IR"), `Non-significant` = c("Lipids", 
" ", " ", "Obesity, hypertension, raised fasting glucose")), row.names = c(NA, 
-4L), class = "data.frame")

genpop %>%
    kbl( longtable = TRUE, escape = F, booktabs = T, caption = 'Some text') %>% 
    
    column_spec(1, width = "6em")%>%
    column_spec(2, width = "6em")%>% 
    column_spec(3, width = "3em")%>%
    column_spec(4, width = "2em")%>%
    column_spec(5, width = "4em")%>%
    column_spec(6, width = "4em")%>%
    column_spec(7, width = "4em")%>%
  column_spec(8, width = "4em")%>%
  column_spec(9, width = "4em")%>%
  column_spec(10, width = "4em")%>%
     column_spec(11, width = "9em")%>%
    column_spec(12, width = "9em")%>%
  kable_styling(latex_options = c("repeat_header"), font_size = 10, repeat_header_method = "replace")%>%
  footnote(general = "description", general_title = "")%>%
  landscape()

This is the output: enter image description here

Is it possible to force words to split across lines to fit cell width?

Upvotes: 2

Views: 701

Answers (1)

You can manually tell latex about possible hyphenation points by using "Tri\\-glycer\\-ides"

---
title: "Queries"
header-includes:
- \usepackage{pdflscape}
- \usepackage{longtable}
output: 
  pdf_document:
    toc: false
---

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

```{r genpop}
genpop = structure(list(`First author, Year` = c("Hsu  2009", "Hallan  2009", 
"Bash  2010", "Ryu  2009"), Population = c("Medical insurance check up", 
"General population", "Age 45-64", "Men, age 30-59"), `Mean age` = c(40, 
50, 54, 37), `Participants (n)` = c(177570, 65589, 15324, 10685
), `Follow up (years)` = c(25, 10, 16, 3.8), Outcome = c("ESRD", 
"ESRD/Death", "ESRD", "Incident CKD (eGFR<60)"), `1st` = c("BMI (category)", 
"Diabetes (present)", "Diabetes (present)", "Low HDL"), `2nd` = c("Hypertension (stage)", 
"Low physical activity", "Tri\\-glycer\\-ides", "Tri\\-glycer\\-ides"), `3rd` = c("Diabetes (Y/N)", 
"SBP", "SBP", " "), `4th` = c("Uric acid (highest quartile)", 
"Low HDL", "BMI", " "), `Other significant` = c("Dipstick proteinuria\\*, race, age, education, low Hb", 
"eGFR\\* \\& ACR\\* (in full model), antihypertensive treatment, male sex, age", 
"Race, smoking, age, CHD, male sex", "HOMA-IR"), `Non-significant` = c("Lipids", 
" ", " ", "Obesity, hypertension, raised fasting glucose")), row.names = c(NA, 
-4L), class = "data.frame")

genpop %>%
    kbl( longtable = TRUE, escape = F, booktabs = T, caption = 'Some text') %>% 
    
    column_spec(1, width = "6em")%>%
    column_spec(2, width = "6em")%>% 
    column_spec(3, width = "3em")%>%
    column_spec(4, width = "2em")%>%
    column_spec(5, width = "4em")%>%
    column_spec(6, width = "4em")%>%
    column_spec(7, width = "4em")%>%
  column_spec(8, width = "4em")%>%
  column_spec(9, width = "4em")%>%
  column_spec(10, width = "4em")%>%
     column_spec(11, width = "9em")%>%
    column_spec(12, width = "9em")%>%
  kable_styling(latex_options = c("repeat_header"), font_size = 10, repeat_header_method = "replace")%>%
  footnote(general = "description", general_title = "")%>%
  landscape()

enter image description here

Upvotes: 2

Related Questions