Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 63 down vote accepted

\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.

share|improve this answer
25  
\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
2  
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
    
Charles, thanks for the answer. Do you have a reference for the term "semi-simple group"? I can't locate any occurrence of it in the TeXbook... –  Jubobs Nov 21 '13 at 20:32
1  
@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 at 10:32
show 3 more comments

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.

share|improve this answer
    
Not the same effect: \begingroup...\endgroup and {...} in math mode give different results. –  egreg Jul 15 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 at 8:32
1  
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 at 8:35
    
Thanks, I've corrected my post. –  wipet Jul 15 at 8:43
2  
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 at 13:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.