如何在 KableExtra 的单元格中强制换行和加粗字符串

How to force linebreak and bold string in cell within KableExtra

我使用 kableExtra 包在 RMarkdown 中生成了以下 table。我试图将单元格中选定的单词加粗(下面示例中的单词 First)并在单元格中的两个单词(First 和 Message)之间强制换行,但这似乎不起作用。关于如何做到这一点有什么想法吗?

library(kableExtra)
library(tidyverse)

first <- c('\textbf{First} Message','\textbf{First}\n Message','First Message')
second <- c('Second Message','Second Message','Second Message')
third <- c('Third Message','Third Message','Third Message')

data.frame(first,second,third) %>% 
  kable(format='latex',caption="Caption",
        col.names = c('First',"Second","Third"), booktabs = T, escape = FALSE) %>% 
  kable_styling(latex_options = c("HOLD_position"), font_size = 7) %>% 
  row_spec(0,bold=T,color = 'white', background = '#7c3042') 



您需要将 mutate_all(linebreak) %>% 添加到您的代码中。

在此处查看文档(第 26 页):https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

修改您的代码:

library(kableExtra)
library(tidyverse)

first <- c('\textbf{First} Message','\textbf{First}\n Message','First Message')
second <- c('Second Message','Second Message','Second Message')
third <- c('Third Message','Third Message','Third Message')

data.frame(first,second,third) %>%
  mutate_all(linebreak) %>%
  kable(format='latex',caption="Caption",
        col.names = c('First',"Second","Third"), booktabs = T, escape = FALSE) %>% 
  kable_styling(latex_options = c("HOLD_position"), font_size = 7) %>% 
  row_spec(0,bold=T,color = 'white', background = '#7c3042') 

结果: