Support footnotemark and footnotetext

\footnotemark and \footnotetext were unsupported, so deferring a note body was
impossible. The footnote numbering pass now selects among \footnote,
\footnotemark and \footnotetext (longest name first so the prefix cannot win):
\footnotemark advances the counter, emits the numbered reference and records an
empty note for the number; \footnotetext fills that pending note with its body
and leaves no text in the line; the remaining text is appended once.

A test pairs a mark and its text in one paragraph, checks the page carries
"(1)" and "Deferred note." with no warnings, and that neither command leaks.
This commit is contained in:
gloamxun committed 2026-10-27 01:58:22 +08:00
1 parent 53a299bd58
commit 6c6963cac7
4 files changed
+74 -17

No files matched your search

+68 -11
View File
@@ -11747,20 +11747,53 @@ fn number_footnotes(paragraphs: &mut [SourceParagraph]) {
let source = paragraph.text.clone();
let mut text = String::with_capacity(source.len());
let mut rest = source.as_str();
while let Some(index) = rest.find("\\footnote") {
text.push_str(&rest[..index]);
let after = rest[index + "\\footnote".len()..].trim_start();
let Some((body, consumed)) = braced_group(after) else {
text.push_str(&rest[index..]);
rest = "";
loop {
let mut next: Option<(usize, &str)> = None;
for command in ["\\footnotemark", "\\footnotetext", "\\footnote"] {
if let Some(index) = rest.find(command) {
let better = next.is_none_or(|(known, _)| index < known);
if better {
next = Some((index, command));
}
}
}
let Some((index, command)) = next else {
text.push_str(rest);
break;
};
number += 1;
let _ = write!(text, " ({number})");
paragraph.footnotes.push((number, body.trim().to_string()));
rest = &after[consumed..];
text.push_str(&rest[..index]);
let after = rest[index + command.len()..].trim_start();
match command {
"\\footnotemark" => {
number += 1;
let _ = write!(text, " ({number})");
paragraph.footnotes.push((number, String::new()));
rest = after;
}
"\\footnotetext" => {
let Some((body, consumed)) = braced_group(after) else {
text.push_str(&rest[index..]);
rest = "";
break;
};
if let Some(last) = paragraph.footnotes.last_mut() {
last.1 = body.trim().to_string();
}
rest = &after[consumed..];
}
_ => {
let Some((body, consumed)) = braced_group(after) else {
text.push_str(&rest[index..]);
rest = "";
break;
};
number += 1;
let _ = write!(text, " ({number})");
paragraph.footnotes.push((number, body.trim().to_string()));
rest = &after[consumed..];
}
}
}
text.push_str(rest);
paragraph.text = text;
}
}
@@ -16391,6 +16424,30 @@ A definition.
assert!(!text.contains("makeatletter"), "{text}");
}
#[test]
fn footnotemark_and_footnotetext_pair_up() {
let source = "\\documentclass{article}\n\\begin{document}\n\nText\\footnotemark and more.\\footnotetext{Deferred note.}\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("Deferrednote."), "{text}");
assert!(text.contains("(1)"), "{text}");
assert!(!svg.contains("footnotemark"), "{svg}");
assert!(!svg.contains("footnotetext"), "{svg}");
}
#[test]
fn protect_survives_edef_and_expands_normally() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\a{A}\n\\edef\\b{\\protect\\a}\n\\def\\a{C}\n\n\\b and \\protect\\a\n\n\\end{document}\n";