Search code examples
r-markdownrowexpressionsymbols

Adding greek letters as row entries in a results table in R


I am trying to create a results table in R (or Rmarkdown) and trying to add Greek letters for various parameters as row entries and can't seem to get it to work. Here is an example

library(knitr)
library(xtable)
library(tidyverse)

df <- tibble(Truth = c(-0.75, 0.5), Post_Mean = c(-0.74, 0.52))
Parameter <- c("$\\gamma$", "$\\beta$")
dt <- tibble(Parameter = Parameter, df)
dt
# print(xtable(dt, type = "latex"), sanitize.text.function = function(x) {x})

I tried a few suggestions I found online but none seemed to work. Any ideas on how I can work around this?


Solution

  • For RMarkdown solution, you can use kableExtra package.

    library(knitr)
    library(xtable)
    library(tidyverse)
    library(kableExtra)
    df <- tibble(Truth = c(-0.75, 0.5), Post_Mean = c(-0.74, 0.52))
    Parameter <- c("$\\gamma$", "$\\beta$")
    dt <- tibble(Parameter = Parameter, df)
    kable(dt, escape = F, align = "c", caption = "Posterior means and true values of parameters")
    

    Key parameter is escape = F which enables the recognition of LaTeX symbols.

    This chunk produces a table like this in HTML output:

    enter image description here