               { include: '$self' }
             ] # This is amazing! It works!
           9: name: 'punctuation.definition.arguments.end.latex'
-        end: '((\\\\)end)(\\{)(\\4)(\\})'
+        end: '((\\\\)end)(\\{)(\\4)(\\})|(?=})'
         endCaptures:
           1: name: 'support.function.environment.end.latex'
           2: name: 'punctuation.definition.function.latex'

        name: 'variable.parameter.optional.function.environment.latex'
        patterns: [{ include: '$self' }]
      9: name: 'punctuation.definition.arguments.optional.end.latex'
    end: '((\\\\)end)(\\{)(\\4)(\\})'
    endCaptures:
      1: name: 'support.function.environment.end.latex'
      2: name: 'punctuation.definition.function.latex'

        name: 'variable.parameter.optional.function.environment.latex'
        patterns: [{ include: '$self' }]
      9: name: 'punctuation.definition.arguments.optional.end.latex'
    end: '((\\\\)end)(\\{)(\\4)(\\})|(?=})'
    endCaptures:
      1: name: 'support.function.environment.end.latex'
      2: name: 'punctuation.definition.function.latex'
