Skip to content

Commit 48087af

Browse files
committed
fix a problem discovered at rstudio/rticles#288 (comment): when a bibtex error occurs, should not stop immediately but try to parse and install missing packages first
1 parent 9b48862 commit 48087af

2 files changed

Lines changed: 24 additions & 22 deletions

File tree

‎DESCRIPTION‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ BugReports: https://github.com/yihui/tinytex/issues
2222
Encoding: UTF-8
2323
LazyData: true
2424
RoxygenNote: 7.1.1
25+
Remotes: yihui/xfun

‎R/latex.R‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -228,35 +228,36 @@ latexmk_emu = function(
228228
# generate bibliography
229229
bib_engine = match.arg(bib_engine)
230230
install_cmd(bib_engine)
231+
pkgs_last = character()
231232
aux = aux_files[if ((biber <- bib_engine == 'biber')) 'bcf' else 'aux']
232233
if (file.exists(aux)) {
233234
if (biber || require_bibtex(aux)) {
234235
blg = aux_files['blg'] # bibliography log file
235236
build_bib = function() system2_quiet(bib_engine, shQuote(aux), error = {
236-
stop("Failed to build the bibliography via ", bib_engine, call. = FALSE)
237+
check_blg = function() {
238+
if (!file.exists(blg)) return(TRUE)
239+
x = readLines(blg)
240+
if (length(grep('error message', x)) == 0) return(TRUE)
241+
warn = function() {
242+
warning(
243+
bib_engine, ' seems to have failed:\n\n', paste(x, collapse = '\n'),
244+
call. = FALSE
245+
)
246+
TRUE
247+
}
248+
if (!tlmgr_available() || !install_packages) return(warn())
249+
# install the possibly missing .bst package and rebuild bib
250+
r = '.* open style file ([^ ]+).*'
251+
pkgs = parse_packages(files = xfun::grep_sub(r, '\\1', x), quiet = !verbose)
252+
if (length(pkgs) == 0 || identical(pkgs, pkgs_last)) return(warn())
253+
pkgs_last <<- pkgs
254+
tlmgr_install(pkgs); build_bib()
255+
FALSE
256+
}
257+
if (check_blg())
258+
stop("Failed to build the bibliography via ", bib_engine, call. = FALSE)
237259
})
238260
build_bib()
239-
check_blg = function() {
240-
if (!file.exists(blg)) return(TRUE)
241-
x = readLines(blg)
242-
if (length(grep('error message', x)) == 0) return(TRUE)
243-
warn = function() {
244-
warning(
245-
bib_engine, ' seems to have failed:\n\n', paste(x, collapse = '\n'),
246-
call. = FALSE
247-
)
248-
TRUE
249-
}
250-
if (!tlmgr_available() || !install_packages) return(warn())
251-
# install the possibly missing .bst package and rebuild bib
252-
r = '.* open style file ([^ ]+).*'
253-
pkgs = parse_packages(files = gsub(r, '\\1', grep(r, x, value = TRUE)))
254-
if (length(pkgs) == 0) return(warn())
255-
tlmgr_install(pkgs); build_bib()
256-
FALSE
257-
}
258-
# check .blg at most 3 times for missing packages
259-
for (i in 1:3) if (check_blg()) break
260261
}
261262
}
262263
for (i in seq_len(max_times)) {

0 commit comments

Comments
 (0)