sa90210
sa90210

Reputation: 585

How to force linebreak and bold string in cell within KableExtra

I have the following table generated in RMarkdown using the kableExtra package. I'm trying to bold selected words in a cell (the word First in my example below) and force a linebreak between two words in a cell (First and Message), however this doesn't seem to work. Any ideas on how to do this?

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



Upvotes: 1

Views: 953

Answers (1)

bttomio
bttomio

Reputation: 2306

You need to add mutate_all(linebreak) %>% to your code.

Check the documentation here (page 26): https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

Modifying your code:

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

Result:

enter image description here

Upvotes: 1

Related Questions