]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyxsweave.R
Add a preference option to specify the default length unit (cm or in).
[lyx.git] / lib / scripts / lyxsweave.R
1 # file lyxsweave.R
2 # This file is part of LyX, the document processor.
3 # Licence details can be found in the file COPYING.
4
5 # author Jean-Marc Lasgouttes
6 # author Yihui Xie
7
8 # Full author contact details are available in file CREDITS
9
10 # Wrapper around Sweave that sets up some things for LyX
11 # argument 1 is the absolute name of the input file
12 # argument 2 is the absolute name of the output file
13 # argument 3 is the iconv name for the encoding of the file
14 # argument 4 is the original document directory
15
16 .cmdargs <- commandArgs(trailingOnly=TRUE)
17
18 # check whether Sweave.sty is seen by LaTeX. if it is not, we will
19 # copy it alongside the .tex file (in general in the temporary
20 # directory). This means that an exported LaTeX file will not work,
21 # but this is a problem of installation of R on the user's machine.
22 # The advantage compared to the use of stylepath, is that the exported
23 # .tex file will be portable to another machine. (JMarc)
24 if (!length(system("kpsewhich Sweave.sty", intern=TRUE, ignore.stderr=TRUE))) {
25   file.copy(file.path(R.home("share"), "texmf", "tex", "latex", "Sweave.sty"),
26     dirname(.cmdargs[2]), overwrite=TRUE)
27 }
28
29 # set default encoding for input and output files; .orig.enc is used in
30 # the sweave module preamble to reset the encoding to what it was.
31 .orig.enc <- getOption("encoding")
32 options(encoding=.cmdargs[3])
33
34 # Change current directory to the document directory, so that R can find 
35 # data files.
36 setwd(.cmdargs[4])
37
38 # this is passed as a prefix.string to tell where temporary files should go
39 # the output file without extension and without '.'
40 tmpout <- gsub(".", "-", sub("\\.tex$", "", basename(.cmdargs[2])), fixed = TRUE)
41 # replace
42 .prefix.str <- paste(dirname(.cmdargs[2]), tmpout, sep="/")
43 rm(tmpout)
44
45 # finally run sweave
46 Sweave(file=.cmdargs[1], output=.cmdargs[2], syntax="SweaveSyntaxNoweb", prefix.string=.prefix.str)
47
48 # remove absolute path from \includegraphics
49 options(encoding=.cmdargs[3])  # encoding has been changed in the preamble chunk
50 ls.doc = readLines(.cmdargs[2])
51 ls.cmd = paste('\\includegraphics{', dirname(.cmdargs[2]), "/", sep = "")
52 ls.idx = grep(ls.cmd, ls.doc, fixed = TRUE)
53 if (length(ls.idx)) {
54    ls.doc[ls.idx] = sub(ls.cmd, "\\includegraphics{", ls.doc[ls.idx], fixed = TRUE)
55    writeLines(ls.doc, .cmdargs[2])
56 }