-knit(.rnw.file, tangle = 'tangle' %in% .cmdargs)
-
-setwd(.cmdargs[4])
-## remove the copied .Rnw if it did not exist, otherwise move the original one back
-if (.rnw.exists) file.rename(.tmp.file, .rnw.file) else unlink(.rnw.file)
-file.rename(basename(.cmdargs[2]), .cmdargs[2]) # move .tex to the temp dir
-rm(.tmp.file, .rnw.file, .rnw.exists) # clean up these variables
-
+if (is.null(formals(knit)$encoding)) {
+ .orig.enc = getOption("encoding")
+ options(encoding = .cmdargs[3])
+ knit(.cmdargs[1], output = .cmdargs[2], tangle = 'tangle' %in% .cmdargs)
+} else {
+ knit(.cmdargs[1], output = .cmdargs[2], encoding = .cmdargs[3],
+ tangle = 'tangle' %in% .cmdargs)
+}