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.

  • 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. – Ethan Bolker Jan 09 '14 at 15:48

5 Answers5

29

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!

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

  • 2
    MacTeX does not seem to have Sweave (I've tried 2018 version). I followed https://joey711.wordpress.com/2011/07/28/adding-sweave-sty-and-rd-sty-to-your-latex-path-in-mac-os-x/ 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 '18 at 00: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. – Eric Anderson Aug 18 '22 at 04: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 '22 at 06: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. – Eric Anderson Aug 19 '22 at 16:24
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
dardisco
  • 423
0

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().

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
zabala
  • 61