Skip to content

Commit

Permalink
make .qmd files autobehave the same as standard knitr files
Browse files Browse the repository at this point in the history
  • Loading branch information
philchalmers committed Sep 8, 2024
1 parent 8ee99b3 commit 011681c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 51 deletions.
48 changes: 34 additions & 14 deletions R/Eqn.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -189,6 +185,30 @@ title: '&nbsp;'
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
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 3 additions & 29 deletions dev/Eqn_test_quarto.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
12 changes: 4 additions & 8 deletions man/Eqn.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 011681c

Please sign in to comment.