How to use Greek letters in kable rownames

I have a table that I am trying to format for pdf , using kable(),with Greek letters in the rownames. I have tried creating a list of strings outside of kable and then changing the row names using rownames(XXX) <- MyListof Strings, and using "$\Delta$", "$\\Delta$", and several other methods to include a Greek \Delta, but nothing that I have tried has worked. Is there a way that I can do this, and if so, how?
Larry Hunsicker

Hi, this is a job for xtable. Try knitting an Rmd file with the following example from nebi


output: pdf_document

library(knitr)
library(xtable)

url <- 'https://cdn.rawgit.com/fsbmat/StackOverflow/master/'
dt1 <- read.table(paste0(url, 'sim50.txt'), header = TRUE)
attach(dt1)
dt2 <- read.table(paste0(url, 'sim200.txt'), header = TRUE)
attach(dt2)
dt3 <- read.table(paste0(url, 'sim1000.txt'), header = TRUE)
attach(dt3)

gamma0=1.23
gamma1=0.07
gamma2=1.05
#gamma3=0.7
beta0=1.18
beta1=0.23
beta2=0.25
phi1 <- 0.69
rho <- 0.03
truevalue <- c(gamma0,gamma1,gamma2,beta0,beta1,beta2,phi1,rho)

eqm=function(x,theta){ 
  N=length(x)
  sqrt(sum(((x-theta)^2))/N)}
m  <- dt1
m2 <- dt2
m3 <- dt3

eqmest=c(eqm(x=m[,1],theta=truevalue[1]),
         eqm(x=m[,2],theta=truevalue[2]),
         eqm(x=m[,3],theta=truevalue[3]),
         eqm(x=m[,4],theta=truevalue[4]),
         eqm(x=m[,5],theta=truevalue[5]),
         eqm(x=m[,6],theta=truevalue[6]),
         eqm(x=m[,7],theta=truevalue[7]),
         eqm(x=m[,8],theta=truevalue[8]))

eqmest2=c(eqm(x=m2[,1],theta=truevalue[1]),
          eqm(x=m2[,2],theta=truevalue[2]),
          eqm(x=m2[,3],theta=truevalue[3]),
          eqm(x=m2[,4],theta=truevalue[4]),
          eqm(x=m2[,5],theta=truevalue[5]),
          eqm(x=m2[,6],theta=truevalue[6]),
          eqm(x=m2[,7],theta=truevalue[7]),
          eqm(x=m2[,8],theta=truevalue[8]))

eqmest3=c(eqm(x=m3[,1],theta=truevalue[1]),
          eqm(x=m3[,2],theta=truevalue[2]),
          eqm(x=m3[,3],theta=truevalue[3]),
          eqm(x=m3[,4],theta=truevalue[4]),
          eqm(x=m3[,5],theta=truevalue[5]),
          eqm(x=m3[,6],theta=truevalue[6]),
          eqm(x=m3[,7],theta=truevalue[7]),
          eqm(x=m3[,8],theta=truevalue[8]))
parameter <- c("$\\gamma 0 (\\mu 2)$","$\\gamma 1 (\\mu 2)$", "$\\gamma 2 (\\mu 2)$", "$\\beta 0 (\\mu 1)$", "$\\beta 1 (\\mu 1)$","$\\beta 2 (\\mu 1)$","$\\phi 1 (sd)$","$\\rho (cor)$")
dt <- data.frame(Parameters=parameter,VV=truevalue,Mean=cbind(colMeans(dt1)),EQM=eqmest,Mean=cbind(colMeans(dt2)),EQM=eqmest2,Mean=cbind(colMeans(dt3)),EQM=eqmest3)

print(xtable(dt,digits=c(5,5,5,5,5,5,5,5,5), caption = "",align = rep("c",9)), 
      caption.placement = "top", include.rownames = FALSE, type = "latex", 
      sanitize.text.function = function(x) {x})

Can you share a reproducible example of what you tried ?

I tried this and it works

---
title: "test"
output: pdf_document
---

```{r}
df <- data.frame(k=1:3, delta = (1:3)/2)
knitr::kable(df, col.names = c("$k$", "$\\Delta$"))
```

image

2 Likes

Yes. This works for column names. But the kable parameter col.names takes only a T/F/NULL response. It seems that one can rename the rows outside of kable and then print the table.


title: "Test"
author: "Hunsicker LG"
date: "1/5/2020"
output: pdf_document

df <- data.frame(k=1:3, delta = (1:3)/2)
row.names(df) <- c('$\\lambda$', "$\\mu$", "$\\pi$")
knitr::kable(df)

test.pdf (71.1 KB)

1 Like

There might be a way to do it in kabel. I suggested xtable because I know it can be

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.