Is there a way for a knitr engine to delete a file after a latex render #12524
-
DescriptionI have a custom knitr engine as part of I figured out how to embed the tex and image files within the knitr output so that those files so that those files can be deleted before the render; however, the style file needs to be there for the tex output to be rendered by the latex engine. Is there a way I can delete the style file produced by the engine after the latex render finishes? Here is a minimal version of what I would be talking about: ---
format: pdf
---
```{r, output = FALSE}
custom_engine <- function(options) {
writeLines(
c(
"\\NeedsTeXFormat{LaTeX2e}",
"\\ProvidesPackage{highlight}[2025/04/11 Simple text highlighting]",
"\\RequirePackage{xcolor}",
"\\newcommand{\\highlight}[1]{\\colorbox{yellow}{#1}}",
"\\endinput"
),
"style.sty"
)
options$results <- "asis"
knitr::engine_output(
options,
code = "",
out = "This is \\highlight{important} text."
)
}
style_dependency <- list(
name = "style",
options = NULL,
extra_lines = NULL
)
class(style_dependency) <- "latex_dependency"
knitr::knit_meta_add(list(style_dependency))
knitr::knit_engines$set(custom = custom_engine)
```
```{custom}
whatever
``` Now, there is a style.sty file in the same directory as qmd file. I would prefer it didn't exist. Options I considered:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
First, don't use R inline syntax for code cell options. Simple rule is: there is nothing but the language between the brackets. Quarto uses YAML style.
instead of
Regarding your question, use post-render script. |
Beta Was this translation helpful? Give feedback.
-
Check out the withr package, it has internal functions that allow you to defer an operation when knitr exists or retrieve knitr's exist frame. You can copy the code and reuse it in your package (see https://r-pkgs.org/license.html#sec-code-you-bundle to properly license the code and credit their authors). You can use
Note that I opened an issue some months ago to ask the developers if they could export |
Beta Was this translation helpful? Give feedback.
If you don't want or cannot use post-render, and want to do it in R, then it's unrelated to Quarto CLI.