Skip to content

Commit

Permalink
Fix incorrect handling of user-defined environments introduced in #856 (
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvc committed Oct 8, 2024
1 parent 43bb7d9 commit 96e2080
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions ts/input/tex/newcommand/NewcommandMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 96e2080

Please sign in to comment.