Two ways of defining an 'environment' (not a LaTeX environment)

I would like to understand if there are differences between defining an environment by 'paired definitions' and by 'simple definitions'.

Consider

\long\def\begx#1\endx{\dosomething #1\dosomethingelse}


and

\def\begx{\dosomething\bgroup}
\def\endx{\egroup\dosomethingelse}


Suppose \dosomething and \dosomethingelse are arbitrary token sequences.

I know they are not equivalent: In fact in the first case the argument #1 is not enclosed in a group; and in the second case \begx does not require necessarily an \endx to terminate, but it is sufficient an \egroup. A part from this differences, what advantages are there in using the first approach over the second in defining an environment? Sometimes I see the first, some other times I see the second. But I can't understand what makes someone choose one approach over the other.

EDIT

I just found a little difference: the first captures the argument using the category codes defined before \begx. So, for example,

\def\begx{\bgroup}
\def\endx{\egroup}

\begx
\catcode\*=\active
\def*{\relax}
\endx


is valid, but

\def\begx#1\endx{#1}

\begx
\catcode\*=\active
\def*{\relax}
\endx


isn't.

• i can't think of any real difference between the two forms of definition you've shown. in both cases you can forget to input the \endx and get in trouble. in both cases you can bury the argument in a group (although you haven't here). you've taken care of providing the ability to have multiple paragraphs by specifying \long with the first form. with either version you can redefine \dosomething and/or \dosomethingelse. so it's probably a matter of personal preference. (i'm willing to be shown to be wrong by means of a counterexample or a better argument.) – barbara beeton Dec 26 '15 at 20:07
• @touhami I think this is the same as in my EDIT. Am I right? – User Dec 26 '15 at 20:40

In the first form. the "environment body" is picked up as a macro argument so the whole content is tokenised before the command is executed. This explains the reason why catcode changes have no effect.

The second form is normally used with the grouping before the command, a definition equivalent to latex environment forms would be

\def\begx{\begingroup\dosomething}
\def\endx{\dosomethingelse\endgroup}


The other main difference is nesting, if you want

\begx
..
\begx
..
\endx
...
\endx


then the group form automatically works via tex grouping but the delimited argument form will break, with the outer \begx ending at the first \endx.

Sometimes you need the environment body as a macro argument, so want a delimited argument form but with a more complicated definition that checks for matching begin end pairs, and re-starts the "collection" if an unmatched end is found, This is the basis of environments such as ams alignments, tabularx, or environments defined via the environ package in latex.