2011-04-13 10:04:20 +00:00
|
|
|
# file lyxsweave.R
|
|
|
|
# This file is part of LyX, the document processor.
|
|
|
|
# Licence details can be found in the file COPYING.
|
|
|
|
|
|
|
|
# author Jean-Marc Lasgouttes
|
2011-06-20 10:59:23 +00:00
|
|
|
# author Yihui Xie
|
2011-04-13 10:04:20 +00:00
|
|
|
|
|
|
|
# Full author contact details are available in file CREDITS
|
|
|
|
|
2010-10-21 17:02:05 +00:00
|
|
|
# Wrapper around Sweave that sets up some things for LyX
|
2011-03-21 14:48:22 +00:00
|
|
|
# argument 1 is the absolute name of the input file
|
|
|
|
# argument 2 is the absolute name of the output file
|
|
|
|
# argument 3 is the iconv name for the encoding of the file
|
|
|
|
# argument 4 is the original document directory
|
2010-10-21 17:02:05 +00:00
|
|
|
|
2011-08-23 09:52:21 +00:00
|
|
|
.cmdargs <- commandArgs(trailingOnly=TRUE)
|
2010-10-21 17:02:05 +00:00
|
|
|
|
2011-04-13 10:04:20 +00:00
|
|
|
# check whether Sweave.sty is seen by LaTeX. if it is not, we will
|
2011-06-20 10:59:23 +00:00
|
|
|
# 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)
|
2011-08-23 09:52:21 +00:00
|
|
|
if (!length(system("kpsewhich Sweave.sty", intern=TRUE, ignore.stderr=TRUE))) {
|
|
|
|
file.copy(file.path(R.home("share"), "texmf", "tex", "latex", "Sweave.sty"),
|
|
|
|
dirname(.cmdargs[2]), overwrite=TRUE)
|
2011-06-20 10:59:23 +00:00
|
|
|
}
|
2010-10-21 17:02:05 +00:00
|
|
|
|
2011-08-23 09:52:21 +00:00
|
|
|
# set default encoding for input and output files; .orig.enc is used in
|
2011-04-13 10:04:20 +00:00
|
|
|
# the sweave module preamble to reset the encoding to what it was.
|
2011-08-23 09:52:21 +00:00
|
|
|
.orig.enc <- getOption("encoding")
|
|
|
|
options(encoding=.cmdargs[3])
|
2010-10-22 07:51:39 +00:00
|
|
|
|
2011-04-13 10:04:20 +00:00
|
|
|
# Change current directory to the document directory, so that R can find
|
|
|
|
# data files.
|
2011-08-23 09:52:21 +00:00
|
|
|
setwd(.cmdargs[4])
|
2010-12-12 21:25:57 +00:00
|
|
|
|
2011-04-13 10:04:20 +00:00
|
|
|
# this is passed as a prefix.string to tell where temporary files should go
|
2011-06-15 14:51:50 +00:00
|
|
|
# the output file without extension and without '.'
|
2011-08-23 09:52:21 +00:00
|
|
|
tmpout <- gsub(".", "-", sub("\\.tex$", "", basename(.cmdargs[2])), fixed = TRUE)
|
|
|
|
# replace
|
|
|
|
.prefix.str <- paste(dirname(.cmdargs[2]), tmpout, sep="/")
|
|
|
|
rm(tmpout)
|
2011-03-21 14:48:22 +00:00
|
|
|
|
|
|
|
# finally run sweave
|
2011-08-23 09:52:21 +00:00
|
|
|
Sweave(file=.cmdargs[1], output=.cmdargs[2], syntax="SweaveSyntaxNoweb", prefix.string=.prefix.str)
|
2011-06-15 15:51:02 +00:00
|
|
|
|
|
|
|
# remove absolute path from \includegraphics
|
2011-08-23 09:52:21 +00:00
|
|
|
options(encoding=.cmdargs[3]) # encoding has been changed in the preamble chunk
|
|
|
|
ls.doc = readLines(.cmdargs[2])
|
|
|
|
ls.cmd = paste('\\includegraphics{', dirname(.cmdargs[2]), "/", sep = "")
|
2011-06-15 15:51:02 +00:00
|
|
|
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)
|
2011-08-23 09:52:21 +00:00
|
|
|
writeLines(ls.doc, .cmdargs[2])
|
2011-06-15 15:51:02 +00:00
|
|
|
}
|