15

I downloaded the most recent version of Miktex Basic Installer (2.9.5105). After following the Miktex's instructions, I began working through Nicola Satori's Sweave Tutorial.

The R portion of example 1 worked fine, however, when I compiled the .tex file, I received the error below.

!Latex Error: 'Sweave.sty' not found

After spending hours pouring over numerous forums, I found the reason why I received this error and the likely reason that you're receiving this error as well.

My answer below is the initial answer that I was looking for. Feel free to add comments and explanations, however, keep in mind that the instructions are simply to fix this error and allow Miktex to function properly when calling the Sweave package.

1
  • If you're just getting started with sweave you might want to consider knitr instead. It's a slightly more modern tool for embedding R in TeX source. Jan 9, 2014 at 15:48

4 Answers 4

25

The likely reason that this error is triggered is due to the texmf folder not being properly mapped to Miktex's root directory. Since Sweave.sty is positioned in a sub directory of texmf, Miktex never finds it.

Sweave.sty comes standard with current R downloads.

To properly map the texmf directory to Miktex's root directory, follow these instructions carefully.

  1. Locate the texmf directory inside the share folder in R. For me, the path was C:\Program Files\R\R-3.0.2\share\texmf but this can change from user to user
  2. Launch Miktex's Options via the start menu or by locating this exectuable C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\mo_admin.exe. You can also follow these instructions
  3. Click on the "Roots" tab and click "Add"
  4. Map the folder path to texmf that you located earlier.
  5. If you receive an error about the file being in use, make sure to close out of any open session of TeXworks.
  6. Click "Ok" and you're set to go.

Like I said above, this is the straight forward answer to get you out of the forums and exploring Miktek and Sweave.

Enjoy!

3
  • 1
    See also Create a local texmf tree in MiKTeX.
    – Speravir
    Jan 10, 2014 at 1:49
  • Hint: when adding the path, stop at texmf, don't include the tex/latex part! Apr 25, 2016 at 15:27
  • I used the MikTex console: settings > Directories then the small + button to add the new root folder as described. Many thanks!
    – peter2108
    Oct 6, 2020 at 17:17
1

I have Mac (OSx) and the accepted solution it didn't work to me.

This was the solution in my case, to install mactex: https://tug.org/mactex/mactex-download.html

1
  • 1
    MacTeX does not seem to have Sweave (I've tried 2018 version). I followed joey711.wordpress.com/2011/07/28/… to copy the files from R to /usr/local/texlive/texmf-local/tex/latex/Sweave and then texhash (sudo texhash /usr/local/texlive/texmf-local).
    – chan1142
    Aug 12, 2018 at 0:43
1

I eventually ended up with the following solution:

install.packages("tinytex")   
require("tinytex")}
install_tinytex(force = TRUE)
tlmgr_install('montserrat') 
xelatex('Report.tex')

This code install TinyTex, then you can compile with pdflatex(), xelatex() or lualatex().

1

For Tex Live users, the problem is similar: Sweave.sty is not typically on the search path used by kpsewhich and so the same error is produced: !Latex Error: 'Sweave.sty' not found.

According to the "LaTeX/Installing Extra Packages" Wiki, the best place for this extra material is your home directory i.e. ~/texmf. This is to prevent it from being lost if you update your TeX distribution e.g. 2019 to 2020.

The following bash script should copy the relevant directories from a typical R installation to this folder, then update the Tex search tree:

cp --recursive $(locate Sweave.sty | grep --only-matching .*texmf/) ~/
texhash

There is no need for sudo texhash here as we are updating the local part of our search tree. We can confirm that this has worked by checking the output from kpsewhich. (I have abbreviated my home folder to ~ below).

~$ kpsewhich Sweave.sty
~/texmf/tex/latex/Sweave.sty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy