Give standalone labels their heading title

A \label on its own paragraph after a heading resolved \ref and \autoref, but
\nameref fell back to the number because the label-to-title table only recorded
labels taken from heading text. Section numbering now tracks the latest heading
title and records it for label-only paragraphs (and ordinary paragraphs that
carry a label), so \nameref returns the title for those labels too.

A preview test puts \label on its own line after \section, then checks
"Introduction and Section 1" with no warnings.
This commit is contained in:
gloamxun committed 2026-10-27 03:07:12 +08:00
1 parent abf5e90578
commit 4ad15f1f0b
4 files changed
+42 -6

No files matched your search

+36
View File
@@ -12003,6 +12003,7 @@ fn number_sections_with_titles(
let mut appendix = false;
let mut labels = Vec::new();
let mut last_number: Option<String> = None;
let mut last_title: Option<String> = None;
let mut index = 0;
while index < blocks.len() {
if blocks[index].text.trim() == "\\appendix" {
@@ -12020,9 +12021,17 @@ fn number_sections_with_titles(
} else {
String::new()
};
if level.is_some() {
last_title = Some(block_title.clone());
}
if level.is_none() {
if !block_labels.is_empty() && blocks[index].text.trim().is_empty() {
let value = last_number.clone().unwrap_or_else(|| "??".to_string());
if let Some(title) = last_title.clone() {
for label in &block_labels {
titles.push((label.clone(), title.clone()));
}
}
for label in block_labels {
labels.push((label, value.clone()));
}
@@ -12030,6 +12039,11 @@ fn number_sections_with_titles(
continue;
}
let value = last_number.clone().unwrap_or_else(|| "??".to_string());
if let Some(title) = last_title.clone() {
for label in &block_labels {
titles.push((label.clone(), title.clone()));
}
}
for label in block_labels {
labels.push((label, value.clone()));
}
@@ -17089,6 +17103,28 @@ A definition.
assert!(text.contains("D"), "{text}");
}
#[test]
fn nameref_resolves_standalone_labels() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\section{Introduction}\n\n\\label{sec:intro}\n\nSee \\nameref{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("IntroductionandSection1"), "{text}");
assert!(!text.contains("??"), "{text}");
}
#[test]
fn scaled_box_wrappers_keep_their_content() {
let source = "\\documentclass{article}\n\\begin{document}\n\nA \\scalebox{0.5}{small} B \\resizebox{2cm}{!}{wide} C \\rotatebox{90}{turned} D \\raisebox{2pt}{lifted} E.\n\n\\end{document}\n";