29

On Windows, how could I instruct R to call TeXlive instead of MikTeX?

I've got R set up on my Linux and Windows machines. On my Windows machines, I happen to have both MikTeX and TeXlive available. For reasons I won't go into I'd like R to call TeXlive. At this time, R is picking up MikTeX instead.

I'm guessing I would need to set TEXINPUTS inside my Renviron file, or something similar. But I haven't been able to find precise instructions online. Help will be appreciated.

UPDATE: Here are different things I tried: changing the order of TeXlive and MikTeX in the PATH. Removing MikTeX from the path. Neither worked, MikTeX is still being picked up.

I couldn't find the relevant documentation for R, but I did find some hints in the RStudio documentation, so I attempted to solve the problem within RStudio. I successfully defined the RSTUDIO_PDFLATEX environment variable:

Sys.getenv('RSTUDIO_PDFLATEX')
[1] "C:/texlive/2012/bin/win32"

[Reference: http://www.rstudio.com/ide/docs/authoring/latex_program?version=0.97.312&mode=desktop]

but MikTeX is still called upon.

Sys.which("pdflatex")
                                         pdflatex 
"C:\\PROGRA~2\\MIKTEX~1.9\\miktex\\bin\\pdflatex.exe" 

UPDATE 2: Another thing I've tried: While texlive is already on my PATH, just in case I added it from within R.

Sys.setenv("PATH" = paste(Sys.getenv("PATH"),"C:/texlive/2012/bin/win32",sep=":"))

I also tried to set the path to pdflatex as returned by Sys.which("pdflatex") with the following:

Sys.setenv(pdflatex="C:/texlive/2012/bin/win32")

as well as variants with PDFLATEX or "PDFLATEX", but that doesn't help. I have also removed everything from the path except the path to texlive:

Sys.setenv("PATH" = "C:/texlive/2012/bin/win32")

That gives me the desired path

Sys.which("pdflatex")
                                 pdflatex 
"C:\\texlive\\2012\\bin\\win32\\pdflatex.exe" 

However, running texi2dvi fails:

tools::texi2pdf(Out)
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet,  : 
pdflatex is not available
user227710
  • 3,164
  • 18
  • 35
PatrickT
  • 10,037
  • 9
  • 76
  • 111
  • Try the various things you tried above with just plain R (no RStudio) so that you can at least determine whether its an R problem or an R Studio problem. Also make your example reproducible. We have no idea what you did to get that warning message. – G. Grothendieck Feb 25 '13 at 15:51
  • Thanks! I'm running knitr within R (not RStudio, except for testing the environment variable RSTUDIO_PDFLATEX, as described). The error messages display a path to the miktex executables. However, if run with texlive there are no errors. I know this because I have 2 linux setups and 1 windows setup that call texlive (I specifically removed miktex from that windows machine to test), and it works there. I have defined an environment variable named R_PDFLATEX, which is picked up by R, but it's not helping R find texlive. Any suggestions? Thanks. – PatrickT Feb 25 '13 at 19:40
  • You could also try `R_PDFLATEXCMD` and `R_LATEXCMD` to see if those have any effect. – G. Grothendieck Feb 25 '13 at 19:48
  • I put this in my Rprofile (and also executed it in the console): Sys.setenv(pdflatex="C:/texlive/2012/bin/win32"). Notwithstanding Sys.which("pdflatex") returns the path to MikTeX... – PatrickT Feb 26 '13 at 21:01
  • The environment variables that end in `CMD` must have the `pdflatex.exe` or `latex.exe` command as part of them. – G. Grothendieck Feb 26 '13 at 23:39
  • Google for "R_PDFLATEXCMD" to find examples. – G. Grothendieck Feb 28 '13 at 13:23
  • It's less than ideal but you could just uninstall MikTeX... – cameron.bracken Mar 13 '13 at 15:59
  • have you tried this using the "installr" package? If there is a texlive package that it can install from then it may be able to include whichever internal settings you are missing. You might explore the installr content for miktex - perhaps the switch you are looking for is in there. – EngrStudent Oct 16 '15 at 02:39

2 Answers2

2

I was confronted with the same issue in a similar project and spent some time understanding the tricks. The fact that MikTeX comes out selected even if you cleaned up the path results from the somewhat exasperating fact (especially for *nix-oriented devs like me and others) that on installing MikTeX, a fairly big number of Windows registry entries is set. The portable MikTeX (win32 only) distro is the sole exception to this. So if you do need MikTeX on board, my advice is just to do what I did:

  1. uninstall your current MikTeX distro
  2. cleanup the registry (ccleaner worked out well)
  3. install the portable win32 MikTeX distro

Now check that your path does include your TeXlive bin directory path again.
If you use pandoc for creating pdf files from Rmarkdown or other markdown languages, a convenient way to work around path issues is to specify the --latex-engine option and add the full filepath as an argument. Below is a possible command line, adapted from the RStudio IDE:
path/to/pandoc.exe -V papersize=A4 +RTS -K512m -RTS file.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output file.pdf --template path/to/default.tex --highlight-style tango --latex-engine /path/to/pdflatex.exe --variable geometry:margin=1in

0

I'm pretty sure you need to add paths here in your global environment variables. See: http://statmath.wu.ac.at/software/R/qfin/ and http://www.howtogeek.com/51807/how-to-create-and-use-global-system-environment-variables/

ZagNut
  • 1,431
  • 15
  • 20