fix #2032: keep figure captions when a chunk produces multiple figures - #2454
Merged
Conversation
When a chunk produced multiple figures with captions for Markdown-based output (HTML or PDF via Pandoc), the plot hook emitted the images in a single paragraph, e.g. ` `. Pandoc treats several images in one paragraph as inline images and drops the captions (no figure environment or <figcaption>). Insert a blank line after each captioned figure except the last so that Pandoc emits a separate figure (with caption) per image. This is done in the two bare-image producers, hook_plot_md_base() and hook_plot_md_pandoc(); the LaTeX hook already wraps each plot in its own figure environment and is unaffected. Closes #1524, supersedes #1760 (thanks, @atusy). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a single chunk produces multiple captioned figures for Markdown-based output (HTML, or PDF via Pandoc), the captions are silently dropped.
Minimal example:
The plot hook is called once per figure and the bare images end up in a single paragraph in the intermediate
.md:Pandoc treats several images in one paragraph as inline images, so it emits no figure environment /
<figcaption>and the captions disappear. Reproduced for bothpdf_documentandhtml_document.Fix
Insert a blank line after each captioned figure except the last, so Pandoc renders a separate figure (with caption) per image:
This is applied in the two bare-image producers,
hook_plot_md_base()andhook_plot_md_pandoc(), via a small helpersep_captioned_fig(). The condition keys on caption presence and figure position (fig.cur < fig.num) — figures without a caption are left inline, and single-figure chunks and the last figure of a chunk are unchanged. The LaTeX hook (hook_plot_tex()) already wraps each plot in its ownfigureenvironment and is unaffected.Relation to prior work
Supersedes the stale #1760 (thanks @atusy for the original diagnosis and PR). That PR gated on
echo == FALSE || fig.show == 'hold', which missed cases likepurrr::walk(list(g, g), print); keying on the caption instead handles those too. Also closes #1524. And also closes #1760.Verification
pdf_documentandhtml_documentnow keep per-figure captions (HTML went from 0 to N<figcaption>s).test-hooks-md.Rcovering non-last / last / single / no-caption cases.testitsuite passes.🤖 Generated with Claude Code