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

+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 {