Tuesday, November 19, 2013

Setting R Path in Mac OS for Latex

There's a nice latex() function from R package 'Hmisc' to generate LaTex tables automatically,  but the function might not work for Mac OS X platform due to the path environmental variables in R isn’t set to include the path to TeX binaries.

Here, we can see that the R path does not include the path to TeX binaries:
> Sys.getenv("PATH")
[1] "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"
 

So, if you try to call the latex() function, it will give an error message like that:

library(Hmisc) # Load Hmisc to load latex()
x <- matrix(1:6, nrow=2, dimnames=list(c('a','b'),c('c','d','this that'))) # From latex() examples
latex(x)
error /bin/sh: latex: command not found
error in system(cmd, intern = TRUE, wait = TRUE) :
error in running command
sh: xdvi: command not found
To add the Tex PATH into the R envirnment, following my last blog to create .Rprofile and add the following command to the file:
Sys.setenv(PATH=paste(Sys.getenv("PATH"),"/usr/texbin",sep=":"))
After reboot R, you will find the tex path was added.
> Sys.getenv("PATH")
[1] "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin"
 

No comments:

Post a Comment