Append page numbers to float lists

This commit is contained in:
gloamxun committed 2026-10-25 01:21:49 +08:00
1 parent 60281dc9ca
commit fa99aaccfa
4 files changed
+74 -10

No files matched your search

+70 -6
View File
@@ -4910,20 +4910,28 @@ fn expand_float_lists(
figure_heading: &str,
table_heading: &str,
) {
let figures: Vec<String> = paragraphs
let figures: Vec<(String, String)> = paragraphs
.iter()
.filter_map(|paragraph| match &paragraph.float {
Some(spec) if spec.kind == FloatKind::Figure => {
spec.list_caption.clone().or_else(|| spec.caption.clone())
let caption = spec.list_caption.clone().or_else(|| spec.caption.clone())?;
let prefix = caption
.split_once(':')
.map_or("Figure", |(prefix, _)| prefix);
Some((prefix.trim().to_string(), caption))
}
_ => None,
})
.collect();
let tables: Vec<String> = paragraphs
let tables: Vec<(String, String)> = paragraphs
.iter()
.filter_map(|paragraph| match &paragraph.float {
Some(spec) if spec.kind == FloatKind::Table => {
spec.list_caption.clone().or_else(|| spec.caption.clone())
let caption = spec.list_caption.clone().or_else(|| spec.caption.clone())?;
let prefix = caption
.split_once(':')
.map_or("Table", |(prefix, _)| prefix);
Some((prefix.trim().to_string(), caption))
}
_ => None,
})
@@ -4947,9 +4955,9 @@ fn expand_float_lists(
let mut heading = bibliography_heading(heading);
heading.space_before = space_before;
list.push(heading);
for text in entries.iter() {
for (prefix, text) in entries.iter() {
list.push(SourceParagraph {
text: text.clone(),
text: format!("\\texrrafloatpage{{{prefix}}} {text}"),
page_break: false,
centered: false,
right_aligned: false,
@@ -5028,6 +5036,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
// Table-of-contents entries carry an invisible marker naming the heading
// whose page number they show; resolve it once the units exist.
let mut toc_targets: Vec<(usize, String)> = Vec::new();
let mut float_targets: Vec<(usize, String)> = Vec::new();
for (index, paragraph) in paragraphs.iter_mut().enumerate() {
if let Some(rest) = paragraph.text.trim_start().strip_prefix("\\texrratocpage{") {
if let Some((number, tail)) = rest.split_once('}') {
@@ -5035,6 +5044,16 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
paragraph.text = tail.trim_start().to_string();
toc_targets.push((index, number));
}
} else if let Some(rest) = paragraph
.text
.trim_start()
.strip_prefix("\\texrrafloatpage{")
{
if let Some((number, tail)) = rest.split_once('}') {
let number = number.trim().to_string();
paragraph.text = tail.trim_start().to_string();
float_targets.push((index, number));
}
}
}
let line_width = geometry.line_width.unwrap_or(480 * SCALE);
@@ -5348,6 +5367,27 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
entry.toc_target = Some(target as u64);
}
}
for (index, prefix) in float_targets {
let Some(target) = paragraphs.iter().position(|paragraph| {
paragraph
.float
.as_ref()
.and_then(|spec| spec.caption.as_deref())
.is_some_and(|caption| {
caption
.strip_prefix(&prefix)
.is_some_and(|rest| rest.starts_with(':'))
})
}) else {
continue;
};
if let (Some(entry), Some(target)) = (
units.get_mut(unit_for_paragraph[index]),
unit_for_paragraph.get(target).copied(),
) {
entry.toc_target = Some(target as u64);
}
}
let mut document = Document::new(700 * SCALE, units);
if footnote_count > 0 {
document.insertion_classes.push(InsertionClass {
@@ -7348,6 +7388,30 @@ A definition.
assert!(!stamp_toc_pages(&mut document, &engine, &geometry));
}
#[test]
fn float_lists_gain_page_numbers() {
let source = "\\listoffigures\n\n\\newpage\n\n\\begin{figure}[t]\n\\caption{Picture}\n\\end{figure}\n\nBody.";
let (body, geometry) = geometry_and_body(source);
let paragraphs =
paragraphs_with_definitions(&body, &geometry.theorem_definitions).expect("blocks");
let mut document = build_document(&paragraphs, &geometry);
apply_geometry(&mut document, &geometry);
let engine = layout_document(&document);
assert!(stamp_toc_pages(&mut document, &engine, &geometry));
let entries: Vec<&Unit> = document
.units
.iter()
.filter(|unit| unit.toc_target.is_some())
.collect();
assert_eq!(entries.len(), 1, "{entries:?}");
assert!(
entries[0].source.ends_with("\\hfill 2"),
"{}",
entries[0].source
);
assert!(!stamp_toc_pages(&mut document, &engine, &geometry));
}
#[test]
fn table_of_contents_lists_numbered_headings() {
let source = "\\tableofcontents\n\n\\section{Intro}\n\nText.\n\n\\subsection{Sub}\n\nMore.\n\n\\section*{Notes}\n\nEnd.";