diff --git a/R/Eqn.R b/R/Eqn.R index 76c2be64..f5ec92f6 100644 --- a/R/Eqn.R +++ b/R/Eqn.R @@ -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 @@ -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') @@ -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, @@ -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: ' ' +--- +") + + } if(is.null(quarto)) quarto <- FALSE stopifnot(is.logical(quarto)) number <- !is.null(label) @@ -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) } diff --git a/man/Eqn.Rd b/man/Eqn.Rd index 6f7f55d7..68f6e885 100644 --- a/man/Eqn.Rd +++ b/man/Eqn.Rd @@ -14,6 +14,7 @@ Eqn( ..., label = NULL, align = FALSE, + preview = FALSE, html_output = knitr::is_html_output(), quarto = getOption("quartoEqn"), mat_args = list() @@ -54,6 +55,9 @@ defined explicitly in the document} \item{align}{logical; use the \code{align} environment with explicit \code{&} representing alignment points. Default: \code{FALSE}} +\item{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} + \item{html_output}{logical; use references for HTML outputs instead of the LaTeX? Automatically changed for compiled documents that support \code{knitr}} @@ -138,6 +142,9 @@ well as documentation types (\code{.Rmd} vs \code{.qmd}). # 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') @@ -172,6 +179,13 @@ Eqn("X &= U \\\\lambda V", Eqn_newline(), 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,