X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Fscripts%2Flyxsweave.R;h=7573d315c1fd5d7b668edd23236757355ef6ae7a;hb=08b285966326523a10954cf6235d0c7b200c842f;hp=32979c22af5ef4aa5f893d11b018575131405f53;hpb=dc217ed08cf3da0f599010b9bacb4a95b46e3614;p=lyx.git diff --git a/lib/scripts/lyxsweave.R b/lib/scripts/lyxsweave.R index 32979c22af..7573d315c1 100644 --- a/lib/scripts/lyxsweave.R +++ b/lib/scripts/lyxsweave.R @@ -3,6 +3,7 @@ # Licence details can be found in the file COPYING. # author Jean-Marc Lasgouttes +# author Yihui Xie # Full author contact details are available in file CREDITS @@ -12,30 +13,64 @@ # argument 3 is the iconv name for the encoding of the file # argument 4 is the original document directory -ls.args <- commandArgs(trailingOnly=TRUE) +.cmdargs <- commandArgs(trailingOnly=TRUE) +.doc.enc <- .cmdargs[3] # check whether Sweave.sty is seen by LaTeX. if it is not, we will -# 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')) +} -# set default encoding for input and output files; ls.enc is used in -# the sweave module preamble to reset the encoding to what it was. -ls.enc <- getOption("encoding") -options(encoding=ls.args[3]) # Change current directory to the document directory, so that R can find # data files. -setwd(ls.args[4]) +setwd(.cmdargs[4]) # this is passed as a prefix.string to tell where temporary files should go -ls.pr <- gsub('\\.([^/]*)$', '-\\1', sub("\\.tex$", "", ls.args[2])) - -# Replace the default pdf device by the null device (tip from Yihui Xie) -# See: http://yihui.name/en/2010/12/a-special-graphics-device-in-r-the-null-device/ -.Call("R_GD_nullDevice", PACKAGE = "grDevices") +# the output file without extension and without '.' +tmpout <- gsub(".", "-", sub("\\.tex$", "", basename(.cmdargs[2])), fixed = TRUE) +# replace +.prefix.str <- paste(dirname(.cmdargs[2]), tmpout, sep="/") +rm(tmpout) +# avoid the default Rplots.pdf +options(device = function(...) { + pdf(file = tempfile()) +}) # finally run sweave -Sweave(file=ls.args[1], output=ls.args[2], syntax="SweaveSyntaxNoweb", stylepath=ls.sp, prefix.string=ls.pr) + +# The Sweave version provided with R >= 0.13.1 has proper handling for +# encodings and our workaround for previous versions does not work +# anymore. Therefore, the invocation has to be different. +if (is.null(formals(Sweave)$encoding)) { + # set default encoding for input and output files; .orig.enc is used in + # the sweave module preamble to reset the encoding to what it was. + .orig.enc <- getOption("encoding") + options(encoding=.doc.enc) + Sweave(file=.cmdargs[1], output=.cmdargs[2], syntax="SweaveSyntaxNoweb", + prefix.string=.prefix.str) +} else { + Sweave(file=.cmdargs[1], output=.cmdargs[2], syntax="SweaveSyntaxNoweb", + prefix.string=.prefix.str, encoding=.doc.enc) +} + +# remove absolute path from \includegraphics +options(encoding=.doc.enc) # encoding may have been changed in the preamble chunk +ls.doc <- readLines(.cmdargs[2]) +ls.cmd <- paste('\\includegraphics{', dirname(.cmdargs[2]), "/", sep = "") +ls.idx <- grep(ls.cmd, ls.doc, fixed = TRUE) +if (length(ls.idx)) { + ls.doc[ls.idx] <- sub(ls.cmd, "\\includegraphics{", ls.doc[ls.idx], fixed = TRUE) + writeLines(ls.doc, .cmdargs[2]) +}