Resolve autoref to its section number

\autoref was unsupported and leaked. The section-reference pass now rewrites
\autoref{label} to "Section <number>" using the same label table as \ref, and
unresolved autorefs become "??" with the other reference commands.

A preview test labels a heading inside its braces
(\section{Introduction\label{sec:intro}}), shows \ref and \autoref side by
side, and checks "1 and Section 1" with no warnings.
This commit is contained in:
gloamxun committed 2026-10-27 02:12:21 +08:00
1 parent 6139621653
commit c399481c6a
4 files changed
+33 -7

No files matched your search

+27 -1
View File
@@ -11808,8 +11808,12 @@ fn resolve_section_refs(blocks: &mut [SourceParagraph], labels: &[(String, Strin
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}"),
);
}
for command in ["\\ref", "\\eqref"] {
for command in ["\\ref", "\\eqref", "\\autoref"] {
loop {
let prefix = format!("{command}{{");
let Some(start) = block.text.find(&prefix) else {
@@ -16540,6 +16544,28 @@ A definition.
assert!(!svg.contains("numberwithin"), "{svg}");
}
#[test]
fn autoref_resolves_section_labels() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\section{Introduction\\label{sec:intro}}\n\nSee \\ref{sec:intro} and \\autoref{sec:intro}.\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 svg = value["pages"][0]["svg"].as_str().expect("svg");
let text: String = svg
.split('>')
.filter_map(|piece| piece.split('<').next())
.collect::<String>()
.split_whitespace()
.collect();
assert!(text.contains("1andSection1"), "{text}");
assert!(!text.contains("autoref"), "{text}");
}
#[test]
fn phantom_commands_reserve_invisible_space() {
let source = "\\documentclass{article}\n\\begin{document}\n\nA\\phantom{wide}B and \\vphantom{XX}C and \\hphantom{QQ}D.\n\n\\end{document}\n";