Resolve nameref for figure captions

\nameref fell back to the number for float labels because only headings fed the
title table. The float numbering pass now records each label with its raw
caption text, and that table is merged into the same title map nameref reads, so
\nameref{fig:a} returns "A plot" while \autoref keeps "Figure 1".

A preview test labels a figure with \caption{A plot} and checks
"See A plot and Figure 1." with no warnings.
This commit is contained in:
gloamxun committed 2026-10-27 03:26:04 +08:00
1 parent 8a5411af1d
commit 4d713df3dd
4 files changed
+39 -7

No files matched your search

+33 -1
View File
@@ -12438,6 +12438,7 @@ fn number_floats(
table_start: u32,
) -> Vec<(String, String)> {
let mut kinds = Vec::new();
let mut titles = Vec::new();
number_floats_with_kinds(
paragraphs,
figure_name,
@@ -12445,6 +12446,7 @@ fn number_floats(
figure_start,
table_start,
&mut kinds,
&mut titles,
)
}
@@ -12455,6 +12457,7 @@ fn number_floats_with_kinds(
figure_start: u32,
table_start: u32,
kinds: &mut Vec<(String, String)>,
titles: &mut Vec<(String, String)>,
) -> Vec<(String, String)> {
let mut figures = figure_start;
let mut tables = table_start;
@@ -12482,12 +12485,14 @@ fn number_floats_with_kinds(
}
};
let prefix = spec.kind.caption_prefix(figure_name, table_name);
*caption = format!("{prefix} {number}: {}", caption.trim());
let caption_text = caption.trim().to_string();
*caption = format!("{prefix} {number}: {caption_text}");
if let Some(short) = &spec.caption_short {
spec.list_caption = Some(format!("{prefix} {number}: {short}"));
}
for label in found {
kinds.push((label.clone(), prefix.to_string()));
titles.push((label.clone(), caption_text.clone()));
labels.push((label, number.to_string()));
}
}
@@ -12824,6 +12829,7 @@ fn build_document_with_labels(
geometry_counter(geometry, "figure"),
geometry_counter(geometry, "table"),
&mut reference_kinds,
&mut section_titles,
));
expand_float_lists(
&mut paragraphs,
@@ -17192,6 +17198,32 @@ A definition.
assert!(text.contains("D"), "{text}");
}
#[test]
fn nameref_resolves_figure_captions() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\begin{figure}[h]\n\\rule{10pt}{10pt}\n\\caption{A plot}\\label{fig:a}\n\\end{figure}\n\nSee \\nameref{fig:a} and \\autoref{fig:a}.\n\n\\end{document}\n";
let json = preview_json(source).expect("preview");
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
assert_eq!(
value["warnings"].as_array().map(Vec::len),
Some(0),
"{}",
value["warnings"]
);
let pages = value["pages"].as_array().expect("pages");
let mut text = String::new();
for page in pages {
if let Some(svg) = page["svg"].as_str() {
text.push_str(
&svg.split('>')
.filter_map(|piece| piece.split('<').next())
.collect::<String>(),
);
}
}
let text: String = text.split_whitespace().collect();
assert!(text.contains("SeeAplotandFigure1."), "{text}");
}
#[test]
fn counter_forms_expand_in_text() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\setcounter{section}{4}\n\n\\arabic{section} \\roman{section} \\Alph{section} \\value{section}.\n\n\\end{document}\n";