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.

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

share|improve this question
add comment

6 Answers

up vote 7 down vote accepted

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
1  
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
1  
@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
\showboxbreadth=999
\showbox\savedparbox

Another ordinary paragraph.
\bye
share|improve this answer
add comment

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
share|improve this answer
add comment

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)

share|improve this answer
1  
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
add comment
\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

share|improve this answer
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.