How to Set the Latex Path for Sweave in R

How can I set the latex path for sweave in R?

If you have multiple installs of LaTeX (i.e. MikTeX) and you want to use a specific one of these, then you need to make sure that R finds the one you need first. This means that you have to add the location of your preferred version of pdfLaTeX at the front of your PATH system environment variable.

If you do not have administrator rights in Windows, then you can use R's environment file to change the PATH variable for R only. See ?Startup in R for details on this process. Follow the following steps:

  1. in R, check the output of Sys.getenv("R_ENVIRON"). This will return the full path to an existing environment file, but will be empty in most cases. If a file exists, skip to step 3 below.
  2. if no path is returned in step 1, create a file Renviron.site in the folder R_HOME/etc where R_HOME is the path returned by Sys.getenv("R_HOME").
  3. add a line to the environment file as follows: PATH=C:\\full\\path\\to\\the\\folder\\with\\pdflatex;"${PATH}" (the quotations marks are important)
  4. restart R and check Sys.getenv("PATH") and Sys.which("pdflatex") returns the correct paths.

If you do not have rights to create a Renviron.site file in R_HOME\etc, then you can also create a .Renviron file in HOME (Sys.getenv("HOME")).

Sweave not working in Rstudio - found path, but how to set?

I had the same problem and I got a solutions that worked for me. Open your terminal and type the following command:

sudo ln -s /usr/texbin/pdflatex /usr/bin/pdflatex

Edit Windows environmental variable

C:\\PROGRA~1\\MIKTEX~1.9 doesn't mean literally MiKTeX v1.9. It is an 8.3 filename. Because the string MiKTeX 2 contains a "special character" (i.e. a space), it is converted to MIKTEX~1 (the .9 part still remains as the "extension", so MiKTeX 2.9 became MIKTEX~1.9, which is indeed confusing in this case).

I feel the problem you are actually trying to solve might be a different one. If that's the case, you may ask the actual question. There isn't anything wrong with your environment variables, as far as I can see.

If you really need the long name, you can call normalizePath() to convert the short 8.3 name to a long name.

Using graphicspath in latex/sweave

I expand my comment with three different ways of achieve your goal; Suppose you want to include a file image named IMG_7254.JPG (note that I do not write the file extension in the following code because I use the \DeclareGraphicsExtensions{.JPG}), You can do it in three ways:

  1. to put the file to compile (the .tex one) and the image to include in the same folder. LaTeX searches (by default) for pictures to include in documents in the same directory where the file to be compiled is. In this case the code is just:

    \includegraphics[width=0.7\textwidth]{IMG_7254}
  2. Have pictures and file to compile in very different folders, in this case you must specify the complete path of the file (it can be found in different ways), example with Mac OS read here. In this case the code is:

     \includegraphics[width=0.7\textwidth]{/Users/SabDeM/Pictures/IMG_7254}
  3. to put the file to compile in a folder (say File_to_com), then put all pictures to include in a sub folder, say, File_to_com/picturesToInc, and then specify the path. In this latter case the code is:

    \includegraphics[width=0.7\textwidth]{picturesToInc/IMG_7254}

As you can see in all ways (one way or another) LaTeX (which render the PDF) must to know where the pictures are located in your computer.

R Sweave: NO TeX installation detected

Usually Rstudio finds the correct path to your Latex installation automatically. In your case that doesn't seem to be the case. After making sure Miktex is installed correctly (have you opened it and compiled a simple file?) you can set the pdflatex path manually:

Sys.setenv(PATH = paste(Sys.getenv("PATH"), "C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64", sep=.Platform$path.sep))

This is the path to my pdflatex.exe file, yours might live somewhere else.

You will have to run this every time you restart RStudio. Adding the path to pdflatex.exe to your PATH variable in Windows should make this permanent but I haven't tested that. Go to System Properties, click on Environment Variables, then on Path and then Edit. Then just enter the path to your pdflatex.exe:

Sample Image



Related Topics



Leave a reply



Submit