LaTeX Symbols Inside Pandoc Table Column Headings Inside RMarkdown Documents

I just spent ~ 30 minutes figuring out how to include LaTeX symbols … in a Pandoc table column header … in an RMarkdown document.

There are plenty of LaTeX cheat sheets like this one to choose from. And there are plenty of examples of using LaTeX in RMarkdown as well.

The thing is that the syntax for these LaTeX symbols gets trickier when run inside a chunk (i.e. block of R code). The code below demonstrates one way to get the pander table to output the column names with LaTeX syntax.

# set random seed
set.seed(123)

# set K for number of replications
K <- 1000

# rate (lambda)
lambda <- 3

# n 
n <- c(5, 10, 15, 20, 25)

# simulate data
sims <- lapply(n, function(x) replicate(K, rexp(x, lambda)))

# calculate sample means
sims_mean <- lapply(sims, function(x) apply(x, 2, mean))

# calculate sample variances
sims_var <- lapply(sims, function(x) apply(x, 2, var))


library(pander)
xbars <- sapply(sims_mean, mean)
vars <- sapply(sims_mean, var)
expected_s2 <- sapply(sims_var, mean)

propdata <- data.frame(n = n,
                       xbars = xbars, 
                       vars  = vars,
                       expected_s2 = expected_s2)

# assign names of dataframe with latex syntax 
names(propdata) <- c("n", "$\\mu_{\\bar{x}}$", "$\\sigma^2_{\\bar{x}}$", "$E(s^2)$")

# names(propdata) <- c("n", "$mu_", "$\\bar", "$\\sigma")
pander(propdata)

The key line is:

names(propdata) <- c("n", "$\\mu_{\\bar{x}}$", "$\\sigma^2_{\\bar{x}}$", "$E(s^2)$")

That piece of code assigns names to the propdata object the desired syntax … err … sort of the desired syntax. You have to escape the \ character that preceeds each of the references to mu, bar and sigma.

It’s not pretty but it works.

Maybe there’s a better way to this with pandoc.table()?

Related