Expand body edef definitions when they are read

\edef/\xdef were expanded eagerly only in the preamble: after \begin{document}
the strip pass no longer captured definitions, and capture_body_definitions had
no edef branch, so a body \edef\b{\a} stored the raw body and expanded \a later.
The body branch now calls edef_definition (which also resolves \csname), and the
strip pass leaves body edef/chardef lines to it instead of swallowing them.

A test with \begin{document} defines a=one, \edef b={\a}, redefines a=two and
expects "one and two"; the existing preamble edef/xdef tests still pass.
This commit is contained in:
gloamxun committed 2026-10-27 00:19:28 +08:00
1 parent 9aa743b601
commit d4de3a3f35
3 files changed
+35 -9

No files matched your search

+1 -1
View File
@@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
PDF opt-in with `?engine=route-d`). 990 workspace tests are green and no oracle
PDF opt-in with `?engine=route-d`). 991 workspace tests are green and no oracle
fixture is ignored. Preview JSON also carries a `warnings` array naming
environments route-D cannot lay out and control words that survived layout, so
unsupported source is reported instead of being silently typeset.
+5 -3
View File
@@ -370,7 +370,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`, `cmssbx10`, `cmssi10`, `cmcsc10`) supplies advances, kerns, ligatures, and italic correction for layout, the display list selects the matching face, and the PDF writes Computer Modern Type1 outlines for each built-in face (`cmr10`/`cmbx10`/`cmti10`/`cmbxti10`/`cmtt10`/`cmss10`/`cmssbx10`/`cmssi10`/`cmcsc10`, plus `cmmi10` for mathematical italic letters, `cmsy10` for math symbols and `cmex10` for big operators), falling back to Liberation subsets only for characters the Type1 encodings cannot address; those fallback glyphs keep their Unicode text under PDF extraction, so accented and Greek words round-trip; Type1 faces carry a ToUnicode CMap built from the requested characters, so glyphs whose names are ambiguous in the Adobe Glyph List (capital Omega, which would otherwise extract as U+2126) keep their intended code points. `Engine::edit` preserves the face when reflowing an edited paragraph.
The crate currently has 437 unit tests plus ten route-D integration tests and forty-two oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 990 tests (437 route-D unit, 365 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 60 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
The crate currently has 437 unit tests plus ten route-D integration tests and forty-two oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 991 tests (437 route-D unit, 366 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 60 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
### M0 incremental demo
@@ -535,8 +535,10 @@ honest open items against the "feature-complete LaTeX compiler" objective are:
command parsing. Runtime `\let\alias=\target` and `\chardef\name=<code>`
also define (or locally shadow) commands, and the line-based capture only
runs before `\begin{document}` and outside braces, so a definition on its own
line inside a group stays local. There is still no catcode state and no token
stream, so class code that relies on those semantics degrades.
line inside a group stays local. `\edef`/`\xdef` lines in the preamble and in
the body expand their bodies (including `\csname`) when read, while a plain
`\def` stays lazy. There is still no catcode state and no token stream, so
class code that relies on those semantics degrades.
- **Boxes**: `\sbox`/`\savebox` content and `\mbox`/`\hbox`/`\makebox`
bodies are inserted as one unbreakable inline box (interword spaces become
fixed-width boxes), so `\usebox` no longer reflows across lines. Savebox
+29 -5
View File
@@ -2608,16 +2608,17 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
continue;
}
let definition_line = strip_definition_prefixes(trimmed);
if trimmed.starts_with("\\chardef") {
if !in_document && trimmed.starts_with("\\chardef") {
if let Some(definition) = char_definition(trimmed) {
geometry.macros.push(definition);
}
continue;
}
if definition_line.starts_with(r"\edef")
|| definition_line.starts_with(r"\xdef")
|| definition_line.starts_with(r"\protected@edef")
|| definition_line.starts_with(r"\protected@xdef")
if !in_document
&& (definition_line.starts_with(r"\edef")
|| definition_line.starts_with(r"\xdef")
|| definition_line.starts_with(r"\protected@edef")
|| definition_line.starts_with(r"\protected@xdef"))
{
if let Some(definition) = edef_definition(definition_line, &geometry.macros) {
geometry.macros.push(definition);
@@ -4933,6 +4934,13 @@ fn capture_body_definitions(
changed = true;
continue;
}
if depth == 0
&& let Some(definition) = edef_definition(definition_line, macros)
{
macros.push(definition);
changed = true;
continue;
}
if depth == 0
&& let Some(definition) = macro_definition(definition_line)
&& definition_consumes_line(definition_line)
@@ -14991,6 +14999,22 @@ A definition.
assert!(grouped.contains("twoone"), "{grouped}");
}
#[test]
fn edef_in_body_expands_at_definition_time() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\a{one}\n\\edef\\b{\\a}\n\\def\\a{two}\n\n\\b and \\a\n\n\\end{document}\n";
let json = preview_json(source).expect("preview");
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
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("oneandtwo"), "{text}");
assert_eq!(value["warnings"].as_array().map(Vec::len), Some(0));
}
#[test]
fn group_lines_and_runtime_aliases() {
let text_of = |source: &str| -> String {