Skip to content

Commit 928468e

Browse files
authored
fix #948: make the numbering scheme of figures and tables independent of number_sections (#1226)
1 parent a3de608 commit 928468e

10 files changed

+64
-17
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- This version has a brand new template project to start a HTML book in one of available format. Template project can be created using the RStudio IDE _New Project..._, or using one of the two new functions, `create_gitbook()` and `create_bs4_book()`, to easily create the template you want to start with from within the R Console (#225, #1123, #1201).
44

5+
- Added an argument `global_numbering` to most output format functions in this package to control the figure/table numbering scheme (thanks, @elfunesto #948, @Kodiologist #1057). If `TRUE`, number figures and tables globally throughout a document (e.g., Figure 1, Figure 2, ...). If `FALSE`, number them sequentially within sections (e.g., Figure 1.1, Figure 1.2, ..., Figure 5.1, Figure 5.2, ...). Previously, this numbering scheme was hard-coded internally according to the `number_sections` argument (`global_numbering = !number_sections`). Now the two arguments have become independent, e.g., you can use `global_numbering = TRUE` with `number_sections = TRUE`.
6+
57
- `bs4_book(splib_bib = TRUE)` can now be specified to have the same effect as in `gitbook()`. References will be shown at the end of each chapter and not only at the end of the book. This is useful with `bs4_book()` when a citation style not supporting footnotes is used because in that case, references are not shown inline in popups (thanks, @shirdekel, #1185).
68

79
- For HTML book formats, a default `404.html` page will now be created if none exists already. This page can be customized by adding a `_404.md` or `_404.Rmd` file which will be rendered to HTML and inserted in the book. Most web serving platforms (e.g. Netlify, GH Pages, etc.) will use this file named `404.html` in the root as a custom error page. Otherwise, like browsers do, a default 404 page is shown. For context, a 404 error indicates that the file can’t be found, and it happens when a browser can’t find a requested web page. This could happen with your online book if you shared a link to a section but change the name of this section leading to a change in url (#1035).

R/bs4_book.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ bs4_book_build <- function(output = "bookdown.html",
9191
output2 <- split_chapters(
9292
output = output,
9393
build = function(...) bs4_book_page(..., rmd_index = rmd_index),
94-
number_sections = TRUE,
94+
global_numbering = FALSE,
9595
split_by = "chapter",
9696
split_bib = split_bib
9797
)

R/ebook.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#'
33
#' Convert a book to the EPUB format, which is is an e-book format supported by
44
#' many readers, such as Amazon Kindle Fire and iBooks on Apple devices.
5+
#' @inheritParams html_document2
56
#' @param fig_width,fig_height,dev,fig_caption Figure options (width, height,
67
#' the graphical device, and whether to render figure captions).
78
#' @param number_sections Whether to number sections.
@@ -32,7 +33,8 @@ epub_book = function(
3233
fig_width = 5, fig_height = 4, dev = 'png', fig_caption = TRUE,
3334
number_sections = TRUE, toc = FALSE, toc_depth = 3, stylesheet = NULL,
3435
cover_image = NULL, metadata = NULL, chapter_level = 1,
35-
epub_version = c('epub3', 'epub', 'epub2'), md_extensions = NULL, pandoc_args = NULL,
36+
epub_version = c('epub3', 'epub', 'epub2'), md_extensions = NULL,
37+
global_numbering = !number_sections, pandoc_args = NULL,
3638
template = 'default'
3739
) {
3840
epub_version = match.arg(epub_version)
@@ -57,7 +59,7 @@ epub_book = function(
5759
knitr = rmarkdown::knitr_options_html(fig_width, fig_height, NULL, FALSE, dev),
5860
pandoc = rmarkdown::pandoc_options(epub_version, from, args, ext = '.epub'),
5961
pre_processor = function(metadata, input_file, runtime, knit_meta, files_dir, output_dir) {
60-
process_markdown(input_file, from, args, !number_sections)
62+
process_markdown(input_file, from, args, global_numbering)
6163
NULL
6264
},
6365
post_processor = function(metadata, input, output, clean, verbose) {

R/gitbook.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#' The GitBook output format
22
#'
3-
#' @description
43
#' This output format function ported a style provided by GitBook
54
#' (\url{https://www.gitbook.com}) for R Markdown. To read more about this format, see:
65
#' \url{https://bookdown.org/yihui/bookdown/html.html#gitbook-style}
@@ -24,7 +23,8 @@
2423
#' @export
2524
gitbook = function(
2625
fig_caption = TRUE, number_sections = TRUE, self_contained = FALSE,
27-
anchor_sections = TRUE, lib_dir = 'libs', pandoc_args = NULL, ..., template = 'default',
26+
anchor_sections = TRUE, lib_dir = 'libs', global_numbering = !number_sections,
27+
pandoc_args = NULL, ..., template = 'default',
2828
split_by = c('chapter', 'chapter+number', 'section', 'section+number', 'rmd', 'none'),
2929
split_bib = TRUE, config = list(), table_css = TRUE
3030
) {
@@ -57,7 +57,7 @@ gitbook = function(
5757

5858
move_files_html(output, lib_dir)
5959
output2 = split_chapters(
60-
output, gitbook_page, number_sections, split_by, split_bib, gb_config, split_by
60+
output, gitbook_page, global_numbering, split_by, split_bib, gb_config, split_by
6161
)
6262
if (file.exists(output) && !same_path(output, output2)) file.remove(output)
6363
move_files_html(output2, lib_dir)

R/html.R

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#' Functions \code{html_book()} and \code{tufte_html_book()} are simple wrapper
66
#' functions of \code{html_chapter()} using a specific base output format.
77
#' @inheritParams pdf_book
8+
#' @inheritParams html_document2
89
#' @param toc,number_sections,fig_caption,lib_dir,template,pandoc_args See
910
#' \code{rmarkdown::\link{html_document}},
1011
#' \code{tufte::\link[tufte:tufte_handout]{tufte_html}}, or the documentation
@@ -50,7 +51,8 @@
5051
#' @export
5152
html_chapters = function(
5253
toc = TRUE, number_sections = TRUE, fig_caption = TRUE, lib_dir = 'libs',
53-
template = bookdown_file('templates/default.html'), pandoc_args = NULL, ...,
54+
template = bookdown_file('templates/default.html'),
55+
global_numbering = !number_sections, pandoc_args = NULL, ...,
5456
base_format = rmarkdown::html_document, split_bib = TRUE, page_builder = build_chapter,
5557
split_by = c('section+number', 'section', 'chapter+number', 'chapter', 'rmd', 'none')
5658
) {
@@ -64,7 +66,7 @@ html_chapters = function(
6466
config$post_processor = function(metadata, input, output, clean, verbose) {
6567
if (is.function(post)) output = post(metadata, input, output, clean, verbose)
6668
move_files_html(output, lib_dir)
67-
output2 = split_chapters(output, page_builder, number_sections, split_by, split_bib)
69+
output2 = split_chapters(output, page_builder, global_numbering, split_by, split_bib)
6870
if (file.exists(output) && !same_path(output, output2)) file.remove(output)
6971
move_files_html(output2, lib_dir)
7072
output2
@@ -111,6 +113,12 @@ tufte_html_book = function(...) {
111113
#' (the i-th figure/table); if \code{FALSE}, figures/tables will be numbered
112114
#' sequentially in the document from 1, 2, ..., and you cannot cross-reference
113115
#' section headers in this case.
116+
#' @param global_numbering If \code{TRUE}, number figures and tables globally
117+
#' throughout a document (e.g., Figure 1, Figure 2, ...). If \code{FALSE},
118+
#' number them sequentially within sections (e.g., Figure 1.1, Figure 1.2,
119+
#' ..., Figure 5.1, Figure 5.2, ...). Note that \code{global_numbering =
120+
#' FALSE} will not work with \code{number_sections = FALSE} because sections
121+
#' are not numbered.
114122
#' @inheritParams pdf_book
115123
#' @return An R Markdown output format object to be passed to
116124
#' \code{rmarkdown::\link{render}()}.
@@ -123,7 +131,8 @@ tufte_html_book = function(...) {
123131
#' @references \url{https://bookdown.org/yihui/bookdown/}
124132
#' @export
125133
html_document2 = function(
126-
..., number_sections = TRUE, pandoc_args = NULL, base_format = rmarkdown::html_document
134+
..., number_sections = TRUE, global_numbering = !number_sections,
135+
pandoc_args = NULL, base_format = rmarkdown::html_document
127136
) {
128137
config = get_base_format(base_format, list(
129138
..., number_sections = number_sections, pandoc_args = pandoc_args2(pandoc_args)
@@ -135,7 +144,7 @@ html_document2 = function(
135144
x = clean_html_tags(x)
136145
x = restore_appendix_html(x, remove = FALSE)
137146
x = restore_part_html(x, remove = FALSE)
138-
x = resolve_refs_html(x, global = !number_sections)
147+
x = resolve_refs_html(x, global_numbering)
139148
write_utf8(x, output)
140149
output
141150
}
@@ -240,7 +249,9 @@ build_chapter = function(
240249

241250
r_chap_pattern = '^<!--chapter:end:(.+)-->$'
242251

243-
split_chapters = function(output, build = build_chapter, number_sections, split_by, split_bib, ...) {
252+
split_chapters = function(
253+
output, build = build_chapter, global_numbering, split_by, split_bib, ...
254+
) {
244255

245256
use_rmd_names = split_by == 'rmd'
246257
split_level = switch(
@@ -311,7 +322,7 @@ split_chapters = function(output, build = build_chapter, number_sections, split_
311322

312323
# no (or not enough) tokens found in the template
313324
if (any(c(i1, i2, i3, i4, i5, i6) == 0)) {
314-
x = resolve_refs_html(x, !number_sections)
325+
x = resolve_refs_html(x, global_numbering)
315326
x = add_chapter_prefix(x)
316327
write_utf8(x, output)
317328
return(output)
@@ -333,7 +344,7 @@ split_chapters = function(output, build = build_chapter, number_sections, split_
333344
' first-level heading(s). Did you forget first-level headings in certain Rmd files?'
334345
)
335346

336-
html_body = resolve_refs_html(html_body, !number_sections)
347+
html_body = resolve_refs_html(html_body, global_numbering)
337348

338349
# do not split the HTML file
339350
if (split_level == 0) {

R/word.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#' @export
33
markdown_document2 = function(
44
number_sections = TRUE, fig_caption = TRUE, md_extensions = NULL,
5-
pandoc_args = NULL, ..., base_format = rmarkdown::md_document
5+
global_numbering = !number_sections, pandoc_args = NULL, ...,
6+
base_format = rmarkdown::md_document
67
) {
78
from = rmarkdown::from_rmarkdown(fig_caption, md_extensions)
89

@@ -12,9 +13,7 @@ markdown_document2 = function(
1213
))
1314
pre = config$pre_processor
1415
config$pre_processor = function(metadata, input_file, ...) {
15-
# Pandoc does not support numbered sections for Word, so figures/tables have
16-
# to be numbered globally from 1 to n
17-
process_markdown(input_file, from, pandoc_args, !number_sections)
16+
process_markdown(input_file, from, pandoc_args, global_numbering)
1817
if (is.function(pre)) pre(metadata, input_file, ...)
1918
}
2019
post = config$post_processor

man/epub_book.Rd

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/gitbook.Rd

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/html_chapters.Rd

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/html_document2.Rd

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)