diff --git a/R/save_kable.R b/R/save_kable.R index 1aa05ee..8040d9c 100644 --- a/R/save_kable.R +++ b/R/save_kable.R @@ -251,6 +251,12 @@ save_kable_latex <- function(x, file, latex_header_includes, keep_tex, density) file_no_ext <- tools::file_path_sans_ext(temp_tex_file) owd <- setwd(dirname(temp_tex_file)) + on.exit(setwd(owd), add = TRUE) + + if (!keep_tex) { + temp_file_delete <- paste0(file_no_ext, c(".tex", ".aux", ".log")) + on.exit(unlink(temp_file_delete), add = TRUE) + } if (!requireNamespace("tinytex", quietly = TRUE)) { system(paste0("xelatex -interaction=batchmode ", @@ -260,10 +266,6 @@ save_kable_latex <- function(x, file, latex_header_includes, keep_tex, density) tinytex::xelatex(gsub(pattern = " ", replacement = "\\ ", temp_tex_file, fixed = TRUE)) } - if (!keep_tex) { - temp_file_delete <- paste0(file_no_ext, c(".tex", ".aux", ".log")) - unlink(temp_file_delete) - } table_img_info <- NULL if (tools::file_ext(file) != "pdf") { @@ -285,8 +287,6 @@ save_kable_latex <- function(x, file, latex_header_includes, keep_tex, density) density = density) } - setwd(owd) - out <- paste0(file_no_ext, ".", tools::file_ext(file)) attr(out, "info") <- table_img_info return(invisible(out))