Give autoref the right kind prefix
\autoref always wrote "Section <number>", even for figure, table or theorem labels. The reference passes now record a kind alongside each label: sections use "Section", floats use their caption prefix (Figure/Table), and theorems use their display name; resolve_section_refs builds \autoref from that kind, falling back to "Section" for unknown labels. A preview test labels a figure, refers to it with \autoref, and checks "Figure 1" with no warnings.
This commit is contained in:
1 parent
48e86fb214
commit
5dda1a948a
4 files changed
+76
-12
No files matched your search
+70
-6
@@ -11911,15 +11911,20 @@ fn resolve_section_refs(
|
||||
blocks: &mut [SourceParagraph],
|
||||
labels: &[(String, String)],
|
||||
titles: &[(String, String)],
|
||||
kinds: &[(String, String)],
|
||||
) {
|
||||
for block in blocks {
|
||||
for (label, value) in labels {
|
||||
block.text = block.text.replace(&format!("\\ref{{{label}}}"), value);
|
||||
block.text = block.text.replace(&format!("\\eqref{{{label}}}"), value);
|
||||
block.text = block.text.replace(
|
||||
&format!("\\autoref{{{label}}}"),
|
||||
&format!("Section {value}"),
|
||||
);
|
||||
let kind = kinds
|
||||
.iter()
|
||||
.find(|(known, _)| known == label)
|
||||
.map(|(_, kind)| kind.as_str())
|
||||
.unwrap_or("Section");
|
||||
block.text = block
|
||||
.text
|
||||
.replace(&format!("\\autoref{{{label}}}"), &format!("{kind} {value}"));
|
||||
let name = titles
|
||||
.iter()
|
||||
.find(|(known, _)| known == label)
|
||||
@@ -12048,6 +12053,7 @@ fn number_theorems(
|
||||
paragraphs: &mut [SourceParagraph],
|
||||
definitions: &[TheoremDefinition],
|
||||
starts: &[(String, u32)],
|
||||
kinds: &mut Vec<(String, String)>,
|
||||
) -> Vec<(String, String)> {
|
||||
let mut counters: Vec<(String, u32)> = Vec::new();
|
||||
let mut labels = Vec::new();
|
||||
@@ -12108,6 +12114,7 @@ fn number_theorems(
|
||||
let number = theorem.number.clone();
|
||||
if let Some(number) = number {
|
||||
for label in theorem.labels.drain(..) {
|
||||
kinds.push((label.clone(), display.clone()));
|
||||
labels.push((label, number.clone()));
|
||||
}
|
||||
} else {
|
||||
@@ -12124,6 +12131,25 @@ fn number_floats(
|
||||
table_name: &str,
|
||||
figure_start: u32,
|
||||
table_start: u32,
|
||||
) -> Vec<(String, String)> {
|
||||
let mut kinds = Vec::new();
|
||||
number_floats_with_kinds(
|
||||
paragraphs,
|
||||
figure_name,
|
||||
table_name,
|
||||
figure_start,
|
||||
table_start,
|
||||
&mut kinds,
|
||||
)
|
||||
}
|
||||
|
||||
fn number_floats_with_kinds(
|
||||
paragraphs: &mut [SourceParagraph],
|
||||
figure_name: &str,
|
||||
table_name: &str,
|
||||
figure_start: u32,
|
||||
table_start: u32,
|
||||
kinds: &mut Vec<(String, String)>,
|
||||
) -> Vec<(String, String)> {
|
||||
let mut figures = figure_start;
|
||||
let mut tables = table_start;
|
||||
@@ -12156,6 +12182,7 @@ fn number_floats(
|
||||
spec.list_caption = Some(format!("{prefix} {number}: {short}"));
|
||||
}
|
||||
for label in found {
|
||||
kinds.push((label.clone(), prefix.to_string()));
|
||||
labels.push((label, number.to_string()));
|
||||
}
|
||||
}
|
||||
@@ -12474,18 +12501,24 @@ fn build_document_with_labels(
|
||||
geometry.contents_name.as_deref().unwrap_or("Contents"),
|
||||
chaptered,
|
||||
);
|
||||
let mut reference_kinds: Vec<(String, String)> = section_titles
|
||||
.iter()
|
||||
.map(|(label, _)| (label.clone(), "Section".to_string()))
|
||||
.collect();
|
||||
let mut references = section_labels;
|
||||
references.extend(number_theorems(
|
||||
&mut paragraphs,
|
||||
&geometry.theorem_definitions,
|
||||
&geometry.counters,
|
||||
&mut reference_kinds,
|
||||
));
|
||||
references.extend(number_floats(
|
||||
references.extend(number_floats_with_kinds(
|
||||
&mut paragraphs,
|
||||
geometry.figure_name.as_deref().unwrap_or("Figure"),
|
||||
geometry.table_name.as_deref().unwrap_or("Table"),
|
||||
geometry_counter(geometry, "figure"),
|
||||
geometry_counter(geometry, "table"),
|
||||
&mut reference_kinds,
|
||||
));
|
||||
expand_float_lists(
|
||||
&mut paragraphs,
|
||||
@@ -12498,7 +12531,12 @@ fn build_document_with_labels(
|
||||
.as_deref()
|
||||
.unwrap_or("List of Tables"),
|
||||
);
|
||||
resolve_section_refs(&mut paragraphs, &references, §ion_titles);
|
||||
resolve_section_refs(
|
||||
&mut paragraphs,
|
||||
&references,
|
||||
§ion_titles,
|
||||
&reference_kinds,
|
||||
);
|
||||
number_footnotes(&mut paragraphs);
|
||||
resolve_citations(&mut paragraphs, &geometry.bibliography);
|
||||
append_bibliography(
|
||||
@@ -16805,6 +16843,32 @@ A definition.
|
||||
assert!(!text.contains("??"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn autoref_uses_the_float_kind() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\begin{figure}[h]\n\\rule{10pt}{10pt}\n\\caption{Cap}\\label{fig:a}\n\\end{figure}\n\nSee \\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("SeeFigure1."), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn proof_bodies_end_with_a_qed_symbol() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\begin{proof}\nBecause it follows.\n\\end{proof}\n\n\\end{document}\n";
|
||||
|
||||
Reference in new issue
Block a user