18

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

5 Answers 5

28

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

4
  • 2
    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
  • @chan1142, I'm not sure how/why this approach worked for Pablo, but yeah: sweave isn't a standalone package on CTAN, and TexLive (MacTex) doesn't include it. The style file is only (AFAIK) distributed as part of the R sweave package. For me in summer 2022, the file lives in /Library/Frameworks/R.framework/Resources/share/texmf/tex/latex/. Annoyingly, the MacOS implementation of locate apparently doesn't index that location. Aug 18, 2022 at 4:06
  • 1
    @EricAnderson, Hmm my locate gives it. But my system is very old (2016) so it could have been indexed long time ago. Maybe I had indexed my HDD manually and then disabled it.
    – chan1142
    Aug 18, 2022 at 6:54
  • 1
    @chan1142 Interesting. I looked into this a little further, and my locate database file hasn't been modified in months, so I think it's "the database isn't being maintained/updated at all" not "that path isn't included." I don't know why it stopped, but that seems to be a separate issue altogether. Aug 19, 2022 at 16:24
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
0

On Mac, according this suggestion.

  1. Bring up the a folder with the .sty files that you need to copy (from within the R framework):
cd /Library/Frameworks/R.framework/Resources/share/texmf/tex
open -a Finder latex
  1. Once you have that folder in front of you, grab the “Rd.sty” and “Sweave.sty” files, copy them with command-c, then open the LaTeX folder that you need for the paste. Do your command-v to paste. Be aware of the version/year folder (2011basic, 2022, 2023, etc.).
cd /usr/local/texlive/2023/texmf-dist/tex/latex/
open -a Finder base
  1. When done adding those .sty files to the tex/latex/base/ directory, make sure to run the texhash command in order to update tex so that its aware of the new .sty files that you just added.
sudo texhash

You must log in to answer this question.