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

6 Answers 6

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.

1
  • 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
    Commented Jul 24, 2012 at 6: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.

6
  • 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. Commented Jan 28, 2011 at 9: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. Commented Jan 28, 2011 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
    Commented Jan 28, 2011 at 22:27
  • Oh, and this doesn't leave a space between "bold" and "not"...
    – SamB
    Commented Jan 28, 2011 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 :). Commented Feb 15, 2011 at 20:03
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)

3
  • 2
    If you use \begingroup instead of \bgroup, you get a compile-time check as well. Commented Jan 28, 2011 at 9:02
  • edited (but I usually use \bgroup because I am too lazy to type any more) Commented Jan 28, 2011 at 9:04
  • Hmm, I guess I should have restricted my question to cases not already covered by \begingroup/\endgroup ;-)
    – SamB
    Commented Jan 28, 2011 at 22:24
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .