Skip to content

Commit

Permalink
close #32: support the global option tinytex.output_dir
Browse files Browse the repository at this point in the history
you may set the global options to redirect the PDF output file to another directory, e.g., to the temp dir:

options(tinytex.output_dir = tempdir(), tinytex.engine_args = shQuote(paste0('--output-directory=', tempdir())))
  • Loading branch information
yihui committed Mar 29, 2018
1 parent dc83674 commit 66a4e52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: tinytex
Type: Package
Title: Helper Functions to Install and Maintain 'TeX Live', and Compile 'LaTeX' Documents
Version: 0.4.3
Version: 0.4.4
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person(family = "RStudio, Inc.", role = "cph"),
Expand Down
18 changes: 15 additions & 3 deletions R/latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,18 @@ latexmk = function(
if (missing(engine_args)) engine_args = getOption('tinytex.engine_args', engine_args)
if (missing(clean)) clean = getOption('tinytex.clean', TRUE)
pdf = gsub('[.]tex$', '.pdf', basename(file))
output_dir = getOption('tinytex.output_dir')
if (!is.null(output_dir)) {
output_dir_arg = shQuote(paste0(if (emulation) '-', '-output-directory=', output_dir))
if (length(grep(output_dir_arg, engine_args, fixed = TRUE)) == 0) stop(
"When you set the global option 'tinytex.output_dir', the argument 'engine_args' ",
"must contain this value: ", capture.output(dput(output_dir_arg))
)
pdf = file.path(output_dir, pdf)
if (missing(pdf_file)) pdf_file = file.path(output_dir, basename(pdf_file))
}
check_pdf = function() {
if (!file.exists(pdf)) show_latex_error(file)
if (!file.exists(pdf)) show_latex_error(file, sub('.pdf$', '.log', pdf))
file_rename(pdf, pdf_file)
pdf_file
}
Expand All @@ -87,7 +97,7 @@ latexmk = function(
)
check_latexmk_version()
})
if (clean) system2('latexmk', '-c', stdout = FALSE) # clean up nonessential files
if (clean) system2('latexmk', c('-c', engine_args), stdout = FALSE)
check_pdf()
}

Expand Down Expand Up @@ -118,6 +128,8 @@ latexmk_emu = function(
)
base = gsub('[.]tex$', '', basename(file))
aux_files = paste(base, aux, sep = '.')
if (!is.null(output_dir <- getOption('tinytex.output_dir')))
aux_files = file.path(output_dir, aux_files)
logfile = aux_files[1]; unlink(logfile) # clean up the log before compilation

# clean up aux files from LaTeX compilation
Expand All @@ -131,7 +143,7 @@ latexmk_emu = function(
}, add = TRUE)

pkgs_last = character()
filep = paste0(base, '.pdf')
filep = sub('.log$', '.pdf', logfile)
run_engine = function() {
on_error = function() {
if (install_packages && file.exists(logfile)) {
Expand Down

0 comments on commit 66a4e52

Please sign in to comment.