Collect footnotes inside figure and table captions

This commit is contained in:
gloamxun committed 2026-10-19 18:39:16 +08:00
1 parent 6414770de7
commit 0c615c8e82
5 files changed
+68 -9

No files matched your search

+3 -2
View File
@@ -344,8 +344,9 @@ separator rule at the bottom of the page. Footnote lines are painted in body
coordinates so they stay below the text block rather than overlapping it. coordinates so they stay below the text block rather than overlapping it.
`\thanks{...}` is a footnote in the title or author block and is painted at `\thanks{...}` is a footnote in the title or author block and is painted at
the bottom of the first page. Its measured height is reserved before body the bottom of the first page. Its measured height is reserved before body
placement, so the last body line stays above the footnote area. Footnotes inside captions, floats, or arbitrary placement, so the last body line stays above the footnote area. Footnotes
boxes are not collected yet. inside figure and table captions are collected and follow the caption's page;
footnotes inside floats or arbitrary boxes are not collected yet.
Basic `\input{file}` imports are read relative to the main `.tex` file. Imported Basic `\input{file}` imports are read relative to the main `.tex` file. Imported
files participate in source mapping and frontend cache invalidation. Local style files participate in source mapping and frontend cache invalidation. Local style
+2 -1
View File
@@ -20,7 +20,8 @@ author-year citations and plainnat bibliography formatting with duplicate-year
(`build_positioned_pages` is the only page builder and no separate break (`build_positioned_pages` is the only page builder and no separate break
planner remains); conditional Base-14 font embedding and natural-width centered planner remains); conditional Base-14 font embedding and natural-width centered
table layout; `\multicolumn` column spans with alignment and partial table layout; `\multicolumn` column spans with alignment and partial
`\cmidrule(r){a-b}`/`\cline{a-b}` rules. `\cmidrule(r){a-b}`/`\cline{a-b}` rules; footnote capture in figure/table
captions with the insertion area reserved on the caption page.
## 1. Float tuning and table queue ## 1. Float tuning and table queue
+4 -3
View File
@@ -96,8 +96,9 @@ See `docs/ROADMAP.md` for concrete implementation plans.
queues are not implemented and there is no queues are not implemented and there is no
`\textfraction`/`\topfraction` tuning. Pagination is performed by the `\textfraction`/`\topfraction` tuning. Pagination is performed by the
positioned renderer; the separate page-break planner was removed. positioned renderer; the separate page-break planner was removed.
- Notes: footnotes inside captions, floats, or arbitrary boxes are not - Notes: footnotes inside figure/table captions are collected and painted on
collected; the insertion area follows the page where the paragraph ends the caption's page; footnotes inside floats or arbitrary boxes are not
collected yet. The insertion area follows the page where the paragraph ends
rather than the exact reference line. Heading keep-with-next is approximate, rather than the exact reference line. Heading keep-with-next is approximate,
so a section heading can be left at the bottom of a page with its body on so a section heading can be left at the bottom of a page with its body on
the next page. the next page.
@@ -115,7 +116,7 @@ See `docs/ROADMAP.md` for concrete implementation plans.
- ASCII-only documents use non-embedded Base-14 Times/Courier (arXiv PDF is - ASCII-only documents use non-embedded Base-14 Times/Courier (arXiv PDF is
about 0.7 MB); documents with accented/non-WinAnsi text embed Liberation about 0.7 MB); documents with accented/non-WinAnsi text embed Liberation
faces. Tables use natural column widths and centered rules. faces. Tables use natural column widths and centered rules.
- `cargo fmt --check` passes; `cargo test --offline` (50 tests: 46 compiler - `cargo fmt --check` passes; `cargo test --offline` (51 tests: 47 compiler
integration tests + 4 server tests) covers the three examples, the arXiv integration tests + 4 server tests) covers the three examples, the arXiv
fixture, figure/table floats (top and bottom), newpage, page styles and `\thispagestyle`, SVG/PDF output, fixture, figure/table floats (top and bottom), newpage, page styles and `\thispagestyle`, SVG/PDF output,
project API, static frontend assets, path traversal, the legacy project API, static frontend assets, path traversal, the legacy
+33 -3
View File
@@ -1114,10 +1114,18 @@ fn footnote_marker(number: u32) -> String {
} }
fn footnote_numbers_for_block(model: &DocumentModel, block_index: usize) -> Vec<u32> { fn footnote_numbers_for_block(model: &DocumentModel, block_index: usize) -> Vec<u32> {
let Some(Block::Paragraph(runs)) = model.blocks.get(block_index) else { let Some(block) = model.blocks.get(block_index) else {
return Vec::new(); return Vec::new();
}; };
let mut numbers = Vec::new(); let mut numbers = Vec::new();
let runs = match block {
Block::Paragraph(runs)
| Block::Heading { runs, .. }
| Block::FramedBox { runs, .. }
| Block::Centered { runs } => runs.as_slice(),
Block::Image { caption, .. } => caption.as_deref().unwrap_or(&[]),
_ => &[],
};
for run in runs { for run in runs {
if let Some(number) = run.footnote_number if let Some(number) = run.footnote_number
&& !numbers.contains(&number) && !numbers.contains(&number)
@@ -2822,6 +2830,8 @@ pub(super) fn build_positioned_pages(
return None; return None;
} }
let figure_height = figure.height; let figure_height = figure.height;
let caption_has_footnotes =
!footnote_numbers_for_block(model, block_index).is_empty();
let anchor = PageAnchor { let anchor = PageAnchor {
block: block_index, block: block_index,
line: 0, line: 0,
@@ -2830,13 +2840,16 @@ pub(super) fn build_positioned_pages(
let bottom_float = placement.contains('b') let bottom_float = placement.contains('b')
&& !placement.contains('h') && !placement.contains('h')
&& !placement.contains('t') && !placement.contains('t')
&& !caption_has_footnotes
&& cursor > 0 && cursor > 0
&& cursor && cursor
.saturating_add(block_gap) .saturating_add(block_gap)
.saturating_add(figure_height) .saturating_add(figure_height)
<= page_goal; <= page_goal;
let deferred_top = let deferred_top = placement.contains('t')
placement.contains('t') && !placement.contains('h') && !placement.contains('p'); && !placement.contains('h')
&& !placement.contains('p')
&& !caption_has_footnotes;
let defer_here = let defer_here =
deferred_top && !pages.last().is_none_or(|page| page.items.is_empty()); deferred_top && !pages.last().is_none_or(|page| page.items.is_empty());
if defer_here { if defer_here {
@@ -2882,6 +2895,23 @@ pub(super) fn build_positioned_pages(
}); });
cursor = cursor.saturating_add(figure_height); cursor = cursor.saturating_add(figure_height);
} }
let caption_reserve = footnote_reserve(
model,
block_index,
content_width_mm,
metrics,
font_cache,
cjk_family,
mono_family,
sans_family,
math_metrics,
math_layout,
cache,
);
if caption_reserve > 0 && !defer_here {
cursor = cursor.saturating_add(caption_reserve);
block_end_pages.insert(block_index, pages.len().saturating_sub(1));
}
if !defer_here { if !defer_here {
pending_gap = paragraph_gap + (normal_line_height as f64 * 0.15).round() as i64; pending_gap = paragraph_gap + (normal_line_height as f64 * 0.15).round() as i64;
has_previous_block = true; has_previous_block = true;
+26
View File
@@ -1431,3 +1431,29 @@ See \href{https://example.com}{the example site} for details.
assert!(svg.contains("fill=\"#008000\""), "{svg}"); assert!(svg.contains("fill=\"#008000\""), "{svg}");
assert!(svg.contains(">example</text>"), "{svg}"); assert!(svg.contains(">example</text>"), "{svg}");
} }
#[test]
fn caption_footnotes_follow_the_figure_page() {
let source = r"\documentclass{article}
\usepackage{graphicx}
\begin{document}
Before.\newpage
\begin{figure}[h]
\includegraphics[width=0.5\textwidth]{examples/plot.png}
\caption{A caption with a footnote\footnote{Caption footnote text.}}
\end{figure}
After text.
\end{document}";
let value = fresh_preview(source, "caption-footnote.tex");
assert_eq!(value["status"], "ok", "{value}");
let pages = pages(&value);
assert!(pages.len() >= 2, "expected a new page before the figure");
assert!(
!page_svg(&value, 0).contains(">Caption</text>"),
"caption footnote leaked onto the first page"
);
assert!(
page_svg(&value, 1).contains(">Caption</text>"),
"caption footnote should be painted on the figure page"
);
}