10

In TeX, the same characters (namely, { and }) are used for grouping and to define macros (\def{...}). I think that this is because it can be distinguished from context which one is which.

So, sometimes { and } are equivalent to \begingroup and \endgroup, and sometimes not. But it turns out that there is one more way how curly braces are used.

Consider answer to exercise 17.16 in TeXbook:

\def\sqr#1#2{{\vcenter{\vbox{\hrule height.#2pt
\hbox{\vrule width.#2pt height#1pt \kern#1pt
\vrule width.#2pt}
\hrule height.#2pt}}}}
\def\square{\mathchoice\sqr34\sqr34\sqr{2.1}3\sqr{1.5}3}

We can remove one level of outer braces from \def\sqr and use them directly in \def\square like this:

\def\square{\mathchoice{\sqr34}{\sqr34}{\sqr{2.1}3}{\sqr{1.5}3}}

So, in what sense are braces used in \mathcoice? They are not here to create block structure (because here we cannot replace { and } to \begingroup and \endgroup), and these braces do not define a macro here. BTW, why those braces are necessary for \mathchoice (isn't \vbox returned by \sqrXY a single token)?

Also, in what sense is { used in \uppercase\expandafter{\romannumeral\year}: why cannot { and } here be replaced by \begingroup and \endgroup?

How to distinguish in each occurence of { or } in what sense it is used? For example, if we type {12} it means that TeX sees token 12, or that TeX executes \begingroup 12 \endgroup? If the latter is true, how does the outer function, to which {12} is passed as an argument, gets 12? Is it that last token between \begingroup ... \endgroup is returned by \endgroup or some other mechanism is used?

EDIT: In this connection, why do we need curly braces around \mathchoice... in the following code from exercise 17.15? (i.e., why it does not work without them?)

\def\puzzle{{\mathchoice{D}{T}{S}{SS}}}
$$\puzzle{\puzzle\over\puzzle^{\puzzle^\puzzle}}$$

Compare with the following, where \alpha is used without extra braces:

$$\alpha{\alpha\over\alpha^{\alpha^\alpha}}$$

Also notice that in the answer to exercise 17.16 Knuth does not put braces around \mathchoice.... Why?

    \def\sqr#1#2{{\vcenter{\vbox{\hrule height.#2pt
        \hbox{\vrule width.#2pt height#1pt \kern#1pt
        \vrule width.#2pt}
        \hrule height.#2pt}}}}
    \def\square{\mathchoice\sqr34\sqr34\sqr{2.1}3\sqr{1.5}3}
  • 3
    { and } are never equivalent to \begingroup and \endgroup. – egreg Sep 13 '15 at 22:34
  • @egreg But they create block structure? – Igor Liferenko Sep 13 '15 at 22:34
  • 1
    they both create groups but these are of different types {\endgroup is an error (unlike {\egroup which is not always an error. the log file distinguishes these as semi simple group (which is probably a joke name:-) – David Carlisle Sep 13 '15 at 22:39
12

Primitives that, according to the syntax rules, should be followed by <general text> have a very peculiar behavior: they expand the next token (if expandable) in order to find tokens that qualify as <filler> and, eventually, the left brace that delimits the <balanced text>. More precisely, this happens whenever the syntax rule allows a <filler> (which is a sequence of any number of \relax and space tokens, after expansion).

Such a brace can even be implicit. So if you do

\def\foo{{abc}}

then \uppercase\foo will produce ABC. For \mathchoice the situation is similar and also for _ and ^. The syntax rule says (TeXbook, p. 292)

\mathchoice<filler>{<math mode material>}<filler>{<math mode material>}<filler>{<math mode material>}<filler>{<math mode material>}

and this means exactly the same thing: in order to find a <filler>, TeX does expansion.

So if \sqr is defined in the shown way, when \mathchoice does its working, it expands \sqr that so looks for its argument and gets replaced by the replacement text; since the brace is part of the replacement text, \mathchoice “sees” it.

In \uppercase\expandafter{\romannumeral\year} the same happens: \expandafter gets expanded. The braces delimit a <general text>, so they don't form a group.

The syntax rules tell precisely when a new level of grouping is entered in consequence of a {. This happens for boxes, for instance; in these cases, both the opening brace and the closing brace can be implicit. In the case of a <general text> the closing brace must be explicit, because the syntax rule reads (p. 276)

<general text> → <filler>{<balanced text><right brace>

Never confuse groups delimited by { and } with ones delimited by \begingroup and \endgroup.

When TeX sees {12} and is not looking for an open brace according to some syntax rule, it opens a “simple group”, executes the tokens it finds and, upon finding the matching } closes the group; \begingroup12\endgroup can never be interpreted as a <general text>, because it lacks {. TeX opens a “semisimple group”, executes the tokens and, upon finding the matching \endgroup closes the group.

  • see Edit in the question – Igor Liferenko Sep 16 '15 at 0:20
  • 1
    @IgorLiferenko Exactly the same feature: ^ allows a <filler>, so it expands the next token; the expansion of \puzzle starts with { which so begins the exponent. – egreg Sep 16 '15 at 8:12
  • Do you consider the following changes in your answer reasonable? 1) "the closing brace must be explicit" -> "the opening brace must be explicit"; 2) "can be implicit" -> "may be implicit". And is the following statement correct? "⟨balanced text⟩ consists only of characters, ligatures, kerns, boxes, and rules" – Igor Liferenko Sep 16 '15 at 9:13
  • @IgorLiferenko The first proposed change would be wrong; the second is just style. And the final statement is incorrect. A <balanced text> is any list of tokens that is balanced with respect to explicit braces. – egreg Sep 16 '15 at 9:18
  • 2
    @IgorLiferenko According to the conventions in the TeXbook, a { in the syntax rules denotes an explicit or implicit character of category 1, whereas <left brace> denotes an explicit character of category 1; similarly for } and <right brace>. Ask Knuth why he decided for this. – egreg Sep 17 '15 at 7:57
7

The braces {} (more precisely the chracters with the catcode 1 and 2) have various meanings depending of the context.

(1) They begins and ends group when they are use alone. Moreover, in math mode, they creates new Ord atom.

(2) They are separators for parameters of macros (and they are removed from the boundary of the parameter). For example \macro{parameter}.

(3) They are a part of syntax rules when TeX primitives are used (for example in \def{body of macro} or in \message{message text} or \mathchoice{A}{B}{C}{D}.

The nested {} must always match regardless of they actual meaning. The special exceptions from this rule are possible in some primitives due to their syntax described in the TeXbook: \message\bgroup Hello} or \hbox {text\egroup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.