Reserve environment definitions declared in the body

This commit is contained in:
gloamxun committed 2026-10-26 20:36:30 +08:00
1 parent 0d9ed56a55
commit 40abb7bef3
3 files changed
+23 -4

No files matched your search

+21 -2
View File
@@ -4250,12 +4250,20 @@ fn capture_body_definitions(
macros: &mut Vec<MacroDefinition>,
conditionals: &mut Vec<ConditionalDefinition>,
theorems: &mut Vec<TheoremDefinition>,
environments: &mut Vec<EnvironmentDefinition>,
) {
let mut kept = String::new();
let mut changed = false;
for line in body.lines() {
let trimmed = line.trim_start();
let definition_line = strip_definition_prefixes(trimmed);
if trimmed.starts_with("\\newenvironment") || trimmed.starts_with("\\renewenvironment") {
if let Some(definition) = environment_definition(definition_line) {
environments.push(definition);
}
changed = true;
continue;
}
if let Some(definition) = newtheorem_definition(definition_line) {
theorems.push(definition);
changed = true;
@@ -4318,8 +4326,6 @@ fn geometry_and_body(source: &str) -> (String, PageGeometry) {
// paragraphs of their own.
let mut body = latex_body(&stripped).trim_matches('\n').to_string();
geometry.bibliography = extract_bibliography(&mut body);
body = expand_environments(&body, &geometry.environments);
body = expand_saveboxes(&body, &mut geometry.macros);
capture_body_registers(
&mut body,
&mut geometry.count_registers,
@@ -4331,7 +4337,10 @@ fn geometry_and_body(source: &str) -> (String, PageGeometry) {
&mut geometry.macros,
&mut geometry.conditionals,
&mut geometry.theorem_definitions,
&mut geometry.environments,
);
body = expand_environments(&body, &geometry.environments);
body = expand_saveboxes(&body, &mut geometry.macros);
dedupe_macros(&mut geometry.macros);
let macros = geometry.macros.clone();
body = expand_conditionals(
@@ -12767,6 +12776,16 @@ See \ref{fig:a} and \ref{tab:b}.";
assert!(!json.contains("newtheorem"));
}
#[test]
fn body_environment_definitions_are_reserved() {
let source = "\\newenvironment{note}{Note: }{!}\n\n\\begin{note}Body.\\end{note}";
let (body, _geometry) = geometry_and_body(source);
assert_eq!(body.trim(), "Note: Body.!");
let json = preview_json(source).expect("preview");
assert!(json.contains("\"warnings\":[]"), "warnings: {json}");
assert!(!json.contains("newenvironment"));
}
#[test]
fn body_definitions_expand_later_in_the_document() {
let source = "\\def\\greet{Hello}\n\n\\greet world.";