# When might it be helpful to use implicit braces instead of explicit braces?

We've covered the question "When can one use implicit braces instead of explicit braces?"; this leads immediately in my mind to the question "Why would one want to?"

(Remember, implicit braces are e.g. \bgroup and \egroup as defined by the code \let\bgroup={ \let\egroup=} in plain TeX.)

I suppose this is basically the same as the question "When must one use implicit braces instead of explicit braces?"

-

There's a nice example in Appendix E of the TeXbook, where a letter format is described. A few lines of a possible letter:

\address
Prof.~Brian~K. Reid
Department of Electrical Engineering
Stanford University
Stanford, CA 94305

\body


Here, \address and \body are defined as follows:

\def\address{\beginlinemode\getaddress}
#2
\hsize=\longindentation \everypar{\hangindent2em}#2
\def\body{\beginparmode}
\def\beginparmode{\endmode
\begingroup\parskip=\medskipamount
\def\endmode{\par\endgroup}}


Please pay attention mainly to the \vbox\bgroup in \getaddress and to the \egroup in \endmode that gets called by \beginparmode via \body. What happens is that \address starts a \vbox, which is then ended by the occurence of \body (or anything else calling \endmode). The box thus constructed can then be used "both in the letter and in the label on the envelope", as Knuth explains.

-
So it breaks if you try to put the body before the address, or if you try to put something before the body but after the address? Seems like a potential cause of future confusion.. –  naught101 Jul 24 '12 at 6:32

For example, you can do this:

\def\blah{\bgroup\bf}
\def\bloh{\egroup}

not bold \blah bold\bloh not bold


Doing this:

\def\blah{{\bf}
\def\bloh{}}


would have completely different result.

-
I'd call this an example of structure-defining macros. The question is, why not define \def\blahbloh#1{{\bf #1}}? It's an example of the mechanical difference, but not an example of why they are useful. –  Charles Stewart Jan 28 '11 at 9:09
@Charles: As DEK points out on p. 205 in the TeXbook, this can be useful if you don't want to fill TeX's memory with a long argument. He even gives an example with \beginbold and \endbold, but I think nowadays this would only be useful if you have lots of pages in bold. –  Hendrik Vogt Jan 28 '11 at 20:21
On the other hand, one could just use \begingroup/\endgroup for this and avoid having to do so much thinking, right? –  SamB Jan 28 '11 at 22:27
Oh, and this doesn't leave a space between "bold" and "not"... –  SamB Jan 28 '11 at 22:28
@SamB: just leave a space before \bloh. --- Note that replacing \bgroup by \iftrue{\else}\fi, and \egroup by \iffalse{\else}\fi will also work :). –  Bruno Le Floch Feb 15 '11 at 20:03
show 1 more comment

Among a gazillion other uses, it provides a handy way for storing paragraphs in a box for later processing.

\newbox\savedparbox
\def\saveparbox{\par\begingroup
\def\par{\egroup\endgroup}
\global\setbox\savedparbox\vbox\bgroup}

Ordinary paragraph.
\saveparbox
This paragraph will be saved in \string\box\string\savedparbox.
If you wish, you can unpack the box and do all kinds of processing on it.
In this demo, I won't do any processing.
Look in the log file to examine the box contents.

\showboxdepth=99
\showbox\savedparbox

Another ordinary paragraph.
\bye

-

When testing for the presence of a brace using a \futurelet construction.

\makeatletter
\def\isbraced{\futurelet\next\isbraced@}
\def\isbraced@{\ifx\next\bgroup Braced\else Unbraced\fi}
\makeatother

-

Inside macros, \bgroup sometimes helps readability:

\def\IntroEntry#1#2%
{\blank[line]
\begingroup
\setupalign[right]
\setuptolerance[verytolerant]
\setupindenting[no]
\switchtobodyfont[9pt]
\ss
\begingroup
\bf #1\par
\endgroup
#2\par
\endgroup }


(I tend to get confused when there are too many braces)

-
If you use \begingroup instead of \bgroup, you get a compile-time check as well. –  Charles Stewart Jan 28 '11 at 9:02
edited (but I usually use \bgroup because I am too lazy to type any more) –  Taco Hoekwater Jan 28 '11 at 9:04
Hmm, I guess I should have restricted my question to cases not already covered by \begingroup/\endgroup ;-) –  SamB Jan 28 '11 at 22:24
\documentclass{minimal}
\def\A{%
\bgroup
\B}
\def\B{C\egroup}

\begin{document}
\A
\end{document}


in this case it maybe better to use \begingroup ...\endgroup instead

-