/bin/sh: latex: command not found: setting R path to include TeX binaries pathPosted: January 29, 2011
In integrating statistical analysis in R with writing up documents in LaTeX, some useful functions for generating LaTeX tables are
latex() from the Hmisc package and
xtable() from the xtable package.
You also need R to be able to access TeX binaries! Sometimes, R might not be able to do this, if your path environmental variable in R isn’t set to include the path to TeX binaries. This may be a problem especially if R doesn’t inherit the path from the shell environment, e.g. if you load R from Finder in Mac OS X.
Here, we can see that the R path does not include the path to TeX binaries:
Sys.getenv("PATH") # PATH #"/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"
So if you try to call an R function that calls TeX binaries, it will give an error message:
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) #/bin/sh: latex: command not found #Error in system(cmd, intern = TRUE, wait = TRUE) : # error in running command #sh: xdvi: command not found
To explicitly set the R path, you can create a text file called
.Rprofile in your user directory (for Mac OS X, this is in the
~/ directory). In this text file, you can add to the path so that R can access LaTeX commands (following this post):
Sys.setenv(PATH=paste(Sys.getenv("PATH"),"/usr/texbin",sep=":")) # this adds /usr/texbin to the R path
This should work if you have some TeX Live distribution on a UNIX system.
.Rprofile you can also explicitly set the entire path, not just adding on
/usr/texbin to the existing path, and define any functions you would like to always have in your R environment, etc.)
Sys.getenv("PATH") # PATH #"/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin" library(Hmisc) x <- matrix(1:6, nrow=2, dimnames=list(c('a','b'),c('c','d','this that'))) latex(x) #This is pdfTeX, Version 3.1415926-1.40.11 (TeX Live 2010) #restricted \write18 enabled. #entering extended mode
Now R can access the TeX binaries!