Building R packages: missing path to pdflatex

[This article was first published on Daniel J. Hocking » Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Recently whiling trying to build an R package for generalized estimating equation model selection (QICpack on github), I was getting an error related to latex creating the PDF package manuals. It seems like this is a relatively common problem on some versions of Mac OS X, but I did not find it easy to find an answer so I thought I’d describe my ultimate solution.

The specific error I was getting was:

error in texi2dvi(file = file, pdf = true, clean = clean, quiet = quiet, : 
pdflatex is not available

I was getting this error regardless of whether building the package using R CMD packagename in the Terminal or using build in R or Rstudio via the devtools package.

I tried reinstalling MacTex and restarting my computer to no avail. It turned out that it was a problem with not having a path to the Tex installation. You can check using the following code in the Terminal or in R:

Sys.which("pdflatex")
Sys.getenv("PATH")

In my case, I got something like:

Sys.which(“pdflatex”)

 ""

Sys.getenv(“PATH”)

"/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"

I don’t know much about setting paths, profiles, and environments, but this indicated that there is no path to latex and so the build function can’t find pdflatex needed to convert the .Rd files (LaTeX code of manuals in R packages) to PDF files for the R package. I was able to fix the problem by adding a path to the latex binaries using:

Sys.setenv(PATH=paste(Sys.getenv("PATH"),"/usr/texbin",sep=":"))

Now when I run Sys.getenv("PATH") I get

"/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin"

Now R is able to find the LaTeX binaries and when I run Sys.which("pdflatex") I get

              pdflatex 
"/usr/texbin/pdflatex"

That should fix the problem and now the build function can turn the .Rd files into PDF documents of the R functions.


Filed under: R Tagged: Error, LaTeX, mactex, packages, pdflatex, R

To leave a comment for the author, please follow the link and comment on their blog: Daniel J. Hocking » Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)