Number equation environments in route-D
This commit is contained in:
1 parent
3cbfdabe03
commit
bb897411d0
5 files changed
+207
-6
No files matched your search
+139
-3
@@ -10,7 +10,7 @@ use std::fmt::Write as _;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
use texrra_tex_core::cmr10::Cmr10Metrics;
|
||||
use texrra_tex_core::math::{parse_math, split_math_lines};
|
||||
use texrra_tex_core::math::{MathAtom, parse_math, split_math_lines};
|
||||
use texrra_tex_core::pdf::{PageOrigin, display_lists_to_pdf_with_paper};
|
||||
use texrra_tex_core::{Document, Engine, ParagraphFont, ParagraphStyle, Unit};
|
||||
|
||||
@@ -639,6 +639,8 @@ struct SourceParagraph {
|
||||
space_before: i64,
|
||||
space_stretch: i64,
|
||||
space_shrink: i64,
|
||||
/// Number of the equation environment this block came from.
|
||||
equation_number: Option<u32>,
|
||||
}
|
||||
|
||||
/// Recognise an explicit vertical skip command standing alone between
|
||||
@@ -703,6 +705,7 @@ fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
|
||||
space_stretch: pending.1,
|
||||
space_shrink: pending.2,
|
||||
page_break: pending_break,
|
||||
equation_number: None,
|
||||
});
|
||||
pending = (0, 0, 0);
|
||||
pending_break = false;
|
||||
@@ -718,7 +721,7 @@ fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
|
||||
pending.2 += skip.2;
|
||||
continue;
|
||||
}
|
||||
let mut blocks = parse_blocks(chunk);
|
||||
let mut blocks = parse_blocks_raw(chunk);
|
||||
if let Some(first) = blocks.first_mut() {
|
||||
first.space_before += pending.0;
|
||||
first.space_stretch += pending.1;
|
||||
@@ -729,6 +732,7 @@ fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
|
||||
}
|
||||
result.extend(blocks);
|
||||
}
|
||||
resolve_equation_numbers(&mut result);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
@@ -738,7 +742,41 @@ fn command_body<'a>(text: &'a str, command: &str) -> Option<&'a str> {
|
||||
Some(rest.trim())
|
||||
}
|
||||
|
||||
fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
|
||||
/// Assign equation numbers in source order and substitute label references.
|
||||
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() {
|
||||
continue;
|
||||
}
|
||||
number += 1;
|
||||
block.equation_number = Some(number);
|
||||
loop {
|
||||
let Some(start) = block.text.find("\\label{") else {
|
||||
break;
|
||||
};
|
||||
let Some(end) = block.text[start..].find('}') else {
|
||||
break;
|
||||
};
|
||||
let label = block.text[start + 7..start + end].trim().to_string();
|
||||
labels.push((label, number));
|
||||
block.text.replace_range(start..start + end + 1, "");
|
||||
}
|
||||
}
|
||||
for block in blocks.iter_mut() {
|
||||
for (label, value) in labels.iter() {
|
||||
let eqref = format!("\\eqref{{{label}}}");
|
||||
let reference = format!("\\ref{{{label}}}");
|
||||
block.text = block
|
||||
.text
|
||||
.replace(&eqref, &format!("({value})"))
|
||||
.replace(&reference, &value.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
|
||||
for (environment, numbered) in [("itemize", false), ("enumerate", true)] {
|
||||
let begin = format!("\\begin{{{environment}}}");
|
||||
let finish = format!("\\end{{{environment}}}");
|
||||
@@ -772,6 +810,7 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
|
||||
space_stretch: 0,
|
||||
space_shrink: 0,
|
||||
page_break: false,
|
||||
equation_number: None,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
@@ -797,6 +836,7 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
|
||||
space_stretch: 0,
|
||||
space_shrink: 0,
|
||||
page_break: false,
|
||||
equation_number: None,
|
||||
}];
|
||||
}
|
||||
}
|
||||
@@ -831,6 +871,7 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
|
||||
space_stretch: 0,
|
||||
space_shrink: 0,
|
||||
page_break: false,
|
||||
equation_number: None,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
@@ -845,6 +886,44 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
|
||||
/// a full LaTeX reader; it exists so the live endpoint can show headings and
|
||||
/// emphasis while the semantic layer is built out.
|
||||
fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
if let Some(inner) = raw
|
||||
.strip_prefix("\\begin{equation}")
|
||||
.and_then(|text| text.strip_suffix("\\end{equation}"))
|
||||
{
|
||||
return SourceParagraph {
|
||||
text: inner.trim().to_string(),
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
math: true,
|
||||
left_skip: 0,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
space_before: 0,
|
||||
space_stretch: 0,
|
||||
space_shrink: 0,
|
||||
page_break: false,
|
||||
equation_number: Some(0),
|
||||
};
|
||||
}
|
||||
if let Some(inner) = raw
|
||||
.strip_prefix("\\begin{equation*}")
|
||||
.and_then(|text| text.strip_suffix("\\end{equation*}"))
|
||||
{
|
||||
return SourceParagraph {
|
||||
text: inner.trim().to_string(),
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
math: true,
|
||||
left_skip: 0,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
space_before: 0,
|
||||
space_stretch: 0,
|
||||
space_shrink: 0,
|
||||
page_break: false,
|
||||
equation_number: None,
|
||||
};
|
||||
}
|
||||
if let Some(math) = raw
|
||||
.strip_prefix("$$")
|
||||
.and_then(|text| text.strip_suffix("$$"))
|
||||
@@ -865,6 +944,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
space_stretch: 0,
|
||||
space_shrink: 0,
|
||||
page_break: false,
|
||||
equation_number: None,
|
||||
};
|
||||
}
|
||||
let candidates = [
|
||||
@@ -886,6 +966,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
space_stretch: 0,
|
||||
space_shrink: 0,
|
||||
page_break: false,
|
||||
equation_number: None,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -901,6 +982,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
space_stretch: 0,
|
||||
space_shrink: 0,
|
||||
page_break: false,
|
||||
equation_number: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -927,6 +1009,13 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
if paragraph.math {
|
||||
let font = Cmr10Metrics::Math;
|
||||
let atom = parse_math(¶graph.text);
|
||||
let atom = match paragraph.equation_number {
|
||||
Some(number) => MathAtom::Tagged {
|
||||
body: Box::new(atom),
|
||||
tag: format!("({number})"),
|
||||
},
|
||||
None => atom,
|
||||
};
|
||||
let lines = split_math_lines(&atom, &font, 480 * SCALE);
|
||||
let mut unit = Unit::math_lines(index as u64, &lines, &font);
|
||||
unit.space_before = paragraph.space_before;
|
||||
@@ -1390,3 +1479,50 @@ mod source_tests {
|
||||
assert!(engine.pages()[0].items.len() >= 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod equation_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn equations_are_numbered_and_labels_resolved() {
|
||||
let source = r"\begin{equation}
|
||||
x^2 + y^2
|
||||
\label{eq:sum}
|
||||
\end{equation}
|
||||
|
||||
See \eqref{eq:sum} and \ref{eq:sum}.
|
||||
|
||||
\begin{equation*}
|
||||
z
|
||||
\end{equation*}
|
||||
|
||||
\begin{equation}
|
||||
a + b
|
||||
\end{equation}";
|
||||
let blocks = paragraphs(source).expect("paragraphs");
|
||||
let numbers: Vec<u32> = blocks
|
||||
.iter()
|
||||
.filter_map(|block| block.equation_number)
|
||||
.collect();
|
||||
assert_eq!(numbers, vec![1, 2], "equation numbering");
|
||||
let reference = blocks
|
||||
.iter()
|
||||
.find(|block| block.text.starts_with("See"))
|
||||
.expect("reference paragraph");
|
||||
assert_eq!(reference.text, "See (1) and 1.");
|
||||
assert!(
|
||||
!blocks.iter().any(|block| block.text.contains("\\label")),
|
||||
"labels are stripped from equation bodies"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numbered_equation_renders_its_tag() {
|
||||
let json = preview_json("\\begin{equation}\nx^2\n\\end{equation}").expect("preview");
|
||||
assert!(json.contains(">x</text>"), "formula glyphs in SVG: {json}");
|
||||
assert!(json.contains(">(</text>"), "open tag parenthesis");
|
||||
assert!(json.contains(">1</text>"), "equation number");
|
||||
assert!(json.contains(">)</text>"), "close tag parenthesis");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user