Skip to content

Commit 2c97ef3

Browse files
committed
when a numbered element (figure/table/equation) appears before Chapter 1 in the document, we number it with 0.x (otherwise here j will be character(0), and the following num[k} = cntr$inc() will error)
example: https://cran.rstudio.com/web/packages/SimCorrMix/vignettes/corr_mixture.html
1 parent 02e5afd commit 2c97ef3

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: bookdown
22
Type: Package
33
Title: Authoring Books and Technical Documents with R Markdown
4-
Version: 0.9.18
4+
Version: 0.9.19
55
Authors@R: c(
66
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
77
person("JJ", "Allaire", role = "ctb"),

R/html.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,12 @@ reg_label_types = paste(reg_label_types, 'ex', sep = '|')
550550
parse_fig_labels = function(content, global = FALSE) {
551551
lines = grep(reg_chap, content)
552552
chaps = gsub(reg_chap, '\\2', content[lines]) # chapter numbers
553-
if (length(chaps) == 0) global = TRUE # no chapter titles or no numbered chapters
553+
if (length(chaps) == 0) {
554+
global = TRUE # no chapter titles or no numbered chapters
555+
} else {
556+
chaps = c('0', chaps) # use Chapter 0 in case of any figure before Chapter 1
557+
lines = c(0, lines)
558+
}
554559
arry = character() # an array of the form c(label = number, ...)
555560
if (global) chaps = '0' # Chapter 0 (could be an arbitrary number)
556561

@@ -567,6 +572,7 @@ parse_fig_labels = function(content, global = FALSE) {
567572
if (length(lab <- labs[[i]]) == 0) next
568573

569574
j = if (global) chaps else tail(chaps[lines <= i], 1)
575+
if (length(j) == 0) j = chaps[1] # use Chapter 0
570576
lab = gsub('^\\(#|\\)$', '', lab)
571577
type = gsub('^([^:]+):.*', '\\1', lab)
572578
# there could be multiple labels on the same line, but their types must be

0 commit comments

Comments
 (0)