## Configure LyX and Sweave Automatically under Windows, Mac and Ubuntu ## Author: Yihui Xie ## License: GPL-3 ## URL: http://yihui.name/en/2010/10/how-to-start-using-pgfsweave-in-lyx-in-one-minute ## This script comes with ABSOLUTELY NO WARRANTY. Please use it at your own risk. if (compareVersion(paste(R.version$major, R.version$minor, sep='.'), '2.12.0') < 0) { (if (.Platform$OS.type == 'windows') winDialog else warning)(message = 'This script expects at least R 2.12.0; please update your R first.') } ## the three lines below are nasty (force you to install and update) but they can remove lots of puzzles if (!require("pgfSweave")) install.packages("pgfSweave", Sys.getenv('R_LIBS_USER')) if (!require("xtable")) install.packages("xtable", Sys.getenv('R_LIBS_USER')) if (class(x <- try(update.packages(ask = FALSE, repos = 'http://cran.r-project.org'))) == 'try-error') { warning('Unable to update all the packages. You have to run this script with system admin.') try(update.packages(Sys.getenv('R_LIBS_USER'), ask = FALSE, repos = 'http://cran.r-project.org')) } owd = setwd(tempdir()) ## download Sweave.sty and related files url.prefix = 'http://gitorious.org/yihui/lyx-sweave/blobs/raw/master/texmf/' tex.files = c("bibtex/bib/RJournal.bib", "bibtex/bib/Rnews.bib", "bibtex/bst/jss.bst", "tex/latex/jss.cls", "tex/latex/omsaer.fd", "tex/latex/omsaett.fd", "tex/latex/omscmtt.fd", "tex/latex/Rd.sty", "tex/latex/Sweave.sty", "tex/latex/ts1aer.fd", "tex/latex/ts1aett.fd", "tex/latex/upquote.sty") sapply(unique(dirname(tex.files)), dir.create, showWarnings=FALSE, recursive=TRUE) sapply(tex.files, function(f) download.file(paste(url.prefix, f, sep=''), f)) tex.files = c('bibtex', 'tex') layout.files = c("ctex-article-sweave.layout", "ctex-article.layout", "ctex-book-sweave.layout", "ctex-book.layout", "ctex-report-sweave.layout", "ctex-report.layout", "literate-article.layout", "literate-book.layout", "literate-report.layout", "literate-scrap.inc", "literate-jss.layout", "literate-beamer.layout") sapply(layout.files, function(f) download.file(paste("http://gitorious.org/yihui/lyx-sweave/blobs/raw/master/lyx/layouts/", f, sep = ""), f)) download.file('http://gitorious.org/yihui/lyx-sweave/blobs/raw/master/lyx-sweave-exec.R', 'lyx-sweave-exec.R') ## update the preferences file updatePref = function(){ dir.create(lyxscripts <- file.path(dirname(lyxpref), 'scripts'), showWarnings=FALSE) file.copy('lyx-sweave-exec.R', lyxscripts, overwrite = TRUE) xx = c("\\format \"r\" \"R\" \"R/S code\" \"\" \"\" \"\" \"document\"", "\\format \"literate\" \"Rnw\" \"Sweave\" \"\" \"\" \"\" \"document\"", "\\converter \"literate\" \"r\" \"R CMD Stangle $$i\" \"\"", "\\converter \"literate\" \"latex\" \"Rscript %s%s/lyx-sweave-exec.R%s %sUTF-8%s %spgfSweave%s $$r $$p $$i > $$i.log 2>&1\" \"\"", "\\converter \"literate\" \"pdflatex\" \"Rscript %s%s/lyx-sweave-exec.R%s %sUTF-8%s %spgfSweave%s $$r $$p $$i > $$i.log 2>&1\" \"\"") xx[4] = sprintf(xx[4], quote.char, lyxscripts, quote.char, quote.char, quote.char, quote.char, quote.char) xx[5] = sprintf(xx[5], quote.char, lyxscripts, quote.char, quote.char, quote.char, quote.char, quote.char) roa = function(pattern, value) { idx = grep(pattern, x) if (!length(idx)) append(x, value) else { x[idx] = value x } } if (file.exists(lyxpref)) { x = readLines(lyxpref) x = roa('\\\\format "r".*', xx[1]) x = roa('\\\\format "literate".*', xx[2]) x = roa('\\converter "literate" "r".*', xx[3]) x = roa('\\converter "literate" "latex".*', xx[4]) x = roa('\\converter "literate" "pdflatex".*', xx[5]) writeLines(x, lyxpref) } else { writeLines(xx, lyxpref) } message('LyX preferences updated') flush.console() } if (.Platform$OS.type == 'windows') { ## make sure you have installed R and LyX/MikTeX; this R script will ## take care of the gory details which nobody wants to know except hackers ## it was tested under Win Server 2003, Win Vista and Win 7 miktex = length(grep('MiKTeX', system('latex --version', intern = TRUE))) > 0 if (miktex) { ## install an additional required LaTeX package system("mpm --install mptopdf") system("mpm --install fancyvrb") system("mpm --install ae") system("mpm --install ms") system("mpm --install xcolor") system("mpm --install pgf") if (length(grep('Chinese', Sys.getlocale()))) { system("mpm --install ctex") ## if you are in the Chinese locale, you have to set the locale in order to call R in LyX Rprofile = file.path(Sys.getenv('HOME'), '.Rprofile') if (!file.exists(Rprofile) || !length(grep('Sys\\.setlocale', readLines(Rprofile)))) cat('if (.Platform$OS.type == "windows" && !interactive()) invisible({ Sys.setlocale(, "Chinese (Simplified)_People\'s Republic of China.936") })\n', file = Rprofile, append = TRUE) } ## a tricky way to get the User's directory for MikTeX mikreport = system("initexmf --report", intern = TRUE) mikpath = sub("^UserInstall: ", "", grep("^UserInstall: ", mikreport, value = TRUE)) if (!length(mikpath)) { mikpath = sub("^UserData: ", "", grep("^UserData: ", mikreport, value = TRUE)) } ## create some directories in the 'Application Data' directory dir.create(mikpath, showWarnings = FALSE, recursive = TRUE) file.copy(tex.files, mikpath, recursive = TRUE) ## update LaTeX filename database if (system("initexmf --admin --update-fndb -v") != 0) { system("initexmf --update-fndb -v") } } else { ## you are probably using TeXLive texpath = file.path(Sys.getenv('USERPROFILE'), 'texmf') dir.create(texpath, showWarnings = FALSE, recursive = TRUE) file.copy(tex.files, texpath, recursive = TRUE) try(system(sprintf('texhash %s', shQuote(normalizePath(texpath)))), TRUE) } message('required LaTeX packages installed') flush.console() lyxpath = head(grep('.*(lyx)([0-9]+)$', list.files(Sys.getenv("APPDATA"), full.names=TRUE), ignore.case=TRUE, value=TRUE), 1) if (!length(lyxpath)) lyxpath = file.path(Sys.getenv("APPDATA"), "LyX16") try(file.remove(file.path(lyxpath, list.files(lyxpath, '\\.lst$'))), silent=TRUE) dir.create(file.path(lyxpath, 'layouts'), showWarnings = FALSE, recursive = TRUE) file.copy(layout.files, file.path(lyxpath, 'layouts'), overwrite=TRUE) message('required LyX layouts installed') ## configure LyX lyxpref = file.path(lyxpath, 'preferences') quote.char = "\\\"" updatePref() ## put R's bin path to the system PATH variable if (shell('R --version') != 0) { system(paste('setx PATH "', normalizePath(file.path(R.home(), 'bin')), ';', Sys.getenv('PATH'), '"', sep = '')) } ## try really hard to find where is LyX in your system if (!inherits(try({ lyxinstall = readRegistry("LyX.Document\\Shell\\open\\command", "HCR") }, silent = TRUE), "try-error")) { lyxinstall = dirname(gsub("(^\"|\" \"%1\"$)", "", lyxinstall[[1]])) lyxbin = file.path(lyxinstall, 'LyX.exe') } else { lyxbin = file.path(grep('LyX', list.files(Sys.getenv('ProgramFiles'), full.names=TRUE), value=TRUE), 'bin', 'LyX.exe') } while (!file.exists(lyxbin)) { winDialog('ok', 'Oh my Gosh, I really cannot find where you installed LyX... Please select the folder containing "LyX.exe" in the next diaglog box.') lyxbin = file.path(choose.dir(Sys.getenv('ProgramFiles'), 'Where is LyX?'), 'bin', 'LyX.exe') } message('Please restart LyX after you see the message "The system has been reconfigured"') ## setwd(lyxpath) ## system(paste(shQuote(normalizePath(file.path(lyxbin, c('../../python/python.exe', '../../Resources/scripts/TeXFiles.py')))), collapse=' ')) system(paste(shQuote(normalizePath(lyxbin)), '-x reconfigure')) ## done! yeah!! Thank you, Microsoft Windows! } else if (Sys.info()["sysname"] == "Linux") { ## configuration under Ubuntu is a piece of cake oowd = setwd(Sys.getenv('HOME')) dir.create('texmf', showWarnings=FALSE) file.copy(file.path(oowd, tex.files), 'texmf/', recursive=TRUE) message('required LaTeX packages installed') dir.create('.lyx/layouts', showWarnings=FALSE, recursive=TRUE) file.copy(file.path(tempdir(), layout.files), '.lyx/layouts', overwrite=TRUE) message('required LyX layouts installed') try(file.remove(list.files('.lyx/', '\\.lst$', full.names=TRUE)), TRUE) lyxpref = file.path(getwd(), '.lyx/preferences') quote.char = "'" setwd(oowd) updatePref() system('lyx -x reconfigure') } else if (Sys.info()["sysname"] == "Darwin") { oowd = setwd(Sys.getenv('HOME')) dir.create('Library/texmf', showWarnings=FALSE) file.copy(file.path(oowd, tex.files), 'Library/texmf', recursive=TRUE) message('required LaTeX packages installed') lyxpath = tail(grep('^LyX', list.files('Library/Application Support', full.names=TRUE), value=TRUE), 1) if (!length(lyxpath)) lyxpath = 'Library/Application Support/LyX-1.6' lyxpath = file.path(getwd(), lyxpath) dir.create(file.path(lyxpath, 'layouts'), showWarnings=FALSE, recursive=TRUE) file.copy(file.path(tempdir(), layout.files), file.path(lyxpath, 'layouts'), overwrite=TRUE) message('required LyX layouts installed') try(file.remove(file.path(lyxpath, list.files(lyxpath, '\\.lst$'))), TRUE) lyxpref = file.path(lyxpath, 'preferences') quote.char = "'" setwd(oowd) updatePref() ## Done! Thank you, Mac OS! } else { message('I really have no idea about your OS. Sorry.') } setwd(owd)