如何在 kable pdf 输出中跨行拆分整个单词以防止文本与下一个单元格重叠

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

我正在使用 rmarkdown 制作 pdf。我的文档包含一个包含大量文本的 longtable。通过使用 column_spec 设置列宽,我可以让文本换行,但在某些单元格中,我需要拆分整个单词,否则它们会流入下一个单元格并重叠。

---
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()

这是输出:

是否可以强制单词跨行拆分以适合单元格宽度?

您可以使用 "Tri\-glycer\-ides"

手动告知 latex 可能的断字点
---
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()