# When should one use \begingroup instead of \bgroup?

Is there ever a case when the use of \begingroup is preferred to \bgroup? Obviously, \bgroup is necessary in cases where you need to have balanced braces and thus cannot use {, but when is it necessary to use \begingroup?

A related question would be: What's the difference between a simple group and a semi-simple group?

\bgroup is a synonym for {, which is defined in Plain TeX using \let\bgroup={.

It interacts with TeX's "digestive system" in hairy ways: {s and \bgroups start the same sort of groups, called simple groups, and each can be terminated with either }s or \egroups, since they are the same. But when the TeX digestive system encounters them, they are of different catcodes, so commands that look ahead, e.g., in LaTeX with \section\bgroup Title}, can break this matching.

\begingroup is different. It is a TeX primitive, and it matches a different sort of group that TeX accounts for separately, called "semi-simple groups" (a Knuth joke, I assume). Thus a \begingroup must be terminated by an \endgroup, not a }, and vice versa for \endgroup.

I generally avoid \bgroup, and use \begingroup, but \bgroup could be useful if you are messing about with a nested token list.

• \begingroup...\endgroup also behaves differently in math mode, in which {...} or \bgroup...\egroup create a subformulae with different spacing. A good rule of thumb would be to avoid using \bgroup...\egroup unless you know you need them. – Will Robertson Aug 18 '10 at 10:05
• I don't know exactly how they behave so my preference goes this way use {} when it works, otherwise \begingroup \endgroup. – Leo Liu Aug 18 '10 at 14:15
• Will is right: I'd strongly favour \begingroup ... \endgroup. – Joseph Wright Aug 18 '10 at 18:35
• Apart from Will Robertson comment, the answer is a little vague (or may be I don't understand). You only said that they are different, but not really the reasons why it is, and the consequences it has to use one or other in different occasions (where the difference is relevant). Could you expand your answer a little bit? – Manuel May 6 '14 at 10:00
• @Manuel I think that adding some examples would be the right way to communicate the differences. I'll add something in due course. – Charles Stewart Jun 27 '14 at 10:32

I have a little bit different opinion about \bgroup versus \begingroup than the previous answer. First, I give a short explanation about TeX internals:

The { and } (more exactly the tokens with catcode 1 and 2) have four different meanings in TeX:

• they are the syntactic part of macro definitions: \def\foo...{...}

• each sequence of tokens scanned as single token list in TeX (i.e. in the parameter of a macro, inside macro definitions...) have to be balanced text by these tokens: \macro{param{e}ter}.

• they are a part of several TeX primitive constructions, for example \hbox...{...}, $e^{...}$, \write...{...}.

• when they are used without any context mentioned above they open and close the group.

The \bgroup and \egroup are declared by \let\bgroup={ \let\egroup=} and you can replace { and } by \bgroup and \egroup only in the third and fourth meaning mentioned above. For example:

\hbox ... \bgroup ...}
\hbox ... {...\egroup
\hbox ... \bgroup ...\egroup
$e^\bgroup ...}$, $e^\bgroup...\egroup$
\write ...\bgroup ...}


The last line in the example is another exception: you canot replace the closing brace by \egroup if the syntactic rule <general text> (like in \write parameter) is applied.

The \bgroup and \egroup without the context mentioned in the third meaning above open and close a group. The same work do primitive commands \begingroup and \endgroup but only this work. They cannot be used instead of { and } in the first to third meaning mentioned above.

This means that always you can use \begingroup and \endgroup, it is possible to use \bgroup and \egroup with the same effect. But the nested groups have to be terminated by the appropriate counterpart, they cannot be crossed. And in math mode, there is a little bit different behavior, see comments.

I am preferring \bgroup and \egroup instead \begingroup and \endgroup. Only, if I see that the nested group crossing error would be usable for users of my macros then I use \begingroup and \endgroup.

• Not the same effect: \begingroup...\endgroup and {...} in math mode give different results. – egreg Jul 15 '14 at 8:27
• Of course, because {...} used in math mode is used in the third type of meanings mentioned above not the fourth. I.e they consists the math subformula. They begins and ends the group in math mode as a side effect, \hbox does the same side effect. – wipet Jul 15 '14 at 8:32
• You say “whereever you can use \begingroup and \endgroup it is possible to use \bgroup and \egroup with the same effect”, which is not true. ;-) – egreg Jul 15 '14 at 8:35
• To sum up, the only feature of using \begingroup and \endgroup at all in TeX is their “transparency” in math mode. I had always thought they where something more (say more important, more relevant, more useful…). – Manuel Jul 15 '14 at 13:16