Skip to content

Commit

Permalink
add preview arg
Browse files Browse the repository at this point in the history
  • Loading branch information
philchalmers committed Sep 6, 2024
1 parent de64a90 commit ca4c20a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
33 changes: 33 additions & 0 deletions R/Eqn.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#' properties of the \code{matrix} input objects. Note that these inputs are used globally, and apply to
#' each \code{matrix} objects supplied. If further specificity is required create
#' \code{\link{latexMatrix}} objects directly.
#' @param preview logical; render an HTML version of the equation and display? This is intended for
#' testing purposes and is only applicable to interactive R sessions
#' @returns NULL
#' @importFrom knitr is_html_output
#' @author Phil Chalmers
Expand All @@ -52,6 +54,9 @@
#' # character input
#' Eqn('e=mc^2')
#'
#' # preview the equation
#' Eqn('e=mc^2', preview=TRUE)
#'
#' # Equation numbers & labels
#' Eqn('e=mc^2', label = 'eq:einstein')
#' Eqn("X=U \\lambda V", label='eq:svd')
Expand Down Expand Up @@ -86,6 +91,13 @@
#' latexMatrix("v", "k", "p", transpose = TRUE),
#' align=TRUE)
#'
#' # preview the equation
#' Eqn("X &= U \\lambda V", Eqn_newline(),
#' "& = ", latexMatrix("u", "n", "k"),
#' latexMatrix("\\lambda", "k", "k", diag=TRUE),
#' latexMatrix("v", "k", "p", transpose = TRUE),
#' align=TRUE, preview=TRUE)
#'
#' # numeric/character matrix example
#' A <- matrix(c(2, 1, -1,
#' -3, -1, 2,
Expand All @@ -104,9 +116,25 @@
Eqn <- function(...,
label = NULL,
align = FALSE,
preview = FALSE,
html_output = knitr::is_html_output(),
quarto = getOption('quartoEqn'),
mat_args = list()) {
preview <- preview && interactive()
if(preview){
quarto <- FALSE
tmpfile <- tempfile()
# everything except the kitchen ...
sink(tmpfile)
on.exit(file.remove(tmpfile))
cat(
"
---
title: '&nbsp;'
---
")

}
if(is.null(quarto)) quarto <- FALSE
stopifnot(is.logical(quarto))
number <- !is.null(label)
Expand Down Expand Up @@ -143,6 +171,11 @@ Eqn <- function(...,
}
cat("\n")
}
if(preview){
sink()
rmarkdown::render(tmpfile, 'html_document', clean = TRUE, quiet = TRUE)
rstudioapi::viewer(paste0(tmpfile, '.html'))
}
invisible(NULL)
}

Expand Down
14 changes: 14 additions & 0 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 ca4c20a

Please sign in to comment.