diff --git a/R/add_report.R b/R/add_report.R index b0372022ea54c7673d06662768754e09b8445f0a..c202398ecebb2f47364f8a14d67c82b4f115a7b7 100644 --- a/R/add_report.R +++ b/R/add_report.R @@ -40,12 +40,14 @@ add_report <- function(name, destpath ) + bookdown_config <- list( + language = list(ui = list( + chapter_name = paste0(stringr::str_to_title(i18n("chapter", lang)), " ") + )), + babel_lang = lang + ) cat( - yaml::as.yaml(list(language = list( - ui = list(chapter_name = paste0(stringr::str_to_title( - i18n("chapter", lang) - ), " ")) - ))), + yaml::as.yaml(bookdown_config), file = file.path(destpath, "_bookdown.yml"), append = TRUE ) @@ -68,13 +70,8 @@ add_report <- function(name, "% Add your own settings below to append or overwrite template settings") writeLines(tex_content, file.path(destpath, sprintf("%s.tex", x))) }) - if ("tinytex" %in% rownames(installed.packages())) { - # Install babel LaTeX package for the specified language - babel_pkg <- paste0("babel-", lang) - if (! babel_pkg %in% tinytex::tl_pkgs(only_installed = TRUE)) { - tinytex::tlmgr_install(babel_pkg) - } - } + + tinytex_install_babel_language_support(lang) return(destpath) } diff --git a/R/render_report.R b/R/render_report.R index a80b31682a0404583d9ea12db9072cb9bf510cde..98fcd453a8931e3c234358d6bd03dc0de1a06f82 100644 --- a/R/render_report.R +++ b/R/render_report.R @@ -10,7 +10,13 @@ render_report <- function(input, stopifnot(output_format %in% c("bookdown::gitbook", "bookdown::pdf_book")) if (output_format == "bookdown::pdf_book") { - if (!requireNamespace("tinytex", quietly=TRUE)) install.packages("tinytex") + if (!requireNamespace("tinytex", quietly = TRUE)) install.packages("tinytex") + if (dir.exists(input)) { + babel_lang <- yaml::read_yaml(file.path(input, "_bookdown.yml"))$babel_lang + if (!is.null(babel_lang)) { + tinytex_install_babel_language_support(babel_lang) + } + } } message("output_format=", output_format) diff --git a/R/utils.R b/R/utils.R index f4865bc1ec077df36eb9ab43774693b733c59015..74dea2982d93d20e2a9bc5b19c6fa9c62dee9c9e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -53,3 +53,13 @@ purge_string <- function(x, replacement = " ") { from = "UTF-8", to = "ASCII//TRANSLIT") } + +tinytex_install_babel_language_support <- function(lang) { + if ("tinytex" %in% rownames(installed.packages())) { + # Install babel LaTeX package for the specified language + babel_pkg <- paste0("babel-", lang) + if (! babel_pkg %in% tinytex::tl_pkgs(only_installed = TRUE)) { + tinytex::tlmgr_install(babel_pkg) + } + } +}