From b8223a135c8ddfe6eef81a2cb1a2c835aafdb5d5 Mon Sep 17 00:00:00 2001 From: Sergio Oller Date: Sun, 2 Jun 2024 19:31:33 +0200 Subject: [PATCH 1/2] Restore out.format to its former value out.format needs to be set for knit_theme$get(thm) to work. However, when building a pkgdown article based on a vignette that uses BiocStyle::pdf_document, the out.format is forced to be html, and hardcoding this knit option clashes with pkgdown. By setting and restoring the output format, things keep working as expected --- R/pdf_document.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/pdf_document.R b/R/pdf_document.R index 06ca48e..2c00dd0 100644 --- a/R/pdf_document.R +++ b/R/pdf_document.R @@ -80,12 +80,14 @@ pdf_document <- function(toc = TRUE, ## code chunks and code highlighting thm <- system.file("themes", "default.css", package = "BiocStyle") - opts_knit$set(out.format="latex"); + out_fmt <- opts_knit$get("out.format") + opts_knit$set(out.format="latex") head = c(head, "% code highlighting", knit_theme$get(thm)$highlight, readLines(file.path(resources, "tex", "highlighting-macros.def"))) - + opts_knit$set(out.format=out_fmt) + if (use_unsrturl) head = c(head, sprintf("\\AtBeginDocument{\\bibliographystyle{%s}}\n", sub(".bst$", "", template_files[["bst"]]))) From cd61391fe83c465378e316eb26a2e1cf879572b5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 11 Jul 2026 06:52:17 +0000 Subject: [PATCH 2/2] Add regression test for knitr out.format leak in pdf_document() (#108) BiocStyle::pdf_document() sets the global knitr option 'out.format' to "latex" to look up the syntax highlighting theme but did not restore its previous value, corrupting state for documents rendered later in the same session (e.g. pkgdown building multiple articles in-process). --- inst/unitTests/test_pdf_document.R | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/inst/unitTests/test_pdf_document.R b/inst/unitTests/test_pdf_document.R index 478cfb5..112cc07 100644 --- a/inst/unitTests/test_pdf_document.R +++ b/inst/unitTests/test_pdf_document.R @@ -2,9 +2,27 @@ test_pdf_document <- function() { filename <- rmarkdown::draft(tempfile(fileext = ".Rmd"), edit = FALSE, template="pdf_document", package = "BiocStyle") lines <- BiocStyle:::readUTF8(filename) - lines <- BiocStyle:::modifyLines(lines, from = "title:", replace = FALSE, + lines <- BiocStyle:::modifyLines(lines, from = "title:", replace = FALSE, insert='subtitle: "Vignette Subtitle"') BiocStyle:::writeUTF8(lines, filename) rmarkdown::render(filename) unlink(filename) } + +test_pdf_document_restores_out_format <- function() { + ## Regression test for https://github.com/Bioconductor/BiocStyle/pull/108 + ## + ## BiocStyle::pdf_document() temporarily sets the global knitr option + ## 'out.format' to "latex" in order to look up the syntax highlighting + ## theme, but used to leave it set afterwards. This corrupted the knitr + ## state for anything rendered later in the same R session, e.g. pkgdown + ## building several articles (one of which uses BiocStyle::pdf_document) + ## within a single process, which itself relies on 'out.format' being + ## "html". + out_format_before <- knitr::opts_knit$get("out.format") + on.exit(knitr::opts_knit$set(out.format = out_format_before)) + + knitr::opts_knit$set(out.format = "html") + invisible(BiocStyle::pdf_document()) + checkIdentical(knitr::opts_knit$get("out.format"), "html") +}