51
51
# ' for code testing purposes can be set globally
52
52
# ' via \code{\link{options}} (e.g., \code{options('previewEqn' = FALSE)}).
53
53
# ' 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']"}
54
60
# ' @returns NULL
55
61
# ' @importFrom knitr is_html_output
56
62
# ' @importFrom rstudioapi viewer
77
83
# ' # Quarto output
78
84
# ' Eqn('e=mc^2', label = 'eq-einstein', quarto = TRUE)
79
85
# '
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
+ # '
80
98
# ' # Multiple expressions
81
99
# ' Eqn("e=mc^2",
82
100
# ' Eqn_newline(),
@@ -121,7 +139,9 @@ Eqn <- function(...,
121
139
preview = getOption(' previewEqn' ),
122
140
html_output = knitr :: is_html_output(),
123
141
quarto = getOption(' quartoEqn' ),
124
- mat_args = list ()) {
142
+ mat_args = list (),
143
+ preview.pdf = FALSE ,
144
+ preview.header = " " ) {
125
145
126
146
# for connection safety
127
147
sink.reset <- function (){
@@ -142,13 +162,14 @@ Eqn <- function(...,
142
162
# everything except the kitchen ...
143
163
sink(tmpfile )
144
164
on.exit(file.remove(tmpfile ), add = TRUE )
145
- cat(
165
+ if (preview.header != ' ' )
166
+ preview.header <- paste0(' \n ' , preview.header , collapse = ' ' )
167
+ cat(sprintf(
146
168
"
147
169
---
148
- title: ' '
170
+ title: ' '%s
149
171
---
150
- " )
151
-
172
+ " , preview.header ))
152
173
}
153
174
stopifnot(is.logical(quarto ))
154
175
number <- ! is.null(label )
@@ -187,9 +208,16 @@ title: ' '
187
208
}
188
209
if (preview ){
189
210
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 ' )
193
221
cat(inp )
194
222
}
195
223
invisible (NULL )
0 commit comments