Search code examples
rkablekableextra

Remove italics from kable table


I am trying to add subscript and superscripts to a kable table. I was able to figure out how to add the superscript and subscripts but for some reason, it italicizes the letters. I cant figure out how to remove the italics. Any help would be great.

I feel like I am so close to figuring this out, I've just spent too much time without making any progress

Here is an example of the data

df <- structure(list(SNP = structure(1:21, .Label = c("a", "b", "c", 
"d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", 
"q", "r", "s", "t", "u"), class = "factor"), Gene = structure(1:21, .Label = c("a", 
"b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", 
"o", "p", "q", "r", "s", "t", "u"), class = "factor"), `Cases (338)` = c(87L, 
86L, 72L, 71L, 70L, 69L, 67L, 58L, 55L, 54L, 52L, 50L, 48L, 47L, 
47L, 46L, 45L, 45L, 43L, 43L, 42L), `% cases` = c(25.7, 25.4, 
21.3, 21, 20.7, 20.4, 19.8, 17.2, 16.3, 16, 15.4, 14.8, 14.2, 
13.9, 13.9, 13.6, 13.3, 13.3, 12.7, 12.7, 12.4), `p-value` = c(5.2, 
6.3, 8.5, 1, 1.2, 1.4, 2, 1.1, 1.7, 2, 2.9, 4, 5.6, 6.6, 6.6, 
7.8, 9.2, 9.2, 1.3, 1.3, 1.5), sub = structure(c(1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("$Example^{test}_{sub}$", "$Example^{test}$"
), class = "factor")), class = "data.frame", row.names = c(NA, 
-21L))

Here is an example of the table I am making

kbl(table, format = "html", table.attr = "style='width:40%;'") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
  kable_classic(full_width = F, html_font = "Times New Roman")

Solution

  • If you use some LaTeX code, you can achieve your goal.

    .Label becomes:

    .Label = c("$\\mathrm{Example^{test}_{sub}}$", "$\\mathrm{Example^{test}}$"
    

    Then, you add the option escape = F to kbl (by the way, you need to use df, not table):

    kbl(df, format = "html", table.attr = "style='width:40%;'", escape = F) %>%
      kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
      kable_classic(full_width = F, html_font = "Times New Roman")
    

    The final result is:

    enter image description here