Skip to content

Commit aa69eb8

Browse files
committed
allow PDF previews for special LaTeX cases that require extra packages
1 parent 149cc21 commit aa69eb8

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

R/Eqn.R

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
#' for code testing purposes can be set globally
5252
#' via \code{\link{options}} (e.g., \code{options('previewEqn' = FALSE)}).
5353
#' Disabled whenever \code{quarto} or \code{html_output} are \code{TRUE}
54+
#' @param preview.pdf logical; build a PDF of the preview equation? Generally
55+
#' not require unless additional LaTeX packages are required that are not supported
56+
#' by MathJax
57+
#' @param preview.header character vector to add additional information to the
58+
#' equation preview. Most useful for adding packages when \code{preview.pdf = TRUE}.
59+
#' For example, \code{preview.header = "pdf_document:\n \t extra_dependencies: ['amsmath', 'bm']"}
5460
#' @returns NULL
5561
#' @importFrom knitr is_html_output
5662
#' @importFrom rstudioapi viewer
@@ -77,6 +83,18 @@
7783
#' # Quarto output
7884
#' Eqn('e=mc^2', label = 'eq-einstein', quarto = TRUE)
7985
#'
86+
#' \dontrun{
87+
#' # The following requires LaTeX compilers to be pre-installed
88+
#'
89+
#' # View PDF instead of HTML
90+
#' Eqn('e=mc^2', preview.pdf=TRUE)
91+
#'
92+
#' # Add extra LaTeX dependencies for PDF build
93+
#' Eqn('\\bm{e}=mc^2', preview.pdf=TRUE,
94+
#' preview.header="pdf_document:
95+
#' extra_dependencies: ['amsmath', 'bm']")
96+
#' }
97+
#'
8098
#' # Multiple expressions
8199
#' Eqn("e=mc^2",
82100
#' Eqn_newline(),
@@ -121,7 +139,9 @@ Eqn <- function(...,
121139
preview = getOption('previewEqn'),
122140
html_output = knitr::is_html_output(),
123141
quarto = getOption('quartoEqn'),
124-
mat_args = list()) {
142+
mat_args = list(),
143+
preview.pdf = FALSE,
144+
preview.header="") {
125145

126146
# for connection safety
127147
sink.reset <- function(){
@@ -142,13 +162,14 @@ Eqn <- function(...,
142162
# everything except the kitchen ...
143163
sink(tmpfile)
144164
on.exit(file.remove(tmpfile), add = TRUE)
145-
cat(
165+
if(preview.header != '')
166+
preview.header <- paste0('\n', preview.header, collapse='')
167+
cat(sprintf(
146168
"
147169
---
148-
title: '&nbsp;'
170+
title: '&nbsp;'%s
149171
---
150-
")
151-
172+
", preview.header))
152173
}
153174
stopifnot(is.logical(quarto))
154175
number <- !is.null(label)
@@ -187,9 +208,16 @@ title: '&nbsp;'
187208
}
188209
if(preview){
189210
sink()
190-
rmarkdown::render(tmpfile, 'html_document', clean = TRUE, quiet = TRUE)
191-
rstudioapi::viewer(paste0(tmpfile, '.html'))
192-
inp <- paste0(readLines(tmpfile)[-c(1:4)], collapse='\n')
211+
if(preview.pdf){
212+
rmarkdown::render(tmpfile, 'pdf_document', clean = TRUE, quiet = TRUE)
213+
rstudioapi::viewer(paste0(tmpfile, '.pdf'))
214+
} else {
215+
rmarkdown::render(tmpfile, 'html_document', clean = TRUE, quiet = TRUE)
216+
rstudioapi::viewer(paste0(tmpfile, '.html'))
217+
}
218+
lines <- readLines(tmpfile)
219+
dashloc <- which(lines == '---')
220+
inp <- paste0(lines[-c(1, dashloc[1]:dashloc[2])], collapse='\n')
193221
cat(inp)
194222
}
195223
invisible(NULL)

man/Eqn.Rd

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)