From c5b841b2147c0f99a37667805d2592533188bd10 Mon Sep 17 00:00:00 2001 From: "Davide P. Cervone" Date: Tue, 8 Oct 2024 11:37:59 -0400 Subject: [PATCH] Fix incorrect handling of user-defined environments introduced in #856 (#1137) --- ts/input/tex/newcommand/NewcommandMethods.ts | 37 ++++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/ts/input/tex/newcommand/NewcommandMethods.ts b/ts/input/tex/newcommand/NewcommandMethods.ts index 71eb62edc..f4c8b2fa8 100644 --- a/ts/input/tex/newcommand/NewcommandMethods.ts +++ b/ts/input/tex/newcommand/NewcommandMethods.ts @@ -257,18 +257,22 @@ const NewcommandMethods: { [key: string]: ParseMethod } = { if (begin.getProperty('end') && parser.stack.env['closing'] === name) { // @test Newenvironment Empty, Newenvironment Content delete parser.stack.env['closing']; - if (edef && parser.stack.env['processing'] !== name) { - // Parse the commands in the end environment definition, and do the \end again - parser.stack.env['processing'] = name; - parser.string = ParseUtil.addArgs( - parser, - `${edef}\\end{${begin.getName()}}`, - parser.string.slice(parser.i) - ); - parser.i = 0; - return null; + const beginN = parser.stack.global['beginEnv'] as number; + if (beginN) { + const beginItem = parser.stack.Top(parser.stack.height - beginN); + const prevBegin = beginItem.getProperty('prev-begin') as number; + parser.stack.global['beginEnv'] = prevBegin || 0; + if (edef) { + // Parse the commands in the end environment definition. + let rest = parser.string.slice(parser.i); + parser.string = edef; + parser.i = 0; + parser.Parse(); + // Reset to parsing the remainder of the expression. + parser.string = rest; + parser.i = 0; + } } - delete parser.stack.env['processing']; // Close this environment. return parser.itemFactory.create('end').setProperty('name', name); } @@ -277,12 +281,12 @@ const NewcommandMethods: { [key: string]: ParseMethod } = { const args: string[] = []; if (def != null) { // @test Newenvironment Optional, Newenvironment Arg Optional - const optional = parser.GetBrackets('\\begin{' + begin.getName() + '}'); + const optional = parser.GetBrackets(`\\begin{${name}}`); args.push(optional == null ? def : optional); } for (let i = args.length; i < n; i++) { // @test Newenvironment Arg Optional - args.push(parser.GetArgument('\\begin{' + begin.getName() + '}')); + args.push(parser.GetArgument(`\\begin{${name}}`)); } bdef = ParseUtil.substituteArgs(parser, args, bdef); edef = ParseUtil.substituteArgs(parser, [], edef); // no args, but get errors for #n in edef @@ -293,9 +297,12 @@ const NewcommandMethods: { [key: string]: ParseMethod } = { parser.string.slice(parser.i) ); parser.i = 0; - return parser.itemFactory + const item = parser.itemFactory .create('beginEnv') - .setProperty('name', begin.getName()); + .setProperty('name', name) + .setProperty('prev-begin', parser.stack.global['beginEnv']); + parser.stack.global['beginEnv'] = parser.stack.height; + return item; }, Macro: BaseMethods.Macro,