Support tag and notag on numbered equations

This commit is contained in:
gloamxun committed 2026-10-24 02:00:47 +08:00
1 parent bb897411d0
commit f6726439ed
3 files changed
+99 -7

No files matched your search

+96 -4
View File
@@ -641,6 +641,7 @@ struct SourceParagraph {
space_shrink: i64,
/// Number of the equation environment this block came from.
equation_number: Option<u32>,
equation_tag: Option<String>,
}
/// Recognise an explicit vertical skip command standing alone between
@@ -706,6 +707,7 @@ fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
space_shrink: pending.2,
page_break: pending_break,
equation_number: None,
equation_tag: None,
});
pending = (0, 0, 0);
pending_break = false;
@@ -747,7 +749,18 @@ fn resolve_equation_numbers(blocks: &mut [SourceParagraph]) {
let mut number = 0;
let mut labels: Vec<(String, u32)> = Vec::new();
for block in blocks.iter_mut() {
if !block.math || block.equation_number.is_none() {
if !block.math {
continue;
}
if let Some(tag) = take_braced_argument(&mut block.text, "\\tag") {
block.equation_tag = Some(tag);
block.equation_number = None;
}
if block.text.contains("\\notag") || block.text.contains("\\nonumber") {
block.equation_number = None;
}
block.text = block.text.replace("\\notag", "").replace("\\nonumber", "");
if block.equation_number.is_none() || block.equation_tag.is_some() {
continue;
}
number += 1;
@@ -811,6 +824,7 @@ fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
space_shrink: 0,
page_break: false,
equation_number: None,
equation_tag: None,
}
})
.collect();
@@ -837,6 +851,7 @@ fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
space_shrink: 0,
page_break: false,
equation_number: None,
equation_tag: None,
}];
}
}
@@ -872,6 +887,7 @@ fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
space_shrink: 0,
page_break: false,
equation_number: None,
equation_tag: None,
}
})
.collect();
@@ -885,6 +901,30 @@ fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
/// Map a small, general LaTeX subset onto route-D paragraph faces. This is not
/// a full LaTeX reader; it exists so the live endpoint can show headings and
/// emphasis while the semantic layer is built out.
fn take_braced_argument(text: &mut String, command: &str) -> Option<String> {
let start = text.find(command)?;
let after = start + command.len();
if !text[after..].starts_with('{') {
return None;
}
let mut depth = 0_usize;
for (offset, character) in text[after..].char_indices() {
match character {
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
let inner = text[after + 1..after + offset].to_string();
text.replace_range(start..after + offset + 1, "");
return Some(inner);
}
}
_ => {}
}
}
None
}
fn parse_paragraph(raw: &str) -> SourceParagraph {
if let Some(inner) = raw
.strip_prefix("\\begin{equation}")
@@ -903,6 +943,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
space_shrink: 0,
page_break: false,
equation_number: Some(0),
equation_tag: None,
};
}
if let Some(inner) = raw
@@ -922,6 +963,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
space_shrink: 0,
page_break: false,
equation_number: None,
equation_tag: None,
};
}
if let Some(math) = raw
@@ -945,6 +987,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
space_shrink: 0,
page_break: false,
equation_number: None,
equation_tag: None,
};
}
let candidates = [
@@ -967,6 +1010,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
space_shrink: 0,
page_break: false,
equation_number: None,
equation_tag: None,
};
}
}
@@ -983,6 +1027,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
space_shrink: 0,
page_break: false,
equation_number: None,
equation_tag: None,
}
}
@@ -1009,10 +1054,15 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
if paragraph.math {
let font = Cmr10Metrics::Math;
let atom = parse_math(&paragraph.text);
let atom = match paragraph.equation_number {
Some(number) => MathAtom::Tagged {
let tag = paragraph.equation_tag.clone().or_else(|| {
paragraph
.equation_number
.map(|number| format!("({number})"))
});
let atom = match tag {
Some(tag) => MathAtom::Tagged {
body: Box::new(atom),
tag: format!("({number})"),
tag,
},
None => atom,
};
@@ -1517,6 +1567,48 @@ a + b
);
}
#[test]
fn equation_tags_override_and_suppress_numbers() {
let source = r"\begin{equation}
a
\end{equation}
\begin{equation}
b
\tag{B}
\end{equation}
\begin{equation}
c
\notag
\end{equation}
\begin{equation}
d
\end{equation}";
let blocks = paragraphs(source).expect("paragraphs");
let tags: Vec<(Option<u32>, Option<String>)> = blocks
.iter()
.filter(|block| block.math)
.map(|block| (block.equation_number, block.equation_tag.clone()))
.collect();
assert_eq!(
tags,
vec![
(Some(1), None),
(None, Some("B".to_string())),
(None, None),
(Some(2), None)
]
);
assert!(
!blocks
.iter()
.any(|block| block.text.contains("\\tag") || block.text.contains("\\notag")),
"tagging commands are stripped from the formula"
);
}
#[test]
fn numbered_equation_renders_its_tag() {
let json = preview_json("\\begin{equation}\nx^2\n\\end{equation}").expect("preview");