-# pass the option stylepath=TRUE to sweave so that a full path is given
-# to \usepackage.
-ls.sweavesty <- system("kpsewhich Sweave.sty", intern=TRUE, ignore.stderr=TRUE)
-ls.sp <- (length(ls.sweavesty) == 0)
+# copy it alongside the .tex file (in general in the temporary
+# directory). This means that an exported LaTeX file will not work,
+# but this is a problem of installation of R on the user's machine.
+# The advantage compared to the use of stylepath, is that the exported
+# .tex file will be portable to another machine. (JMarc)
+if (!length(system("kpsewhich Sweave.sty", intern=TRUE, ignore.stderr=TRUE))) {
+ .texmf.path <- file.path(R.home("share"), "texmf")
+ if (!file.exists(.sweave.sty <- file.path(.texmf.path, "Sweave.sty"))) {
+ .sweave.sty <- file.path(.texmf.path, "tex", "latex", "Sweave.sty")
+ }
+ file.copy(.sweave.sty, dirname(.cmdargs[2]), overwrite=TRUE)
+ rm(list = c('.sweave.sty', '.texmf.path'))
+}