Skip to content

html_document: restore code padding on pandoc 3, side-by-side images, caption nesting - #128

Merged
seandavi merged 5 commits into
develfrom
fix/html-css-103-52
Nov 17, 2026
Merged

seandavi merged 5 commits into
develfrom
fix/html-css-103-52

Conversation

@seandavi

Copy link
Copy Markdown
Contributor

Three fixes to BiocStyle::html_document output.

1. Code blocks lose their padding on pandoc 3 (#103)

Broken: in self-contained HTML (the default), code and output blocks had no vertical padding, so the code sat flush against the grey box.

Root cause: modify_css() removes rmarkdown's injected pre code { padding: 0; } rule with an exact match on its two-space indentation. Pandoc >= 3 strips the indentation when it embeds the CSS, so the match failed and the rule overrode BiocStyle's pre code { padding: 10px 10px 10px 0 }.

Fix: match the rule with any whitespace (pre code \{\s*padding: 0;\s*\}, perl). The indented form (pandoc < 3, or self_contained = FALSE) still matches. Bootstrap's minified pre code{padding:0;…} doesn't match, and neither do BiocStyle's own pre code rules.

2. Images with a reduced width are invisible or stacked (#52, #54)

Broken: knitr::include_graphics(rep(...)) or plots with out.width = "30%", fig.show = "hold" and a fig.cap showed only the caption, with zero-width images. Without a caption, or with raw <img width="30%"> tags, the images stacked vertically. A single captioned figure with out.width = "50%" rendered tiny and ignored fig.align (#54).

Root cause: bioconductor.css gives every img display: block and 130px + 150px of horizontal padding under box-sizing: border-box, to indent full-width figures into the text column. A 30% wide image (about 200px) is smaller than its 280px padding, so its content box collapses to 0. The block display also prevents images from sitting side by side.

Fix: images whose width attribute isn't "100%" get display: inline-block; padding: 0. Their containing .figure div or <p> gets the indent instead (through :has()), so the percentages refer to the figure column, several images sit side by side, and knitr's fig.align centers them. The narrow-screen media query removes that container indent, just as it does for full-width figures. Default, fig.small and fig.wide figures all carry width="100%", so these rules never match them.

3. caption_titles() produces invalid tag nesting

Broken: a one-line table caption without a sentence break (e.g. a knitr::kable caption) became <caption>… <span class="caption-title">Title</caption></span><br>.

Root cause: the lazy title group, followed by the end-of-line alternative, consumed the closing </caption>.

Fix: for tables, end the title before a closing </caption> on the same line. The output is now <span class="caption-title">Title</span><br></caption>. A final period is dropped, as it already was for figures. Figure captions are unchanged.

Verification

R 4.6.0, rmarkdown 2.31, knitr 1.51, bookdown 0.47, Chromium 143. Pandoc 3.7.0.2 and 3.10.

  • New unit tests: inst/unitTests/test_html_document.R covers modify_css() (indented and unindented rules, other pre code rules kept) and caption_titles() (nesting, final period, inline markup, multi-line captions, figures unchanged). The regression tests fail with the old modify_css()/caption_titles() and pass with the new ones.
  • New golden test: test_html_golden_code_block_padding in test_render_golden.R checks that the rule is gone from both self-contained and self_contained = FALSE output. It fails with the old modify_css() on pandoc 3.7 and 3.10.
  • Updated test: test_default_figure_options counted lines containing width="100%", which now also match the embedded CSS. It now counts <img> tags.
  • Full suite: BiocGenerics:::testPackage("BiocStyle") gives 29 tests, 0 errors, 0 failures on both pandoc 3.7.0.2 and 3.10.
  • Check: R CMD build + _R_CHECK_FORCE_SUGGESTS_=false R CMD check --no-manual gives Status: OK (no WARNING/NOTE).
  • Visual check: I rendered a test document with a default figure, fig.small, fig.wide, a long caption, an uncaptioned figure, side-by-side images (captioned and uncaptioned include_graphics, two plots with fig.show = "hold", raw <img> tags) and 50%-wide centered figures. I took headless Chromium screenshots before and after at 1400px and 900px, and also compared each img/p.caption/pre element's computed box (left, width, height, padding) between the two renders:
    • Default, fig.small, fig.wide, long-caption and uncaptioned figures (and their captions) have identical boxes before and after, at both widths.
    • Code blocks are 20px taller (10px top and bottom padding restored).
    • The side-by-side images are now visible and in one row, starting at the figure column (the same left edge as full-width figures and captions). The 50% figures are centered with fig.align = "center".
    • Pandoc 3.10 output gives exactly the same boxes as 3.7.

Notes

  • :has() needs Chrome/Edge 105+, Safari 15.4+ or Firefox 121+. In older browsers the container indent is skipped, and reduced-width images still show, side by side, starting at the container's left edge.
  • A single image with an explicit width now gets exactly that width. Previously its padding was subtracted: for example, fig.small = TRUE, out.width = "500px" was about 25px wide and is now 500px.
  • A paragraph that mixes text with an inline <img width="…"> (other than 100%) is now indented to the figure column. Before, such an image was a block element anyway.

Fixes #103
Fixes #52
Fixes #54

seandavi and others added 5 commits November 17, 2026 09:39
modify_css() removed rmarkdown's injected 'pre code { padding: 0; }'
rule with an exact match on its two-space indentation. Pandoc >= 3
strips the indentation when it embeds CSS in self-contained output, so
the match failed and the rule overrode the 10px code block padding in
bioconductor.css. Match the rule with any whitespace instead; output
with the indented form (pandoc < 3, self_contained = FALSE) still
matches.

Add unit tests for modify_css() and a golden render test for both
self-contained and non-self-contained output.

Co-Authored-By: Claude <noreply@anthropic.com>
caption_titles() let the lazy title group run to the end of the line
when a table caption had no sentence break, so a one-line caption such
as '<caption>(#tab:x) Title</caption>' became
'<span class="caption-title">Title</caption></span><br>', which is
invalid nesting. For tables, end the title before a closing </caption>
on the same line (dropping a final period, as for figures). Figure
captions are unchanged.

Co-Authored-By: Claude <noreply@anthropic.com>
bioconductor.css gives every <img> display: block and 280px of
horizontal padding under box-sizing: border-box, to indent full-width
figures into the text column. An image with an explicit smaller width
(out.width = "30%", fig.show = "hold", or raw <img width="30%"> tags)
therefore stacked vertically, and in captioned figures its content box
collapsed to nothing: the images were invisible (#52). A single
captioned figure with out.width = "50%" rendered tiny and ignored
fig.align (#54).

For images whose width attribute is not "100%", drop the padding and
make them inline-block, and indent the containing figure div or
paragraph instead (via :has()), so the percentages refer to the figure
column, several images sit side by side, and fig.align centers them.
The narrow-screen media query removes the container indent as it does
for full-width figures. Default, fig.small and fig.wide figures all
have width="100%" and are not affected.

test_default_figure_options counted lines matching width="100%", which
now also match the embedded CSS; count <img> tags instead.

Co-Authored-By: Claude <noreply@anthropic.com>
@seandavi
seandavi merged commit 52af6c9 into devel Nov 17, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BiocStyle looks ugly with the latest pandoc change figure alignment Side-by-side images with knitr::include_graphics

1 participant