From 011681cebe67d4c1de4c01eb7142e3e24e27111e Mon Sep 17 00:00:00 2001 From: philchalmers Date: Sat, 7 Sep 2024 22:38:15 -0400 Subject: [PATCH] make .qmd files autobehave the same as standard knitr files --- R/Eqn.R | 48 +++++++++++++++++++++++++++++------------ dev/Eqn_test_quarto.qmd | 32 +++------------------------ man/Eqn.Rd | 12 ++++------- 3 files changed, 41 insertions(+), 51 deletions(-) diff --git a/R/Eqn.R b/R/Eqn.R index 16f170a3..d4e5ed1e 100644 --- a/R/Eqn.R +++ b/R/Eqn.R @@ -5,7 +5,8 @@ #' The \code{Eqn} function is designed to produce LaTeX expressions of mathematical #' equations for writing. #' The output can be copied/pasted into documents or -#' used directly in chunks in \code{.Rmd} or \code{.qmd} documents to compile to equations. +#' used directly in chunks in \code{.Rmd}, \code{.Rnw}, or \code{.qmd} +#' documents to compile to equations. #' It wraps the equations generated by its arguments #' in either a \code{\\begin{equation} ...\\end{equation}} or #' \code{\\begin{align} ...\\end{align}} LaTeX environment. \code{ref()} provides for @@ -32,10 +33,11 @@ #' then the equations will be labelled via \code{(\#eq:myeqn)} and references via \code{\@ref(eq:myeqn)}, #' or again via \code{\link{ref}} for convenience #' @param html_output logical; use labels for HTML outputs instead of the LaTeX? Automatically -#' changed for compiled documents that support \code{knitr} +#' changed for compiled documents that support \code{knitr}. Generally not +#' required or recommended for the user to modify, except to view the generated syntax #' @param quarto logical; use Quarto referencing syntax? When \code{TRUE} -#' the \code{html_output} will be irrelevant. Can be set globally via -#' \code{\link{options}} with \code{options(quartoEqn=TRUE)} +#' the \code{html_output} will be irrelevant. Generally not recommended for the +#' user to modify, except to view the generated syntax #' @param align logical; use the \code{align} environment with explicit \code{&} representing alignment #' points. Default: \code{FALSE} #' @param mat_args list of arguments to be passed to \code{\link{latexMatrix}} to change the @@ -64,18 +66,13 @@ #' Eqn('e=mc^2', label = 'eq:einstein') #' Eqn("X=U \\lambda V", label='eq:svd') #' -#' # html_output and quarto outputs only show code: -#' # (auto detected in compiled documents) +#' # html_output and quarto outputs only show code +#' # (both auto detected in compiled documents) #' Eqn('e=mc^2', label = 'eq:einstein', html_output = TRUE) #' #' # Quarto output #' Eqn('e=mc^2', label = 'eq-einstein', quarto = TRUE) #' -#' # Set Quarto option globally for all calls to Eqn() -#' options(quartoEqn=TRUE) -#' Eqn('e=mc^2', label = 'eq-einstein') -#' options(quartoEqn=FALSE) ## reset -#' #' # Multiple expressions #' Eqn("e=mc^2", #' Eqn_newline(), @@ -127,11 +124,10 @@ Eqn <- function(..., } } on.exit(sink.reset()) - if(is.null(quarto)) quarto <- FALSE + quarto <- setQuartoEqn(quarto) preview <- preview && interactive() if(html_output || quarto) preview <- FALSE if(preview){ - quarto <- FALSE tmpfile <- tempfile() # everything except the kitchen ... sink(tmpfile) @@ -189,6 +185,30 @@ title: ' ' invisible(NULL) } +setQuartoEqn <- function(quarto){ + if(interactive()){ + if(is.null(quarto)) quarto <- FALSE + return(quarto) + } + if(isTRUE(getOption('knitr.in.progress')) && is.null(getOption('quartoEqn'))){ + finp <- knitr::current_input() + stripped <- gsub('.rmarkdown', '', finp) + files <- dir() + if(sum(grepl(paste0(stripped, '.qmd'), files)) == 1) + options('quartoEqn' = TRUE) + else options('quartoEqn' = FALSE) + if(getOption('quartoEqn')){ + matched_files <- files[grepl(paste0(stripped, '.'), files, fixed=TRUE)] + exts <- gsub(paste0(stripped, '.'), "", matched_files, fixed=TRUE) + if(any(tolower(exts) %in% c('rmd', 'rnw'))) + stop(c('Detected files with identical names but different extensions. ', + '\n\n Please use unique file names when a mix of .qmd and R markdown files', + ' (e.g., .Rmd, .Rnw) are in the same directory.')) + } + } + invisible(getOption('quartoEqn')) +} + #' Emit a newline in an equation #' #' \code{Eqn_newline()} emits a newline (\code{\\}) in an equation @@ -396,7 +416,7 @@ ref <- function(label, parentheses = TRUE, html_output = knitr::is_html_output(), quarto = getOption('quartoEqn')) { - if(is.null(quarto)) quarto <- FALSE + quarto <- setQuartoEqn(quarto) ret <- if(quarto){ if(parentheses) sprintf('([-@%s])', label) diff --git a/dev/Eqn_test_quarto.qmd b/dev/Eqn_test_quarto.qmd index 62e7d2d2..8068c97d 100644 --- a/dev/Eqn_test_quarto.qmd +++ b/dev/Eqn_test_quarto.qmd @@ -51,35 +51,9 @@ Will also work within an equation, not that this is particularly useful. "@eq-binom" ``` -# Test if .qmd file used - -Set global to TRUE if .qmd file detected from input. Warning raised if files have the same name but different extension (`Eqn.qmd` and `Eqn.Rmd` will raise error if in same dir). - -```{r echo=FALSE} -setQuartoEqn <- function(){ - if(isTRUE(getOption('knitr.in.progress')) && is.null(getOption('quartoEqn'))){ - finp <- knitr::current_input() - stripped <- gsub('.rmarkdown', '', finp) - files <- dir() - if(sum(grepl(paste0(stripped, '.qmd'), files)) == 1) - options('quartoEqn' = TRUE) - else options('quartoEqn' = FALSE) - if(getOption('quartoEqn')){ - matched_files <- files[grepl(paste0(stripped, '.'), files, fixed=TRUE)] - exts <- gsub(paste0(stripped, '.'), "", matched_files, fixed=TRUE) - if(any(tolower(exts) %in% c('rmd', 'rnw'))) - stop(c('Detected files with identical names but different extensions. ', - '\n\n Please use unique file names when a mix of .qmd and R markdown files', - ' (e.g., .Rmd, .Rnw) are in the same directory.')) - } - } - invisible(NULL) -} -``` +# Auto use `quarto=TRUE` if .qmd file used -```{r echo=FALSE} -setQuartoEqn() -``` +Internally, set global to TRUE if .qmd file detected. Error raised if files have the same name but different extension (`Eqn.qmd` and `Eqn.Rmd` will raise error if in same dir). ```{r} @@ -88,4 +62,4 @@ Eqn("f\\left(k\\right) = \\binom{n}{k} p^k\\left(1-p\\right)^{n-k}", label = 'eq ``` -See `{r} ref("eq-auto", quarto=TRUE)` for auto-detected quarto label. +See `{r} ref("eq-auto", quarto=TRUE)` for auto-detected quarto label. Of course, the @ notation still works too; e.g., see @eq-auto for details diff --git a/man/Eqn.Rd b/man/Eqn.Rd index c471d84b..d5705425 100644 --- a/man/Eqn.Rd +++ b/man/Eqn.Rd @@ -106,7 +106,8 @@ Only changes the behaviour for referencing in LaTeX/PDF documents.} The \code{Eqn} function is designed to produce LaTeX expressions of mathematical equations for writing. The output can be copied/pasted into documents or -used directly in chunks in \code{.Rmd} or \code{.qmd} documents to compile to equations. +used directly in chunks in \code{.Rmd}, \code{.Rnw}, or \code{.qmd} +documents to compile to equations. It wraps the equations generated by its arguments in either a \code{\\begin{equation} ...\\end{equation}} or \code{\\begin{align} ...\\end{align}} LaTeX environment. \code{ref()} provides for @@ -150,18 +151,13 @@ Eqn('e=mc^2', preview=FALSE) Eqn('e=mc^2', label = 'eq:einstein') Eqn("X=U \\\\lambda V", label='eq:svd') -# html_output and quarto outputs only show code: -# (auto detected in compiled documents) +# html_output and quarto outputs only show code +# (both auto detected in compiled documents) Eqn('e=mc^2', label = 'eq:einstein', html_output = TRUE) # Quarto output Eqn('e=mc^2', label = 'eq-einstein', quarto = TRUE) -# Set Quarto option globally for all calls to Eqn() -options(quartoEqn=TRUE) -Eqn('e=mc^2', label = 'eq-einstein') -options(quartoEqn=FALSE) ## reset - # Multiple expressions Eqn("e=mc^2", Eqn_newline(),