Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

newb: (plainTeX) difference between '{', \bgroup, and \begingroup

1,828 views
Skip to first unread message

eds

unread,
Sep 4, 2012, 11:08:47 AM9/4/12
to
I haven't been able determine the difference between the following:

- the character whose catcode is 1, normally the '{', the open brace
- the \bgroup control sequence, and
- the \begingroup control sequence.

Can someone explain the differences?

regards,
eds.

Heiko Oberdiek

unread,
Sep 4, 2012, 12:04:50 PM9/4/12
to
Ten years ago I had given a talk about this topic, but the slides and
handout are in German:

Slides:
http://archiv.dante.de/dante2002/handouts/oberdiek-geschweifteklammer-folien.pdf

Handout:
http://archiv.dante.de/dante2002/handouts/oberdiek-geschweifteklammer.pdf

--
Heiko Oberdiek

Dan Luecking

unread,
Sep 4, 2012, 1:06:37 PM9/4/12
to
On Tue, 4 Sep 2012 08:08:47 -0700 (PDT), eds <edstu...@gmail.com>
wrote:
\begingroup is the simplest: it starts a group and that
group must be ended by \endgroup. It is used only for
grouping. That is, to ensure that definitions or other
assignments are local unless explicitly made global.

\bgroup is defined by \let\bgroup={. Wherever in the TeXbook
it talks about "a control sequence \let equal to a character",
then it is talking about \bgroup (among others). It may be
used in some (but not all) places as a replacement for {:

\ifx\bgroup{ is true
\if\bgroup{ is true
\ifcat\bgroup{ is true

One may use \hbox\bgroup...\endgroup
One may use \toks0=\bgroup...}
but not \toks0=\bgroup...\egroup nor \toks0={...\egroup

One may not replace the braces around the replacement text of a
\def with \bgroup or \egroup.

\count0=`{ is valid and assigns 123 to \count0, but
\count0=`\bgroup is an error.

When the TeXbook refers to <balanced text>, it means
characters with category 1 and 2 must be balanced. Typically
this means { and }. \bgroup, \egroup, \begingroup and endgroup
are irrelevent.

A <math field> consisting of more than one character can be
delimited with { and } or with \bgroup and \egroup or they
can be mixed: $x_{mn\egroup$ or $x_\bgroup mn}$.
But \begingroup and \endgroup do not work.

Also, one can change the category code of characters like { and },
but this has no effect on \bgroup and \egroup.

Where the syntax rules of the TeXbook, chapter 24 (and elsewhere)
show a "{" or a "}", then also \bgroup or \egroup may be used. If
the syntax requires a character with category 1 or 2, then the
rules contain "<left brace>" or "<right brace>". These are the
final arbiters of the differences between \bgroup and {.


Dan
To reply by email, change LookInSig to luecking

Enrico Gregorio

unread,
Sep 4, 2012, 1:23:28 PM9/4/12
to
A character of catcode 1 has many uses in TeX's syntax. In some
cases it can be replaced by \bgroup, in others it can't.

A { is compulsory for delimiting the body of a definition, for instance,
and \bgroup can't be used.

It's also compulsory for delimiting an argument to a macro (in case
it consists of more than one token and the argument is not delimited,
see chapter 20 of the TeXbook for delimited and undelimited arguments).

Thus with

\def\macro#1{something {\it #1\/}}

you can say

\macro{fun}

but *not*

\macro\bgroup fun\egroup

By the way, this wouldn't give any error: \bgroup would be the argument
to \macro and the replacement would give

something {\it\bgroup\/}fun\egroup

and here another feature comes into play: when braces are used to
delimit groups (not macro arguments) they /can/ be replaced by
\bgroup and \egroup. So \bgroup matches } and the initial { matches
\egroup.

In other cases \bgroup can be used in place of {, for instance as
the opening brace for the argument to \lowercase, \uppercase or
an assignment such as

\toks0=\bgroup ...}

(the closing must be a real }, not \egroup, in all these cases).

A rule of thumb is that \bgroup can be used in place of { in all
situations where TeX expands tokens in order to find an opening
brace. Such is the case with \lowercase and \uppercase, but also
with the e-TeX primitives \detokenize and \unexpanded.

\bgroup can also be used with all primitives that receive an
argument which is enclosed in an implicit group: \hbox, \vbox,
\vtop, \halign, \valign, \noalign, \vadjust. For this kind of
primitives, the end of the argument can be \egroup.
This allows starting a box with a macro and finish it with another:

\def\startfunnyhbox{\hbox\bgroup...}
\def\stopfunnyhbox{...\egroup}

The question with \begingroup is completely different. This
primitive can /never/ be used as a replacement of { when an
open brace (explicit or implicit) is required. It just opens
a group of a different kind than what would be opened by {
or \bgroup when they don't appear in the context of an argument
to a primitive. A group started with { or \bgroup is called a
"simple group", while one started with \begingroup (and ended
with \endgroup) is called a "semisimple group".

Simple groups and semisimple groups cannot overlap. (There are
many other kind of groups, and none of them can overlap with
another one.)

Simple groups and semisimple groups are mostly equivalent in
normal text (horizontal or vertical mode), but they differ
in math mode: with

$a{+}b$

the group forms a subformula which is treated as an ordinary
atom; thus the output will be different from $a+b$. To the
contrary,

$a\begingroup +\endgroup b$

will result in the same output as $a+b$. A semisimple
group in math mode doesn't make a subformula, but assignments
inside it will be local anyway.

Of course this is just an introduction and much more could
be said. Refer to the TeXbook or TeX by Topic for a more
complete description.

Ciao
Enrico
0 new messages