Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using xtable with R and Latex, math mode in column names?

Tags:

I'm using xtable to compile tables from R automatically while compiling my TeX document. The question I have is how I get the variable names in the table (which in my case are the column names in a dataframe) to be in math mode. I have stored my results in the dataframe adf.results, and essentially what I want is

colnames(adf.results) <- c(" ", "$m^r_t$", "$\delta p_t$",
                           "$R^r_t$", "$R^b_t$", "$y^r_t$")

but that simply inserts $m^r_t$ ... as the column names without interpreting them as being in math mode. Does anyone have a solution?

like image 457
hejseb Avatar asked Feb 14 '13 14:02

hejseb


2 Answers

as suggested in the xtable gallery vignette you should use a sanitization function (as unikum also suggested). Just some dummy code how I got it working with your example:

library(xtable)
adf.results<-matrix(0,ncol=6,nrow=4)
colnames(adf.results) <- c(" ", "$m^r_t$", "$\\delta p_t$","$R^r_t$", "$R^b_t$", "$y^r_t$")
print(xtable(adf.results),sanitize.text.function=function(x){x})

Good luck with it.

Kind regards,

FM

like image 114
FM Kerckhof Avatar answered Oct 12 '22 00:10

FM Kerckhof


Please see xtable gallery: Sanitization section (page 7 and below).

like image 33
Artem Klevtsov Avatar answered Oct 11 '22 23:10

Artem Klevtsov