Support explicit footnote numbers

\footnotemark[n] and \footnotetext[n]{...} left the bracket text in the line
and ignored the requested number. The footnote pass now reads an optional
bracketed integer: \footnotemark[n] emits that marker (and advances the counter
to it), while \footnotetext[n] attaches its body to the pending note with that
number, creating the entry when the mark appeared elsewhere in the paragraph.

A test uses mark and text with [7], checks "(7)" and the deferred body render
with no warnings, and that no "[7]" text leaks.
This commit is contained in:
gloamxun committed 2026-10-27 02:30:01 +08:00
1 parent d3baa78e1e
commit f05723808c
4 files changed
+67 -14

No files matched your search

+61 -8
View File
@@ -11926,6 +11926,18 @@ fn heading_font_size(level: u8, base: i64) -> i64 {
/// Pull `\footnote{...}` bodies out of a paragraph, leaving a numbered
/// reference in the running text. Footnote numbers run across the document.
/// Read an optional `[n]` footnote number, returning the value and the rest.
fn optional_note_number(text: &str) -> (Option<u32>, &str) {
let text = text.trim_start();
if let Some(rest) = text.strip_prefix('[')
&& let Some((value, tail)) = rest.split_once(']')
&& let Ok(number) = value.trim().parse()
{
return (Some(number), tail.trim_start());
}
(None, text)
}
fn number_footnotes(paragraphs: &mut [SourceParagraph]) {
let mut number = 0u32;
for paragraph in paragraphs {
@@ -11950,21 +11962,39 @@ fn number_footnotes(paragraphs: &mut [SourceParagraph]) {
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;
let (explicit, tail) = optional_note_number(after);
let mark = match explicit {
Some(mark) => {
number = mark;
mark
}
None => {
number += 1;
number
}
};
let _ = write!(text, " ({mark})");
paragraph.footnotes.push((mark, String::new()));
rest = tail;
}
"\\footnotetext" => {
let Some((body, consumed)) = braced_group(after) else {
let (explicit, tail) = optional_note_number(after);
let target = explicit.unwrap_or(number);
let Some((body, consumed)) = braced_group(tail) else {
text.push_str(&rest[index..]);
rest = "";
break;
};
if let Some(last) = paragraph.footnotes.last_mut() {
last.1 = body.trim().to_string();
if let Some(entry) = paragraph
.footnotes
.iter_mut()
.find(|(mark, _)| *mark == target)
{
entry.1 = body.trim().to_string();
} else {
paragraph.footnotes.push((target, body.trim().to_string()));
}
rest = &after[consumed..];
rest = &tail[consumed..];
}
_ => {
let Some((body, consumed)) = braced_group(after) else {
@@ -16639,6 +16669,29 @@ A definition.
assert!(!svg.contains("numberwithin"), "{svg}");
}
#[test]
fn explicit_footnote_numbers_pair_marks_and_texts() {
let source = "\\documentclass{article}\n\\begin{document}\n\nA\\footnotemark[7] B\\footnotetext[7]{Custom 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("(7)"), "{text}");
assert!(text.contains("Customnote."), "{text}");
assert!(!text.contains("[7]"), "{text}");
}
#[test]
fn heading_labels_on_the_next_line_resolve() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\section{Introduction}\n\\label{sec:intro}\n\nSee \\ref{sec:intro}.\n\n\\end{document}\n";