List figures and tables from their numbered captions

This commit is contained in:
gloamxun committed 2026-10-24 22:37:18 +08:00
1 parent b2f5b67b07
commit 8955babea8
5 files changed
+127 -4

No files matched your search

+98
View File
@@ -858,6 +858,14 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
});
continue;
}
if trimmed == "\\listoffigures" {
body.push_str("\\texrralof\n\n");
continue;
}
if trimmed == "\\listoftables" {
body.push_str("\\texrralot\n\n");
continue;
}
if trimmed == "\\tableofcontents" {
body.push_str("\\texrratoc\n\n");
continue;
@@ -3812,6 +3820,60 @@ fn expand_toc(paragraphs: &mut Vec<SourceParagraph>) {
paragraphs.splice(position..=position, toc);
}
fn expand_float_lists(paragraphs: &mut Vec<SourceParagraph>) {
let figures: Vec<String> = paragraphs
.iter()
.filter_map(|paragraph| match &paragraph.float {
Some(spec) if spec.kind == FloatKind::Figure => spec.caption.clone(),
_ => None,
})
.collect();
let tables: Vec<String> = paragraphs
.iter()
.filter_map(|paragraph| match &paragraph.float {
Some(spec) if spec.kind == FloatKind::Table => spec.caption.clone(),
_ => None,
})
.collect();
for (marker, entries) in [("\\texrralof", figures), ("\\texrralot", tables)] {
let Some(position) = paragraphs
.iter()
.position(|paragraph| paragraph.text.trim() == marker)
else {
continue;
};
let space_before = paragraphs[position].space_before;
let mut list = Vec::with_capacity(entries.len());
for (index, text) in entries.iter().enumerate() {
list.push(SourceParagraph {
text: text.clone(),
page_break: false,
centered: false,
right_aligned: false,
font: ParagraphFont::Roman,
line_height: 12,
math: false,
left_skip: 0,
space_before: if index == 0 { space_before } else { 2 * SCALE },
space_stretch: 0,
space_shrink: 0,
equation_number: None,
equation_tag: None,
equation_rows: Vec::new(),
align_centered: false,
heading_level: None,
heading_starred: false,
heading_number: None,
font_size: None,
footnotes: Vec::new(),
float: None,
theorem: None,
});
}
paragraphs.splice(position..=position, list);
}
}
fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Document {
let mut paragraphs = expand_maketitle(paragraphs, geometry);
let section_labels = number_sections(&mut paragraphs);
@@ -3822,6 +3884,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
&geometry.theorem_definitions,
));
references.extend(number_floats(&mut paragraphs));
expand_float_lists(&mut paragraphs);
resolve_section_refs(&mut paragraphs, &references);
number_footnotes(&mut paragraphs);
resolve_citations(&mut paragraphs, &geometry.bibliography);
@@ -5038,6 +5101,41 @@ A definition.
plain_y
);
}
#[test]
fn float_lists_follow_their_captions() {
let source = "\\listoffigures\n\n\\listoftables\n\n\\begin{figure}\n\\caption{Plot}\\label{fig:p}\n\\end{figure}\n\n\\begin{table}\n\\begin{tabular}{l}\nA\n\\end{tabular}\n\\caption{Numbers}\\label{tab:t}\n\\end{table}\n\nBody.";
let (body, geometry) = geometry_and_body(source);
let paragraphs =
paragraphs_with_definitions(&body, &geometry.theorem_definitions).expect("blocks");
let document = build_document(&paragraphs, &geometry);
let sources: Vec<&str> = document
.units
.iter()
.map(|unit| unit.source.as_str())
.collect();
assert_eq!(
sources
.iter()
.filter(|source| **source == "Figure 1: Plot")
.count(),
1,
"{sources:?}"
);
assert_eq!(
sources
.iter()
.filter(|source| **source == "Table 1: Numbers")
.count(),
1,
"{sources:?}"
);
assert_eq!(sources.len(), 5, "{sources:?}");
assert!(!sources.iter().any(|source| source.contains("texrralof")));
assert!(!sources.iter().any(|source| source.contains("texrralot")));
assert_eq!(document.units[0].source, "Figure 1: Plot");
assert_eq!(document.units[1].source, "Table 1: Numbers");
}
#[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.";