8

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?"

SamB
  • 2,837

6 Answers6

9

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}
{\obeylines\gdef\getaddress #1
  #2
  {#1\gdef\addressee{#2}%
    \global\setbox\theaddress=\vbox\bgroup\raggedright%
    \hsize=\longindentation \everypar{\hangindent2em}#2
    \def\endmode{\egroup\endgroup \copy\theaddress \bigskip}}}
\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.

Hendrik Vogt
  • 37,935
  • 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 06:32
8

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.

Jan Hlavacek
  • 19,242
  • 2
    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 09:09
  • 3
    @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
  • 1
    @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
  • @Bruno: I know how to fix it, I was just pointing out an apparant mistake I had just noticed in examining the code. (What, did you think I had actually tried it or something?) I am, however, not entirely certain that spaces are treated in the same manner between bold/non-bold text... – SamB Feb 15 '11 at 22:14
7

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
\showboxbreadth=999
\showbox\savedparbox

Another ordinary paragraph.
\bye
6

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
3

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)

Taco Hoekwater
  • 13,724
  • 43
  • 67
1
\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